#include using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) using ll = long long; using P = pair; int visited[110000]; int main() { int n;cin >>n ; int a[n+1]; rep(i,n)cin >> a[i]; vector c; int now = a[0]; rep(i,n-1){ if(a[i+1] != now){ c.push_back(now); now = a[i+1]; } } c.push_back(now); bool ok = true; int m = c.size(); if(m == 1){ cout << 0 << endl; return 0; } int flag = 0; for(auto p:c){ if(visited[p]){ if(c[0] != c[m-1]){ ok = false; } flag = 1; } visited[p] = 1; } if(ok){ if(flag)cout << 1 << endl; else cout << 0 << endl; } else{ cout << -1 << endl; } return 0; }