n = int(input()) A = list(map(int, input().split())) pre = -1 kouho = set() out = [] for i in range(n): if A[i] == pre: continue if A[i] in kouho: out.append(A[i]) pre = A[i] continue kouho.add(A[i]) pre = A[i] if not out: ans = 0 else: if len(out) == 1 and A[0] == A[-1]: ans = 1 else: ans = -1 print(ans)