#include #include #include using namespace std; int main(){ int n;cin>>n; vector A(n); for(int i = 0; n > i; i++){ cin>>A[i]; } int begi = 0; int eni = n-1; if(A[0] == A[n-1]){ while(begi != n && A[0] == A[begi])begi++; if(begi == n){ cout << 0 << endl; return 0; } while(A[n-1] == A[eni])eni--; } set X; X.insert(A[begi]); for(int i = begi+1; eni >= i; i++){ if(A[i] == A[0]){ cout << -1 << endl; return 0; } if(A[i-1] != A[i]){ if(X.count(A[i])){ cout << -1 << endl; return 0; } X.insert(A[i]); } } if(begi == 0){ cout << 0 << endl; }else{ cout << 1 << endl; } }