#include using namespace std; #define rep(i, a, b) for(int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair pii; typedef vector vi; template bool chmin(H& v1, const H v2) { if (v1 > v2) { v1 = v2; return true; } return false; } template bool chmax(H& v1, const H v2) { if (v1 < v2) { v1 = v2; return true; } return false; } template void read(H& head) { cin >> head; } template void read(H& head, T& ...tail) { cin >> head; read(tail...); } template void write(H head) { cout << head << '\n'; } template void write(H head, T ...tail) { cout << head << " "; write(tail...); } template void die(T ...tok) { write(tok...); exit(0); } int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); int n; read(n); vector v(n), lst(n + 1, -1), dis(n + 1, 0); int discount = 0; rep(i, 0, n) cin >> v[i]; rep(i, 0, n) { if (lst[v[i]] != -1 && i != lst[v[i]] + 1) { dis[i] = 1; discount++; // www } lst[v[i]] = i; } if (discount > 1) die(-1); if (discount == 0) die(0); // shift last block to head lst = vi(n + 1, -1); dis = vi(n + 1, 0); int tg = v.back(), cnt = 0; while (v.size() && v.back() == tg) cnt++, v.pop_back(); vi tp(cnt, tg); v.insert(v.begin(), all(tp)); discount = 0; rep(i, 0, n) { if (lst[v[i]] != -1 && i != lst[v[i]] + 1) { dis[i] = 1; discount++; } lst[v[i]] = i; } die(discount == 0 ? 1 : -1); }