from collections import Counter from itertools import groupby N = int(input()) A = list(map(int, input().split())) def solve(arr): c = Counter(arr) g = tuple(groupby(arr)) if len(c) == len(g): return True return False flag = 0 while A and A[-1] == A[0]: A.pop() flag = 1 if not A: flag = 0 if solve(A): print(int(flag)) else: print(-1)