import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N, *A = map(int, read().split()) B = [A[0]] for x in A: if x != B[-1]: B.append(x) op = 0 if B[-1] == B[0] and len(B) >= 2: B.pop() op = 1 if len(B) == len(set(B)): print(op) else: print(-1)