#include #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--) #define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++) #define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++) #define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++) #define ALL(x) x.begin(), x.end() using ll = long long; using namespace std; int main(){ int n; cin >> n; map mp; int mode = -1; int ans = 0; rep(i, n){ int in; cin >> in; if(in != mode){ mp[mode]++; if(mp[mode] == 2){ ans++; }else if(mp[mode] > 2){ cout << -1 << endl; return 0; } mode = in; } } mp[mode]++; if(mp[mode] == 2){ ans++; }else if(mp[mode] > 2){ cout << -1 << endl; return 0; } cout << ans << endl; return 0; }