#include using namespace std; #define rep(i,n) for(int i = 0; i < (n);i++) #define sz(x) int(x.size()) typedef long long ll; typedef long double ld; typedef pair P; constexpr int INF = 2e9; int main() { int n; cin >> n; vector a(n); rep(i,n) cin >> a[i], --a[i]; a.emplace_back(INF); int prev = 0; vector p; for (int i = 0; i <= n; i++) { if (a[i] != a[prev]) { p.emplace_back(a[prev]); prev = i; } } vector cnt(n, 0); bool ok = true; for (int i = 0; i < p.size(); i++) { cnt[p[i]]++; if (cnt[p[i]] > 2) ok = false; } int d = 0; for (int i = 0; i < n; i++) { if (cnt[i] == 2) { d = 1; if (p.back() != p.front()) ok = false; } } if (ok) cout << d << endl; else cout << -1 << endl; return 0; }