#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<unordered_map>
#include<unordered_set>
#define lol(i,n) for(ll i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;

using namespace std;
int NOEL(){
    cout<<-1<<endl;
    return 0;
}
#define N 100010
ll n,a[N],b[N];
unordered_map<ll,ll> A,B;
bool Cand(){
    a[n]=0;
    lol(i,n)a[n]=a[n] xor a[i];
    lol(i,n+1)A[a[i]]++;
    lol(i,n)B[b[i]]++;
    b[n]=-1;
    lol(i,n+1){
	if(A[a[i]]!=B[a[i]]){
	    if(b[n]!=-1)return 0;
	    b[n]=a[i];
	    B[a[i]]++;
	}
    }
    return 1;
}
unordered_map<ll,vector<ll> > g;
unordered_set<ll> vis;
unordered_set<ll> cur;
void dfs(ll x){
    if(cur.find(x)!=cur.end())return;
    vis.insert(x);
    cur.insert(x);
    for(auto y:g[x])dfs(y);
}
int main(){
    cin>>n;
    lol(i,n)cin>>a[i];
    lol(i,n)cin>>b[i];
    if(!Cand())return NOEL();
    lol(i,n+1){
	if(a[i]!=b[i])g[a[i]].push_back(b[i]);
    }
    ll ans=-2+(a[n]==b[n]);
    lol(i,n+1){
	if(vis.find(a[i])!=vis.end())continue;
	cur.clear();
	dfs(a[i]);
	if(cur.size()==1&&a[i]==a[n])ans++;
	if(cur.size()!=1)ans++;
    }
    for(auto x:vis){
	ans+=g[x].size();
    }
    cout<<ans<<endl;
    return 0;
}
