#include #define REP(i, m, n) for(int i = (int)(m); i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) using namespace std; using ll = long long; using ld = long double; template inline bool chmax(T &a, const U &b) { if(a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, const U &b) { if(a > b) { a = b; return true; } return false; } int n, a[100000]; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; rep(i, n) cin >> a[i], a[i]--; int p = -1; map cnt; rep(i, n) { if(a[i] != p) cnt[a[i]]++; p = a[i]; } int over = 0; bool ok = true; for(auto e : cnt) { if(e.second > 2) ok = false; if(e.second == 2) over++; } if(over > 1) ok = false; if(over == 1 && a[0] != a[n-1]) ok = false; if(ok) { cout << over << '\n'; } else { cout << -1 << '\n'; } return 0; }