input() A=list(map(int,input().split())) index=[] prev=A[0] for i in range(len(A)): if prev !=A[i]: if A[i] in index: print(-1) exit() else: prev=A[i] index.append(A[i]) if A[0] != A[-1]: print(0) else: print(1) exit()