//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // fn calc(a: &mut [T]) -> usize { if a.len() <= 1 { return 0; } let m = a.len() / 2; let mut ans = calc(&mut a[..m]) + calc(&mut a[m..]); let mut i = 0; let mut j = m; let mut buf = vec![]; while i < m && j < a.len() { if a[i] <= a[j] { buf.push(a[i]); i += 1; } else { ans += m - i; buf.push(a[j]); j += 1; } } while i < m { buf.push(a[i]); i += 1; } for (a, buf) in a.iter_mut().zip(buf.iter()) { *a = *buf; } ans } fn run() { input! { n: usize, a: [usize; n], b: [usize; n], } let p = a.windows(2).map(|a| a[0] ^ a[1]).collect::>(); let q = b.windows(2).map(|a| a[0] ^ a[1]).collect::>(); let mut x = p.clone(); let mut y = q.clone(); x.sort(); y.sort(); if !(a[0] == b[0] && a[n - 1] == b[n - 1] && x == y) { println!("-1"); return; } y.dedup(); assert!(y.len() == n - 1); let mut index = vec![vec![]; y.len()]; for (i, &q) in q.iter().enumerate() { let k = y.binary_search(&q).unwrap(); index[k].push(i); } index.iter_mut().for_each(|p| p.reverse()); let mut x = vec![]; for &p in p.iter() { let k = y.binary_search(&p).unwrap(); x.push(index[k].pop()); } let ans = calc(&mut x); println!("{}", ans); } fn main() { run(); }