// 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);
    }
}



Measured memory_bytes: 12276