Language: C++

// use std::ops::{Index, IndexMut};
// use std::cmp::{Ordering, min, max};
// use std::collections::{BinaryHeap, BTreeMap};
// use std::collections::btree_map::Entry::{Occupied, Vacant};
// use std::clone::Clone;

fn getline() -> String{
    let mut res = String::new();
    std::io::stdin().read_line(&mut res).ok();
    res
}

macro_rules! readl {
    ($t: ty) => {
        {
            let s = getline();
            s.trim().parse::<$t>().unwrap()
        }
    };
    ($( $t: ty),+ ) => {
        {
            let s = getline();
            let mut iter = s.trim().split(' ');
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*) 
        }
    };
}

macro_rules! readlvec {
    ($t: ty) => {
        {
            let s = getline();
            let iter = s.trim().split(' ');
            iter.map(|x| x.parse().unwrap()).collect::<Vec<$t>>()
        }
    }
}

macro_rules! mvec {
    ($v: expr, $s: expr) => {
        vec![$v; $s]
    };
    ($v: expr, $s: expr, $($t: expr),*) => {
        vec![mvec!($v, $($t),*); $s]
    };
}

macro_rules! debug {
    ($x: expr) => {
        println!("{}: {:?}", stringify!($x), $x)
    }
}

fn printiter<'a, T>(v: &'a T)
where
    &'a T: std::iter::IntoIterator, 
    <&'a T as std::iter::IntoIterator>::Item: std::fmt::Display {
    for (i,e) in v.into_iter().enumerate() {
        if i != 0 {
            print!(" ");
        }
        print!("{}", e);
    }
    println!("");
}

struct UnionFind {
    par: Vec<usize>,
    rank: Vec<usize>,
    cnt: Vec<u32>,
}

impl UnionFind {
    fn new(n: usize) -> UnionFind {
        let mut vec = vec![0;n];
        for i in 0..n {
            vec[i] = i;
        }
        UnionFind {
            par : vec,
            rank : vec![0;n],
            cnt : vec![0;n],
        }
    }

    fn find(&mut self, x: usize) -> usize {
        if x == self.par[x] {
            x
        }else{
            let par = self.par[x];
            let res = self.find(par);
            self.par[x] = res;
            res
        }
    }

    fn same(&mut self, a: usize, b: usize) -> bool {
        self.find(a) == self.find(b)
    }

    fn unite(&mut self, a: usize, b: usize){
        let apar = self.find(a);
        let bpar = self.find(b);
        if apar == bpar {
            return;
        }
        if self.rank[apar] > self.rank[bpar] {
            self.par[bpar] = apar;
            self.cnt[apar] += self.cnt[bpar];
        }else{
            self.par[apar] = bpar;
            self.cnt[bpar] += self.cnt[apar];
            if self.rank[apar] == self.rank[bpar] {
                self.rank[bpar] += 1;
            }
        }
    }

    fn count(&mut self, v: usize) -> u32 {
        let par = self.find(v);
        self.cnt[par]
    }

    fn add(&mut self, v: usize, x: u32) {
        let par = self.find(v);
        self.cnt[par] += 1;
    }
}

fn main() {
    let n = readl!(usize);
    let a = readlvec!(i32);
    let b = readlvec!(i32);
    let axor = a.iter().fold(0, |sum, &x| sum^x);
    let mut acnt = std::collections::HashMap::new();
    for &e in &a {
        let x = acnt.entry(e).or_insert(0);
        *x += 1;
    }
    {
        let x = acnt.entry(axor).or_insert(0);
        *x += 1;
    }
    let mut ex = true;
    let mut usea = false;
    // let mut cnt = 0;
    let mut deg = vec![0; n];
    // debug!(acnt);
    let mut ff = true;
    for i in 0..n {
        if a[i] != b[i] {
            ff = false;
        }
        
        {
            // cnt += \1;
            if axor == b[i] && !usea {
                usea = true;
            }
            let x = acnt.entry(b[i]).or_insert(0);
            if *x == 0 {
                ex = false;
                break;
            }
            *x -= 1;
        }
    }
    // debug!(acnt);
    if !ex {
        println!("-1");
        return;
    } else if ff {
        println!("0");
        return;
    }
    
    use std::collections::HashSet;
    let mut za = a.clone();
    za.push(axor);
    za.sort();
    za.dedup();
    // debug!(za);
    let mut uf = UnionFind::new(za.len());
    for (&a, &b) in a.iter().zip(b.iter()) {
        if a == b {
            continue;
        }
        let ap = za.binary_search(&a).unwrap();
        let bp = za.binary_search(&b).unwrap();
        // debug!(ap); debug!(bp);
        uf.unite(ap, bp);
        uf.add(ap, 1);
    }
    uf.add(za.binary_search(&axor).unwrap(), 1);
    // debug!(uf.cnt);

    let mut used = vec![false; za.len()];
    let mut ans = 0;
    for i in 0..za.len() {
        let par = uf.find(i);
        // debug!(par);
        if !used[par] {
            used[par] = true;
            let num = uf.count(i);
            // debug!(num);
            if num > 1 {
                ans += num+1;
            }
        }
        // debug!(ans);
    }
    if usea {
        println!("{}", ans-2);
    } else {
        println!("{}", ans);
    }
}



Compiler output:
a.cc:54:14: warning: multi-character literal with 10 characters exceeds 'int' size of 4 bytes
   54 | fn printiter<'a, T>(v: &'a T)
      |              ^~~~~~~~~~~~~
a.cc:56:6: warning: missing terminating ' character
   56 |     &'a T: std::iter::IntoIterator,
      |      ^
a.cc:56:6: error: missing terminating ' character
   56 |     &'a T: std::iter::IntoIterator,
      |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
a.cc:57:7: warning: missing terminating ' character
   57 |     <&'a T as std::iter::IntoIterator>::Item: std::fmt::Display {
      |       ^
a.cc:57:7: error: missing terminating ' character
   57 |     <&'a T as std::iter::IntoIterator>::Item: std::fmt::Display {
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:76:18: error: too many decimal points in number
   76 |         for i in 0..n {
      |                  ^~~~
a.cc:150:14: error: too many decimal points in number
  150 |     for i in 0..n {
      |              ^~~~
a.cc:199:14: error: too many decimal points in number
  199 |     for i in 0..za.len() {
      |              ^~~~~~~~~
a.cc:7:1: error: 'fn' does not name a type
    7 | fn getline() -> String{
      | ^~
a.cc:13:1: error: 'macro_rules' does not name a type
   13 | macro_rules! readl {
      | ^~~~~~~~~~~
a.cc:29:1: error: 'macro_rules' does not name a type
   29 | macro_rules! readlvec {
      | ^~~~~~~~~~~
a.cc:39:1: error: 'macro_rules' does not name a type
   39 | macro_rules! mvec {
      | ^~~~~~~~~~~
a.cc:48:1: error: 'macro_rules' does not name a type
   48 | macro_rules! debug {
      | ^~~~~~~~~~~
a.cc:54:1: error: 'fn' does not name a type
   54 | fn printiter<'a, T>(v: &'a T)
      | ^~
a.cc:64:5: error: 'println' does not name a type
   64 |     println!("");
      |     ^~~~~~~
a.cc:65:1: error: expected declaration before '}' token
   65 | }
      | ^
a.cc:68:5: error: 'par' does not name a type
   68 |     par: Vec<usize>,
      |     ^~~
a.cc:73:6: error: expected initializer before 'UnionFind'
   73 | impl UnionFind {
      |      ^~~~~~~~~
a.cc:130:1: error: 'fn' does not name a type
  130 | fn main() {
      | ^~