import sys sys.setrecursionlimit(100000000) MOD = 10 ** 9 + 7 INF = 10 ** 15 def main(): N = int(input()) A = list(map(int,input().split())) X = [] for i in range(N - 1): if A[i] != A[i + 1]: X.append(A[i]) X.append(A[-1]) if len(set(X)) == 1: print(0) return dic = {} for i in range(len(X)): if X[i] in dic: dic[X[i]].append(i) else: dic[X[i]] = [i] ans = 0 M = len(X) for v in dic.values(): if len(v) > 2: print(-1) return if len(v) == 2: if sum(v) == M - 1: ans += 1 else: print(-1) return print(ans) if __name__ == '__main__': main()