#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using P = pair; constexpr int INF = 1001001001; constexpr int mod = 1000000007; // constexpr int mod = 998244353; template inline bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } template inline bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N, A; cin >> N; vector> pos(N + 1); bool solo = true; for(int i = 0; i < N; ++i){ cin >> A; if(pos[A].size()) solo = false; pos[A].emplace_back(i); } if(solo){ cout << 0 << endl; return 0; } int ans = 0; for(int k = 1; k <= N; ++k){ if(pos[k].size() <= 1) continue; bool link = true, multi = false; int n = pos[k].size(); for(int i = 0; i + 1 < n; ++i){ if(pos[k][i] + 1 != pos[k][i + 1]){ if(link) link = false; else multi = true; } } if(multi){ cout << -1 << endl; return 0; } if(!link){ if(pos[k][0] == 0 && pos[k][n - 1] == N - 1){ ans = 1; } else{ cout << -1 << endl; return 0; } } } cout << ans << endl; return 0; }