/** * @FileName f.cpp * @Author kanpurin * @Created 2020.05.15 23:14:45 **/ #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; } } for (int i = ok; i < n; i++) { if (a[i] != a[i-1] && used[a[i]-1] == -1) { used[a[i]-1] = 0; } else if (a[i] != a[i-1]) { puts("-1"); return 0; } } if (used[a[0]-1] == 0) { cout << 1 << endl; } else { cout << 0 << endl; } return 0; }