#include <bits/stdc++.h>
using namespace std;
int a[200010], t[200010];
int main(){
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
sort(a+1, a+n+1);
int cur = 0;
int x = -1;
for (int i = n; i >= 1; i--){
if (a[i] != x){
t[++cur] = x = a[i];
}
}
if (cur < 3){
cout << "-1\n";
goto P;
}
cout << max(t[cur-2], t[cur]%t[cur-1]) << endl;
P:
return 0;
}