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

#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.2")

#define all(x) (x).begin(),(x).end()

int inp(){
    int x;
    cin >> x;
    return x;
}

int get_compos(const int &key, const vector<int> &comp){
    return lower_bound(all(comp), key) - comp.begin();
}

void compress(vector<int> &comp){
    sort(all(comp));
    comp.resize(unique(all(comp)) - comp.begin());
}

void opting(){
    ios::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
}

const int maxn = 2e5 + 69, delta = 1e9 + 7;

vector<int> comp, A, B;
unordered_map<int, int> cnt;
int n;

void input(){
    cin >> n;

    int x1 = 0;
    for(int i = 0; i < n; i++){
        int tmp = inp();
        x1 ^= tmp;
        cnt[tmp] ++;
        A.push_back(tmp);

        comp.push_back(tmp);
    }
    cnt[x1]++;
    A.push_back(x1);

    comp.push_back(x1);

    int x2 = 0;
    for(int i = 0; i < n; i++){
        int tmp = inp();
        x2 ^= tmp;
        cnt[tmp]--;
        B.push_back(tmp);

    }
    cnt[x2]--;
    B.push_back(x2);

    for(auto e : cnt)
        if(e.second != 0){
            cout << -1;
            exit(0);
        }
}

int E = 0;
bool inside[maxn];
vector<int> adj[maxn];
void add_edge(int u, int v){
    adj[u].push_back(v);
    E++;

    inside[u] = inside[v] = inside[A[n]] = inside[B[n]] = true;
}

bool mark[maxn];
void dfs(int v){
    mark[v] = true;
    for(auto u : adj[v])
        if(!mark[u])
            dfs(u);
}
void solve(){
    compress(comp);

    for(int i = 0; i <= n; i++){
        A[i] = get_compos(A[i], comp);
        B[i] = get_compos(B[i], comp);

    for(int i = 0; i <= n; i++)
        if(A[i] != B[i])
            add_edge(A[i], B[i]);

    int comp_cnt = 0;
    for(int i = 0; i <= n; i++)
        if(!mark[i] and inside[i])
            comp_cnt++, dfs(i);

    if(comp_cnt == 0){
        cout << 0;
        return;
    }
    if(comp_cnt == 1)
        cout << E - 1;
    else
        cout << E + comp_cnt - 1;
}


int main(){
    opting();

    input();
    solve();

}
