n=int(input()) if n==1: print(0) exit() 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]) kouho.add(A[i]) pre=A[i] if not out: if len(out)==1 and A[0]==A[-1]: print(1) else: print(-1) else: print(0)