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 prev = a if any(v >= 3 for v in d.values()): print(-1) else: t = len([v for v in d.values() if v >= 2]) if t >= 2: print(-1) elif t == 1: if A[0] == A[-1]: print(1) else: print(-1) else: print(0)