#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define llint long long #define inf 1e18 #define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++) #define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++) #define chmin(x, y) (x) = min((x), (y)) #define chmax(x, y) (x) = max((x), (y)) using namespace std; typedef pair P; llint n; llint a[200005]; map mp; int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]; for(int i = 2; i <= n; i++){ if(a[i] != a[i-1]) mp[a[i-1]]++; } mp[a[n]]++; for(auto it = mp.begin(); it != mp.end(); it++){ if(it->second <= 1) continue; if(it->second >= 3){ cout << -1 << endl; return 0; } if(a[1] == it->first && a[n] == it->first) continue; cout << -1 << endl; return 0; } if(a[1] == a[n] && mp.size() >= 2) cout << 1 << endl; else cout << 0 << endl; return 0; }