n = int(input()) a = list(map(int, input().split())) s = [0] * n s[a[0] - 1] = 1 for i in range(n - 1): if a[i + 1] != a[i]: s[a[i + 1] - 1] += 1 if a[0] == a[-1]: if n - s.count(0) - s.count(1) > 1 or n - s.count(0) - s.count(1) - s.count(2) > 0: print(-1) elif n - s.count(0) - s.count(1) == 1: print(1) else: print(0) else: if n - s.count(0) - s.count(1) > 0: print(-1) else: print(0)