input() A=input().split() #A=list(map(int,input().split())) index=[ [] for x in range(len(A)) ] index={"dummy":[]} for i in range(len(A)): index[A[i]] = index[A[i]]+[i] if A[i] in index.keys() else [i] gaps=0 for k in index.keys(): prev=None for j in index[k]: if prev!=None: if j-prev != 1: gaps=gaps+1 prev=j if gaps >1: print(-1) elif gaps==1 and A[0] != A[-1]: print(-1) elif gaps==1 and A[0] == A[-1]: print(1) elif gaps==0: print(0)