#include <bits/stdc++.h>
using namespace std;
vector<int> a;
vector<int> b;
int main()
{
int n;
cin >> n;
a.resize(n + 1);
for(int i = 1;i <= n;i++) cin >> a[i];
sort(a.begin() + 1,a.end());
n = unique(a.begin() + 1,a.end()) - a.begin() - 1;
if(n <= 2)
{
cout << -1 << endl;
return 0;
}
// for(int i = 1;i < n;i++)
// {
// b.push_back(a[i]);
// for(int j = i + 1;j <= n;j++)
// {
// b.push_back(a[j] % a[i]);
// }
// }
// sort(b.begin(),b.end());
// int m = unique(b.begin(),b.end()) - b.begin();
// if(m <= 2) cout << -1 << endl;
// else
// {
// cout << b[m - 2] << endl;
// }
cout << max(a[n] % a[n - 1],a[n - 2]) << endl;
return 0;
}