import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); ArrayDeque deq = new ArrayDeque<>(); HashMap> positions = new HashMap<>(); int idx = 0; for (int i = 0; i < n; i++) { int x = sc.nextInt(); if (deq.size() == 0 || deq.peekLast() != x) { if (!positions.containsKey(x)) { positions.put(x, new ArrayList<>()); } positions.get(x).add(idx); deq.add(x); idx++; } } int count = 0; for (ArrayList list : positions.values()) { if (list.size() <= 1) { continue; } if (list.size() == 2 && list.get(0) == 0 && list.get(1) == idx - 1) { count++; continue; } System.out.println(-1); return; } System.out.println(count); } }