from collections import defaultdict, deque d = defaultdict(deque) N = int(input()) A = list(map(int, input().split())) A += A l, r = 0, 0 while l < N: while r < N+l: if (len(d[A[r]])>0 and d[A[r]][-1] == r-1) or len(d[A[r]])==0: d[A[r]].append(r) else: for i in range(l, r): d[A[i]].popleft() l = r if l >= N: break r -= 1 r += 1 if r-l == N: if l == 0: print(0) exit() else: print(1) exit() print(-1)