#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<functional>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define MOD 1000000007
#define f(i,n) for(int i=0;i<int(n);i++)
#define N 400000

vector<int>e[N];
bool used[N];
int s;

void dfs(int k){
used[k]=true;
s++;
f(i,e[k].size()){
if(!used[e[k][i]]){
dfs(e[k][i]);
}
}
return;
}




int main(){
map<int,int>m;
set<int>st;
	vector<int>a;
vector<int>b;
	vector<int>c;
vector<int>d;
	int n, k;
	int x, y, z;
	int ans;
	bool v = true;
	ans = 0;
	scanf("%d", &n);
y=0;
k=0;
	f(i,n){
		scanf("%d", &x);
if((st.insert(x)).second){
m[x]=k;
k++;
}
y=y^x;
		a.push_back(x);
		c.push_back(x);
	}
if((st.insert(y)).second){
m[y]=k;
k++;
}
		a.push_back(y);
		c.push_back(y);
z=y;
y=0;
	f(i,n){
		scanf("%d", &x);
		b.push_back(x);
		d.push_back(x);
if(a[i]!=b[i])ans++;
y=y^x;
	}
		b.push_back(y);
		d.push_back(y);
sort(a.begin(),a.end());
sort(b.begin(),b.end());

	f(i,n+1){
if(a[i]!=b[i])v=false;
	}
if(!v){
printf("-1\n");
return 0;
}

f(i,n+1){
e[m[c[i]]].push_back(m[d[i]]);
e[m[d[i]]].push_back(m[c[i]]);
}
f(i,k)used[i]=false;


f(i,k){
if(!used[i]){
s=0;
v=used[m[z]];
dfs(i);
if(s>1){
if(v==used[m[z]])ans++;
}
}
}



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


	return 0;
}