#include <bits/stdc++.h>
#define int long long
#define endl "\n"
using namespace std;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
int a[N];
void solve()
{
int n;
cin >> n;
int chazhi = 0;
bool ok = true;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
if (i > 1)
{
if (a[i] >= a[i - 1])
{
if (!chazhi) chazhi = a[i] - a[i - 1];
else
{
if (chazhi != a[i] - a[i - 1]) ok = false;
}
}
else
{
if (!chazhi) chazhi = a[i] + n - a[i - 1];
else
{
if (chazhi != a[i] + n - a[i - 1]) ok = false;
}
}
}
}
if (a[1] >= a[n])
{
if (a[1] - a[n] != chazhi) ok = false;
}
else
{
if (a[1] + n - a[n] != chazhi) ok = false;
}
}
if (!ok)
{
cout << -1 << endl;
return ;
}
if (chazhi == 0 && a[1] != 0)
{
cout << n + 1 << endl;
return ;
}
for (int i = 1; i <= n; i++ )
{
if (a[i] == 0)
{
if (i == 1) cout << a[2] << endl;
else
{
if (i == n) cout << a[1] + 1 << endl;
else cout << a[i + 1] + 1 << endl;
}
return ;
}
}
cout << chazhi + 2 << endl;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int tt = 1;
while (tt--) solve();
return 0;
}