# problem 1 N = int(input()) K = list(map(int, input().split())) d = [] d.append(K[0]) for i in range(1, N): if K[i] == d[-1]: pass else: d.append(K[i]) ALL = len(d) # 施行は行われても一回までである if d[0] == d[-1]: if len(d) == 1: print(0) exit() else: del d[0] if len(d) == len(set(d)): print(1) else: print(-1) exit() else: if ALL == len(set(d)): print(0) else: print(-1) exit()