fn main() { let mut n = String::new(); std::io::stdin().read_line(&mut n).ok(); let n: usize = n.trim().parse().unwrap(); let mut a = String::new(); std::io::stdin().read_line(&mut a).ok(); let a: Vec = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut colors: Vec> = vec![vec![]; n+1]; let mut prev_color: usize = n+1; a.iter().enumerate().for_each(|pair| { if prev_color == *pair.1 { return; } colors[*pair.1].push(pair.0); prev_color = *pair.1; }); let mut result: usize = 0; for i in 1..=n { let v = &colors[i]; if v.len() > 2 { println!("-1"); return; } else if v.len() == 2 { if v[0] == 0 && i == prev_color { result += 1; } else { println!("-1"); return; } } } println!("{}", result); }