n = int(input())
ai = [int(i) for i in input().split()]

fst = ai[0]

ima = ai[0]

li = [0]*(n+1)

flag = False

for i in range(n-1):
    #print(flag)
    if flag == True:
        if ima != ai[i+1]:
            print(-1)
            exit()
    if ima == ai[i+1]:
        continue
    #print(fst==ai[i+1])
    if fst == ai[i+1]:
        flag = True
    if li[ai[i+1]] == 0:
        li[ai[i+1]] = 1
        ima = ai[i+1]
    elif li[ai[i+1]] == 1:
        print(-1)
        exit()


if fst == ai[n-1]:
    print(min(1,sum(li)))
else:
    print(0)