#pragma GCC optimize("O3", "unroll-loops") #include using namespace std; #define ll long long int main(void){ int N; cin >> N; vector A(N); for (int i = 0; i < N; ++i) cin >> A[i]; // 全部同じ色か判定 bool all_same = true; for (int i = 1; i < N; ++i) all_same &= A[i] == A[i - 1]; if (all_same){ cout << 0 << endl; return 0; } // 区切れ目を求める int idx = 0; while (A[idx] == A[(idx - 1 + N) % N]) idx = (idx - 1 + N) % N; // 答えを求める map appered; appered[A[idx]] = 1; for (int i = 1; i < N; ++i){ if (A[(idx + i) % N] == A[(idx + i - 1) % N]) continue; if (appered[A[(idx + i) % N]]){ cout << -1 << endl; return 0; } else appered[A[(idx + i) % N]] = 1; } if (idx == 0) cout << 0 << endl; else cout << 1 << endl; return 0; }