# coding: utf-8 # Your code here! import sys read = sys.stdin.read readline = sys.stdin.readline n,*a = list(map(int,read().split())) res = [0]*(10**5+3) now = 10**6 two = 0 for i in a: if i != now: res[i] += 1 if res[i]==2: two += 1 now = i if two >= 2: print(-1) elif two == 1: if a[0] == a[-1] == res.index(2): print(1) else: print(-1) elif two==0: print(0) #print(res[:5],two)