#include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define int long long #define double long double typedef vector VI; typedef pair pii; typedef vector VP; typedef vector VS; typedef priority_queue PQ; templatebool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } templatebool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define fore(i,a) for(auto &i:a) #define REP(i,n) for(int i=0;i, greater > q2; signed main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; VI A(N); REP(i, N)cin >> A[i]; VI B, C; REP(i, N) if (i == N - 1 || A[i] != A[i + 1])B.push_back(A[i]); VI cnt(N + 1, 0); REP(i, B.size()) { if (++cnt[B[i]] == 3) { cout << -1 << endl; return 0; } } REP(i, B.size()) { if (cnt[B[i]] == 2) { C.push_back(B[i]); } } int L = 0, R = C.size() / 2; while (R < C.size()) { if (C[L++] != C[R++]) { cout << -1 << endl; return 0; } } cout << C.size() / 2 << endl; return 0; }