N = int(input()) A = list(map(int, input().split())) d = {} prev = -1 for a in A: if prev != a: d.setdefault(a, 0) d[a] += 1 t = len([i for i in d.values() if i >= 2]) if t >= 2: print(-1) elif t == 1: if A[0] == A[-1]: print(1) else: print(-1) else: print(0)