n = int(input()) ai = [int(i) for i in input().split()] fst = ai[0] ima = ai[0] li = [0]*(n+1) for i in range(n-1): if li[ai[i+1]] == 0: li[ai[i+1]] = 1 ima = ai[i+1] elif ima == ai[i+1]: continue elif li[ai[i+1]] == 1: print(-1) exit() if fst == ai[n-1]: print(1) else: print(0)