#define _USE_MATH_DEFINES #include using namespace std; //template #define rep(i,a,b) for(int i=(a);i<(b);i++) #define ALL(v) (v).begin(),(v).end() typedef long long int ll; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12; templateinline bool chmax(T& a,T b){if(ainline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} //end bool ok(vector v){ set used; rep(i,0,v.size()){ if(used.count(v[i]) and v[i-1]!=v[i])return 0; used.insert(v[i]); } return 1; } bool pos(vector v){ deque deq; int t=v.front(); rep(i,0,v.size())deq.push_back(v[i]); while(deq.size()>1 and deq.front()==deq.back() and deq.front()==t){ deq.pop_back(); deq.pop_front(); } vector tmp; for(int x:deq)tmp.push_back(x); return ok(tmp); } int main(){ int n; scanf("%d",&n); vector a(n),cmp; rep(i,0,n){ scanf("%d",&a[i]); if(cmp.empty() or a[i]!=cmp.back())cmp.push_back(a[i]); } if(ok(cmp))puts("0"); else if(pos(cmp))puts("1"); else puts("-1"); return 0; }