// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long

#define fr(i,x,y) for(int i=x;i<=y;i++)

#define rf(i,x,y) for(int i=x;i>=y;i--)

using namespace std;

const int N=100002;

int n;

struct data{

	int x,y;

}a[N];

map<int,int> mp;

int f[N],sz[N];

vector<int> w[N];

int b[N];



void read(int &x){

	char ch=getchar();x=0;

	for(;ch<'0'||ch>'9';ch=getchar());

	for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';

}



int gf(int x){

	return f[x]==x?x:f[x]=gf(f[x]);

}



void unio(int x,int y){

	int fx=gf(x),fy=gf(y);

	if (fx!=fy){

		f[fx]=fy;

		sz[fy]+=sz[fx];

	}

}



int main(){

	read(n);

	int xr=0;

	fr(i,1,n) read(a[i].x),xr^=a[i].x,mp[a[i].x]++;

	a[n+1].x=xr;mp[xr]++;

	int tot=0;

	/*for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)

	 it->second=++tot;*/

	int flag=0;

	fr(i,1,n){

		read(a[i].y);

		if (!mp.count(a[i].y)){

			printf("-1\n");

			return 0;

		}

		mp[a[i].y]--;

		if (mp[a[i].y]==0) mp.erase(a[i].y);

		if (a[i].y==xr&&a[i].x!=a[i].y) flag=1;

	}

	a[n+1].y=mp.begin()->first;

	

	//sort(a+1,a+1+n,cmp)

	mp.clear();

	fr(i,1,n+1) if (a[i].x!=a[i].y) mp[a[i].x]=1,mp[a[i].y]=1;

	tot=0;

	for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)

	 it->second=++tot;

	fr(i,1,n+1) if (a[i].x!=a[i].y) a[i].x=mp[a[i].x],a[i].y=mp[a[i].y];

	//fr(i,1,n+1) if (a[i].x!=a[i].y) w[a[i].y].push_back(i);

	fr(i,1,tot) f[i]=i;

	fr(i,1,n+1) if (a[i].x!=a[i].y) sz[a[i].x]++;

	fr(i,1,n+1)

	 if (a[i].x!=a[i].y){

	 	//b[a[i].x]=1;

		unio(a[i].x,a[i].y);

	 } 

	int ans=0;

	fr(i,1,tot) if (gf(i)==i) ans+=sz[i]+1;

	if (ans==0){

		printf("0\n");

		return 0;

	}

	//cout<<ans<<endl;

	if (flag) ans-=2;

	if (flag&&a[n+1].x==a[n+1].y) ans++;

	cout<<ans<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.444633568049335x):
#include<bits/stdc++.h>

using namespace std;



const int N = 1000 * 100 + 10;

int a[N], b[N], n, par[N * 2], sz[N * 2];



int get_par(int root) {

	return par[root] == root? root: par[root] = get_par(par[root]);

}



void Union(int v, int u) {

	v = get_par(v), u = get_par(u);

	if (v == u)

		return;

	if (sz[u] > sz[v])

		swap(v, u);

	sz[v] += sz[u];

	par[u] = v;

}



int main() { 

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

	int ans = 0;

	cin >> n;

	for (int i = 0; i < n; i++)

		cin >> a[i], a[n] ^= a[i];

	for (int i = 0; i < n; i++)

		cin >> b[i], b[n] ^= b[i];

	map<int, int> cnt;

	for (int i = 0; i <= n; i++)

		cnt[a[i]]++;

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

		cnt[b[i]]--;

		if (cnt[b[i]] < 0)

			return cout << "-1\n", 0;

	}

	vector<int> vec;

	for (int i = 0; i < n; i++)

		if (a[i] != b[i])

			vec.push_back(a[i]), vec.push_back(b[i]), ans++;

	vec.push_back(a[n]), vec.push_back(b[n]);

	sort(vec.begin(), vec.end());

	vec.resize(unique(vec.begin(), vec.end()) - vec.begin());

	int s = (int)(vec.size());

	for (int i = 0; i < s; i++)

		par[i] = i, sz[i] = 1;

	for (int i = 0; i <= n; i++) 

		if (a[i] != b[i]) {

			a[i] = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin();

			b[i] = lower_bound(vec.begin(), vec.end(), b[i]) - vec.begin();

			Union(a[i], b[i]);

		}

	for (int i = 0; i < s; i++)

		if (get_par(i) == i)

			ans++;

	cout << ans - 1 << '\n';

	return 0;

}