n=int(input()) arr=list(map(int,input().split())) if n==1: print(0) exit() if arr.count(arr[0])==n: print(0) exit() if arr[0]==arr[-1]: s=set() s.add(arr[0]) for i in range(n): if arr[i]==arr[0]: continue else: arr=arr[i:] break for i in range(n): if arr[-(i+1)]==arr[-1]: continue else: arr=arr[:-i] break n=len(arr) arr.append(0) for i in range(n): if arr[i] in s: print(-1) break if arr[i]==arr[i+1]: continue else: s.add(arr[i]) else: print(1) else: s=set() arr.append(0) for i in range(n): if arr[i] in s: print(-1) break if arr[i]==arr[i+1]: continue else: s.add(arr[i]) else: print(0)