use std::io::Read; fn main() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let n: usize = iter.next().unwrap().parse().unwrap(); let mut a: Vec = Vec::with_capacity(n); for _ in 0..n { let x: i32 = iter.next().unwrap().parse().unwrap(); a.push(x - 1); } let mut ans = 0; let mut b: Vec = vec![-1; n]; for (i, x) in a.iter().enumerate() { let j: usize = *x as usize; if b[j] >= 0 { if *x == a[i - 1] { continue; } if b[j] == 0 { ans = 1; } else { println!("-1"); return; } } b[j] = i as i32; } println!("{}", ans); }