#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const int MAXM = 1e9 + 5;
int n, a[MAXN], b[MAXN], cnt, c[MAXN];
int main()
{
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(i == j) continue;
int x = a[i] % a[j];
if(b[x] == 0)
{
b[x] = 1;
cnt++;
c[cnt] = x;
}
}
}
if(cnt < 2)
{
cout << -1;
return 0;
}
sort(c + 1, c + cnt + 1);
cout << c[cnt - 1];
return 0;
}