#include #include #include using namespace std; int main(){ long long N; cin >> N; vector a(N); for(int i = 0; i < N; i++){ cin >> a[i]; a[i]--; } vector zi; zi.push_back(a[0]); for(int i = 1; i < N; i++){ if(a[i] != a[i - 1]) zi.push_back(a[i]); } vector cnt(N, 0); for(int i = 0; i < zi.size(); i++){ cnt[zi[i]]++; if(cnt[zi[i]] > 2){ cout << -1 << endl; return 0; } if(cnt[zi[i]] == 2 && i == zi.size() - 1 && zi[0] == zi[i]){ cout << 1 << endl; return 0; } else if(cnt[zi[i]] == 2){ cout << -1 << endl; return 0; } } cout << 0 << endl; }