#include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define int long long #define double long double typedef vector VI; typedef pair pii; typedef vector VP; typedef vector VS; typedef priority_queue PQ; templatebool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } templatebool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define fore(i,a) for(auto &i:a) #define REP(i,n) for(int i=0;i, greater > q2; signed main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; VI A(N); REP(i, N)cin >> A[i]; int ans = 0; int cnt = 0; stacksta; vectorF(N, false); REP(i, N) { int a = A[i]; if (!F[a]) { F[a] = 1; sta.push(a); chmax(ans, (int)sta.size()); } else { if (i != 0 && A[i - 1] == a)continue; else { bool F = 1; while (!sta.empty()) { if (sta.top() == a) { sta.pop(); F = 0; break; } else { sta.pop(); cnt++; } } if (F) { cout << -1 << endl; return 0; } } } } cout << ans - cnt - sta.size() << endl; return 0; }