''' from math import gcd from itertools import accumulate, combinations, permutations, product from bisect import bisect, bisect_left from heapq import heappush, heappop from functools import reduce from operator import add, mul, sub, truediv, floordiv, mod from decimal import * getcontext().prec = 500 import string ascii = string.ascii_lowercase ''' from collections import deque from sys import stdin def main(): input = lambda: stdin.readline()[:-1] N = int(input()) A = tuple(map(int, input().split())) a1 = [A[0]] for i in range(1, N): if a1[-1] != A[i]: a1.append(A[i]) if len(a1) == 1: print(0) return a2 = deque(a1) for _ in [0] * len(a1): n = a2.popleft() if n != a2[-1]: a2.append(n) if len(set(a1)) != len(a2): print(-1) else: print(len(a1) - len(a2)) main()