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<i32> = 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<i32> = vec![-1; n];

    for i in 0..n {
        let j: usize = a[i] as usize;
        if b[j] >= 0 {
            if a[i] == a[i - 1] { continue; }

            if b[j] == 0 {
                ans = 1;
            } else {
                println!("-1");
                return;
            }
        }
        else if ans == 1 {
            println!("-1");
            return;
        }
        b[j] = i as i32;
    }

    println!("{}", ans);
}