#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;


class UnionFind{
public:
    vector<int> par;
    vector<int> rank;
    UnionFind(int n){
        init(n);
    }
    void init(int n){
        par.resize(n);
        for(int i=0;i<n;i++){
            par[i] = i;
        }
        rank.assign(n, 0);
    }
    int find(int x){
        if(par[x] == x){
            return x;
        }else{
            return par[x] = find(par[x]);
        }
    }
    void unite(int x,int y){
        x = find(x);
        y = find(y);
        if(x == y) return;

        if(rank[x] < rank[y]){
            par[x] = y;
        }else{
            par[y] = x;
            if(rank[x] == rank[y]) rank[x]++;
        }
    }
    bool same(int x,int y){
        return find(x)==find(y);
    }
};

int main(){
    int N;
    cin >> N;
    
    vector<int> a(N+1, 0);
    for(int i=0;i<N;i++){
        cin >> a[i];
        a[N] ^= a[i];
    }

    vector<int> b(N+1, 0);
    for(int i=0;i<N;i++){
        cin >> b[i];
        b[N] ^= b[i];
    }

    if(multiset<int>(a.begin(), a.end()) != multiset<int>(b.begin(), b.end())){
        cout << -1 << endl;
        return 0;
    }

    map<int, int> v2i;
    map<int, int> i2v;
    int idx = 0;
    for(int i=0;i<N;i++){
        if(v2i.find(a[i]) == v2i.end()){
            i2v[idx] = a[i];
            v2i[a[i]] = idx++;
        }
    }

    map<int, vector<int>> v2posb;
    for(int i=0;i<N+1;i++){
        v2posb[b[i]].push_back(i);
    }

    int n = v2i.size();
    UnionFind uf(n);
    int ans = 0;
    for(int i=0;i<N+1;i++){
        if(a[i] == b[i]) continue;
        if(i < N) ans++;
        for(int posb : v2posb[a[i]]){
            // cout << a[i] << " " << b[posb] << " " << v2i[a[i]] << " " << v2i[a[posb]] << endl;
            uf.unite(v2i[a[i]], v2i[a[posb]]);
        }
    }

    for(int i=0;i<n;i++){
        if(b[i] != i2v[i] && uf.same(i, v2i[b[i]])){
            ans--;
            break;
        }
    }

    set<int> s;
    for(int i=0;i<n;i++){
        int par = uf.find(i);
        if(par != i){
            s.insert(par);
        }
    }

    cout << ans + s.size() << endl;

    return 0;
}