#include #define rep(i,n) for(int i=(0);i<(n);i++) using namespace std; typedef long long ll; int main(){ cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; deque deq; rep(i, n){ int a; cin >> a; deq.push_back(a); } int x = deq[0]; while(!deq.empty() && deq.front() == x){ deq.pop_front(); } bool erased = false; while(!deq.empty() && deq.back() == x){ erased = true; deq.pop_back(); } if(deq.empty()){ cout << 0 << endl; exit(0); } set st; st.insert(x); x = deq.front(); while(!deq.empty()){ if(st.count(deq.front()) == 1){ cout << -1 << endl; exit(0); } if(deq.front() != x){ st.insert(x); x = deq.front(); } deq.pop_front(); } if(erased){ cout << 1 << endl; }else{ cout << 0 << endl; } }