n = int(input()) A = list(map(int, input().split())) if len(set(A)) == 1: print(0) exit() from collections import deque B = deque(A) temp = A[0] flag = False while True: x = B.pop() if x != temp: B.append(x) break else: B.appendleft(x) flag = True if flag: ans = 1 else: ans = 0 S = set() C = list(B) temp = C[0] for i in range(n): if C[i] != temp: if C[i] in S: print(-1) exit() else: S.add(temp) temp = C[i] else: print(ans)