N = int(input()) lsA = list(map(int,input().split())) used = set() st = lsA[0] f = True for i in range(1,N): if st == lsA[i]: continue else: if lsA[i] in used: f = False used.add(st) st = lsA[i] if f: print(0) exit() en = lsA[-1] ll = [] while lsA and lsA[-1] == en: ll.append(lsA.pop()) lsB = ll + lsA used = set() st = lsB[0] for i in range(1,N): if st == lsB[i]: continue else: if lsB[i] in used: print(-1) exit() used.add(st) st = lsB[i] print(1)