#include #define rep(i,n) for(int i=0;i<(int)(n);i++) using namespace std; using ll = long long ; using P = pair ; using pll = pair; constexpr int INF = 1e9; constexpr long long LINF = 1e17; constexpr int MOD = 1000000007; int main(){ int n; cin >> n; vector a(n); vector color(n); rep(i,n) cin >> a[i]; rep(i,n) --a[i]; rep(i,n){ color[a[i]]++; } if(color[a[0]]==n){ cout << 0 << endl; return 0; } int p=n-1; while(a[p]==a[0]){ a.pop_back(); --p; color[a[0]]--; } int cnt = 1; bool ok = true; rep(i,a.size()-1){ if(a[i]==a[i+1]){ ++ cnt; }else{ if(color[a[i]]!=cnt) ok = false; cnt = 1; } } if(p==n-1 && ok) cout << 0 << endl; else cout << (ok?1:-1) << endl; return 0; }