#include using namespace std; #define int long long #define rep(i,n) for(int i=0;i>n; rep(i,n){ cin>>a[i]; assert(a[i]<=n); } vectorv; int now=a[0]; rep(i,n+1){ if(now!=a[i]){ v.push_back(now); now=a[i]; } } setS; int flg=0; rep(i,v.size()){ if(S.find(v[i])!=S.end()){ flg++; if(i-1!=v.size())flg++; } S.insert(v[i]); } if(flg>1)flg=-1; cout<