#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main(){ cin.tie(0); ios::sync_with_stdio(0); int N; cin >> N; vector A(N); rep(i,N) cin >> A[i]; vector> V = {{A[0], 1}}; rep(i,N) if(i) { if(V.back().first == A[i]) V.back().second++; else V.push_back({A[i], 1}); } int cost = 0; if(V.size() >= 2 && V[0].first == V.back().first) { cost++; V[0].second += V.back().second; V.pop_back(); } vector used(N + 1, 0); for(auto [x, cnt] : V) { if(used[x]){ cout << -1 << endl; return 0; } used[x] = 1; } cout << cost << endl; }