// Slower C++ solution:
#include<cstdio>

#include<algorithm>

#include<map>

#include<vector>

using namespace std;



const int MAXN=1e5+5;



int n,ncnt,tot,ans;

int a[MAXN],b[MAXN];

int in[MAXN],out[MAXN];

bool tag[MAXN];

map<int,int> vis;

vector<int> E[MAXN];



void dfs(int u){

    tag[u]=1;

    for(int i=0;i<(int)E[u].size();i++){

        int v=E[u][i];

        if(tag[v]) continue;

        dfs(v);

    }

}



int main(){

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d",&a[i]);

        tot^=a[i];

        if(vis.find(a[i])==vis.end()) vis[a[i]]=++ncnt;

        a[i]=vis[a[i]];

    }

    if(vis.find(tot)==vis.end()) vis[tot]=++ncnt;

    tot=vis[tot];

    for(int i=1;i<=n;i++){

        scanf("%d",&b[i]);

        if(vis.find(b[i])==vis.end()) vis[b[i]]=++ncnt;

        b[i]=vis[b[i]];

    }

    for(int i=1;i<=n;i++){

        if(a[i]==b[i]) continue;

        ans++;

        E[b[i]].push_back(a[i]);

        in[a[i]]++,out[b[i]]++;

    }

    bool flag=0;

    for(int i=1;i<=ncnt;i++){

        if(in[i]==out[i]) continue;

        if(in[i]<out[i]){

            if(in[i]!=out[i]-1||i!=tot)

                return puts("-1"),0;

        }

        else{

            if(in[i]!=out[i]+1||flag) return puts("-1"),0;

            flag=1;

        }

    }

    dfs(tot);

    for(int i=1;i<=ncnt;i++){

        if(tag[i]||!out[i]) continue;

        ans++;

        dfs(i);

    }

    printf("%d\n",ans);

}


// Faster C++ solution for the same problem (measured speedup: 3.38529436507618x):
/* be name khoda */

#include <bits/stdc++.h>

using namespace std;



typedef int ll;

typedef pair<ll, ll> pii;



#define forifrom(i, s, n) for (ll i = s; i < n; ++i)

#define fori(i, n) forifrom(i, 0, n)

#define forirto(i, n, e) for (ll i = (n) - 1; i >= e; --i)

#define forir(i, n) forirto(i, n, 0)



#define F first

#define S second



// ------------------------------------------------------------------



const ll maxn = 100010;

const ll maxa = 1 << 30;



ll n, A[maxn], B[maxn];

map<ll, ll> din, dout, vis, vis2;

map<ll, vector<ll>> g;



void add(ll a, ll b) {

    g[a].emplace_back(b);

    g[b].emplace_back(a);

    din[b]++, dout[a]++;

}



void dfs(ll x) {

    vis[x] = 1;

    for (auto y : g[x]) if (!vis[y]) dfs(y);

}



void halt() {

    cout << "-1\n";

    exit(0);

}



int main() {

    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);



    cin >> n;

    ll xr = 0;

    fori (i, n) cin >> A[i], xr ^= A[i];

    fori (i, n) cin >> B[i];

    ll diff = 0;

    fori (i, n) {

        if (A[i] != B[i]) {

            add(A[i], B[i]);

            ++diff;

        }

    }

    if (diff == 0) return cout << "0\n", 0;

    ll that = -1;

    fori (id, maxn * 2) {

        ll i = id < n ? A[id] : B[id - n];

        if (vis2[i]) continue;

        vis2[i] = 1;

        if (din[i] - dout[i] >= 2) halt();

        else if (din[i] - dout[i] == 1) {

            if (that != -1 || i != xr) halt();

            else that = i;

        }

    }

    ll comp = 0;

    fori (id, maxn) {

        ll i = id < n ? A[id] : B[id - n];

        if (!vis[i] && !g[i].empty()) ++comp, dfs(i);

    }

    ll ans = diff + comp - 1;

    if (that == -1 && count(B, B + n, xr) == 0) ++ans;

    cout << ans << '\n';



}