#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define int long long
#define inf 1e18
#define rep(i, l, n) for (int i = l; i <= n; i++)
#define per(i, l, n) for (int i = n; i >= l; i--)
//cout<<fixed<<setprecision(15)
const int mol=998244353;
const int N = 3e5+10,M=5010;
int qmi(int a,int k){int res=1;while(k){if(k&1)res=res*a%mol;a=a*a%mol;k>>=1;}return res;}
int inv(int x){return qmi(x%mol,mol-2);}
int n,m,q,k;
int a[N];
void solve()
{
cin>>n;
rep(i,1,n)cin>>a[i];
m=(a[1]-a[0]+n)%n;
rep(i,1,n-1)
{
if((a[i+1]-a[i]+n)%n!=m)
{
cout<<-1<<endl;
return ;
}
}
if(!a[0]){cout<<m<<endl;
return ;
}
if(!m)m+=n;
return m+1;
}
signed main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T = 1;
//cin>>T;
for (int i = 1; i <= T; i++)solve();
return 0;
}