N = int(input()) A = list(map(int,input().split())) dic = {} import sys for i in range(N): if A[i] != A[i-1]: if A[i] in dic: print (-1) sys.exit() dic[A[i]] = 1 if len(dic) <= 1: print (0) elif A[0] == A[-1]: print (1) else: print (0)