N = int(input()) A = list(map(int,input().split())) X = [0] * (N + 1) start = A[0] for i in range(N - 1): if A[i] != A[i+1]: if X[A[i]] == 1: print(-1) exit() else: X[A[i]] = 1 if X[A[N - 1]] == 0: print(0) else: if A[0] == A[N-1]: print(1) else: print(-1)