def RunLengthEncoding(S): res = [] N = len(S) i = 0 while i < N: cnt = 1 while i < N - 1 and S[i] == S[i + 1]: cnt += 1 i += 1 res.append((S[i], cnt)) i += 1 return res def main(): N = int(input()) A = list(map(int, input().split())) rle = RunLengthEncoding(A) if len(rle) == 1: return 0 ans = 0 if rle[0][0] == rle[-1][0]: rle.pop() ans = 1 memo = set() for color, _ in rle: if color in memo: return -1 memo.add(color) return ans print(main())