#include using namespace std; using ll = long long; constexpr char newl = '\n'; bool judge(int n, const vector& v) { vector used(n, false); used[v[0]] = true; for (int i = 1; i < n; ++i) { if (v[i] == v[i - 1]) continue; if (used[v[i]]) return false; used[v[i]] = true; } return true; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector a(n); for (int i = 0; i < n; i++) { cin >> a[i]; --a[i]; } int ans = 0; if (!judge(n, a)) { for (int i = 1; i < n; ++i) { if (a[i] == a[i - 1]) continue; rotate(a.begin(), a.begin() + i, a.end()); break; } ans = (judge(n, a) ? 1 : -1); } cout << ans << newl; return 0; }