def deg(A): """ 連続して続く文字を圧縮する(※Counterではない) [1,1,1,2,2,2,3] -> [(1,3),(2,3),(3,1)] """ res=[] a0, cnt=A[0], 1 for a in A[1:]: if a!=a0: res.append(a0); cnt=1 else: cnt+=1 a0=a res.append(a0) return res from collections import Counter N=int(input()) A=list(map(int, input().split())) B=deg(A) c=Counter(B) for a in B[1:-1]: if c[a]>=2: print(-1) exit() if A[0]==A[-1] and len(c)>=2: print(1) else: print(0)