#include<bits/stdc++.h>
using namespace std;

int N;
int Xn;
int Xor;
vector<int> A, B, X;
map<int, int> cnt;
map<int, int> dx;
vector<vector<int> > adj;
vector<int> U, V;
vector<vector<int> > av, bv;
vector<int> tin, bck, id;
stack<int> st;
int timer = 0, bcnt = 0;

void dfs(int u, int fe) {
    tin[u] = timer++;
    bck[u] = tin[u];
    st.push(u);
    for(int i = 0; i < adj[u].size(); i++) {
        int e = adj[u][i];
        int v = U[e] + V[e] - u;
        if(e == fe) continue;
        if(tin[v] == -1) {
            dfs(v, e);
            bck[u] = min(bck[u], bck[v]);
        }
        else bck[u] = min(bck[u], tin[v]);
    }
    if(bck[u] == tin[u]) {
        while(1) {
            int t = st.top(); st.pop();
            id[t] = bcnt;
            if(t == u) break;
        }
        bcnt++;
    }
}

int main() {
    scanf("%d", &N);
    A.resize(N);
    Xor = 0;
    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        cnt[ A[i] ]++;
        Xor ^= A[i];
    }
    A.push_back(Xor);
    cnt[Xor]++;
    B.resize(N);
    Xor = 0;
    for(int i = 0; i < N; i++) {
        scanf("%d", &B[i]);
        if(!cnt[ B[i] ]) {
            printf("-1");
            return 0;
        }
        cnt[ B[i] ]--;
        Xor ^= B[i];
    }
    if(!cnt[Xor]) {
        printf("-1");
        return 0;
    }
    B.push_back(Xor);
    N++;

    for(int i = 0; i < A.size(); i++) X.push_back(A[i]);
    sort(X.begin(), X.end());
    X.resize(unique(X.begin(), X.end()) - X.begin());
    Xn = X.size();
    for(int i = 0; i < Xn; i++) dx[ X[i] ] = i;
    for(int i = 0; i < N; i++) {
        A[i] = dx[ A[i] ];
        B[i] = dx[ B[i] ];

    }
    vector<bool> chk(N, false);
    for(int i = 0; i < N - 1; i++) {
        if(A[i] == B[i]) {
            chk[i] = 1;
        }
    }
    av.resize(Xn);
    bv.resize(Xn);
    for(int i = 0; i < N; i++) {
        if(chk[i]) continue;
        av[ A[i] ].push_back(i);
        bv[ B[i] ].push_back(i);
    }
    int E = 0;
    adj.resize(N);
    for(int i = 0; i < Xn; i++) {
        for(int j = 0; j < (int)av[i].size() - 1; j++) {
            int u = av[i][j];
            int v = av[i][j + 1];
            adj[u].push_back(E);
            adj[v].push_back(E);
            U.push_back(u);
            V.push_back(v);
            E++;
            adj[u].push_back(E);
            adj[v].push_back(E);
            U.push_back(u);
            V.push_back(v);
            E++;
        }
        for(int j = 0; j < (int)bv[i].size() - 1; j++) {
            int u = bv[i][j];
            int v = bv[i][j + 1];
            adj[u].push_back(E);
            adj[v].push_back(E);
            U.push_back(u);
            V.push_back(v);
            E++;
            adj[u].push_back(E);
            adj[v].push_back(E);
            U.push_back(u);
            V.push_back(v);
            E++;
        }
    }
    for(int i = 0; i < N; i++) {
        if(chk[i]) continue;
        adj[ av[ A[i] ][0] ].push_back(E);
        adj[ bv[ A[i] ][0] ].push_back(E);
        U.push_back(av[ A[i] ][0]);
        V.push_back(bv[ A[i] ][0]);
        E++;
    }
    tin = bck = id = vector<int>(N, -1);
    for(int i = 0; i < N; i++) if(!chk[i] && tin[i] == -1) dfs(i, -1);
/*
    for(int i = 0; i < N; i++) {
        cout << id[i] << ' ';
    }
    cout << endl;
//*/
    int ans = 0;
    int cnt = 0;
    for(int i = 0; i < N; i++) {
        if(chk[i]) continue;
        if(id[i] == id[N - 1]) {
            cnt++;
        }
    }
    ans += cnt - 1;

    for(int i = 0; i < N; i++) {
        if(chk[i]) continue;
        if(id[i] != id[N - 1]) ans++;
    }
    ans += bcnt - 1;

    printf("%d", ans);
}


Measured memory_bytes: 34296