import sys sys.stdin.readline def I(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(map(int, input().split())) mod=10**9+7 def main(): N=I() A=LI() now=0 use=[0]*(N) for i in range(N): if now!=A[i]: use[A[i]-1]+=1 now=A[i] if max(use)<=1: print(0) else: if A[0]==A[-1]: use[A[0]-1]-=1 if max(use)<=1: print(1) else: print(-1) main()