import sys N = int(input()) A = list(map(int,input().split())) ans = 0 while A and A[-1] == A[0]: A.pop() if not A: print(0) exit() ans = 1 s =set() left = 0 while left < len(A): if A[left] not in s: s.add(A[left]) right = left while right < len(A) and A[right] == A[left]: right += 1 left = right else: print(-1) exit()