n = int(input()) A = list(map(int,input().split())) l = [] for a in A: if l and l[-1] == a: continue l.append(a) dic = {} for i in l: dic[i] = dic.get(i,0)+1 num = 0 for k,v in dic.items(): if v == 1: continue if v > 2: print(-1) exit() if l[0] == l[-1] == k: num += 1 else: print(-1) exit() print(num)