use proconio::input; fn main() { input! { n:usize, a:[usize;n], } let mut seen = vec![false; n + 1]; let mut prev = 0; let mut ok = true; for i in 0..n { if prev != a[i] && seen[a[i]] { ok = false; break; } seen[a[i]] = true; prev = a[i]; } if ok { println!("0"); return; } let mut poss = vec![vec![]; n + 1]; for i in 0..n { poss[a[i]].push(i as i64); } let mut ok = true; for i in 0..=n { if poss[i].len() <= 1 { continue; } if !f(n, poss[i].len(), &poss[i]) { ok = false; } } let ans = if ok { 1 } else { -1 }; println!("{}", ans); } fn f(m: usize, n: usize, a: &Vec) -> bool { for i in 0..n { let l = (a[i] - a[(i as i64 - 1).rem_euclid(n as i64) as usize]).rem_euclid(m as i64); let r = (a[(i + 1) % n] - a[i]).rem_euclid(m as i64); if l != 1 && r != 1 { return false; } } true }