n = int(input()) a = list(map(int, input().split())) idx = 0 while idx < n and a[idx] == a[-1]: a.append(a[0]) idx += 1 s = set([a[idx]]) for i in range(idx, n - 1 + idx): if a[i] != a[i + 1]: if a[i + 1] in s: print(-1) break else: if idx == 0 or idx == n: print(0) else: print(1)