/** * @FileName f.cpp * @Author kanpurin * @Created 2020.05.15 23:34:07 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; int main() { int n; cin >> n; vector< int > a(n); vector< int > used(n, -1); bool same = true; for (int i = 0; i < n; i++) { cin >> a[i]; if (i > 0 && a[0] != a[i]) { same = false; } } if (same) { cout << 0 << endl; return 0; } int ok = -1; for (int i = 1; i < n; i++) { if (ok == -1 && a[i] != a[0]) { ok = i; break; } } used[a[0]-1] = 1; for (int i = ok; i < n; i++) { if (a[i] != a[i-1] && abs(used[a[i]-1]) == 1) { used[a[i]-1] = 0; } else if (a[i] != a[i-1]) { puts("-1"); return 0; } } if (a[0] == a[a.size()-1]) { cout << 1 << endl; } else if (used[a[0]-1] == 0) { cout << -1 << endl; } else { cout << 0 << endl; } return 0; }