import sys sys.setrecursionlimit(10**7) def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) N = I() A = [-1]+LI() flag = [0]*(N+1) if A[1] != A[-1]: color = -1 for i in range(1,N+1): a = A[i] if a != color: if flag[a]: print(-1) break else: color = a flag[a] = 1 else: print(0) else: if A == [-1]+[A[1]]*N: print(0) exit() color = A[1] for i in range(1,N+1): a = A[i] if a != color: if flag[a]: print(-1) break else: color = a flag[a] = 1 else: print(1)