#include #include #include #include #include #include #include using namespace std; #define int long long #define endl "\n" constexpr long long INF = (long long)1e18; constexpr long long MOD = 1'000'000'007; struct fast_io { fast_io(){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); }; } fio; class union_find { int _setnum; vector par, nume; public: union_find(){ } union_find(int x){ par.resize(x); nume.resize(x); init(); } ~union_find(){ // } void clear(){ _setnum = 0; par.clear(); nume.clear(); } void init(){ _setnum = par.size(); for(int i = 0; i < par.size(); i++){ par[i] = i; nume[i] = 1; } } void resize(int x){ par.resize(x); nume.resize(x); init(); } int find(int x){ return par[x] == x ? x : par[x] = find(par[x]); } void unite(int x, int y){ x = find(x); y = find(y); if(x == y)return; _setnum--; if(nume[x] > nume[y]) std::swap(x,y); par[x] = y; nume[y] += nume[x]; } bool same(int x, int y){ return find(x) == find(y); } int numel(int x){ return nume[find(x)]; } int size(){ return par.size(); } int setnum(){ return _setnum; } }; signed main(){ cout<> ind(100000); vector A; int ans = 0; int N; union_find uf; cin>>N; A.resize(N); uf.resize(N); for(int i = 0; i < N; i++){ cin>>A[i]; A[i]--; ind[A[i]].push_back(i); } for(int i = 0; i < ind.size(); i++){ if(ind[i].size() <= 1) continue; if(ind[i].size() == N) continue; if(ind[i].front() == 0 && ind[i].back() == N-1) { uf.unite(0, N-1); ans = 1; } for(int j = 1; j < ind[i].size(); j++){ if(ind[i][j-1] + 1 == ind[i][j]) { uf.unite(ind[i][j-1], ind[i][j]); } } if(uf.numel(ind[i][0]) != ind[i].size()) { ans = -1; break; } } cout<