#include using namespace std; int main() { ios::sync_with_stdio(false); int N; { cin >> N; } vector A(N); { for (int i = 0; i < N; ++i) cin >> A[i]; } int res = 0; A.erase(unique(A.begin(), A.end()), A.end()); if (A.back() == A.front() && A.size() > 2) ++res, A.pop_back(); set bag(A.begin(), A.end()); if (bag.size() == A.size()) cout << res << endl; else cout << -1 << endl; }