#include<bits/stdc++.h>
#define N 200005
using namespace std;
vector<int> G[N];
int n,m,a[N],b[N],c[N],d[N],w[N],lw,f[N],ans;
int find(int x){ return f[x] ? f[x]=find(f[x]) : x;}
int main()
{
	int i;
	scanf("%d",&n);
	for(i=1;i<=n;i++) scanf("%d",&a[i]),a[n+1]^=a[i];
	for(i=1;i<=n;i++) scanf("%d",&b[i]),b[n+1]^=b[i];
	n++;
	for(i=1;i<=n;i++) if(a[i]!=b[i]) ++m,c[m]=a[i],d[m]=b[i];
	ans=m;
	if(a[n]!=b[n]) ans-=2;
	else{
		for(i=1;i<=m;i++)
			if(c[i]==a[n]) break;
		if(i<=m) ans--;
	  }
	sort(a+1,a+n+1);
	sort(b+1,b+n+1);
	for(i=1;i<=n;i++) if(a[i]!=b[i]){ printf("-1"); return 0;}
	for(i=1;i<=m;i++) w[++lw]=c[i],w[++lw]=d[i];
	sort(w+1,w+lw+1);
	lw=unique(w+1,w+lw+1)-w-1;
	for(i=1;i<=m;i++){
		c[i]=lower_bound(w+1,w+lw+1,c[i])-w;
		d[i]=lower_bound(w+1,w+lw+1,d[i])-w;
		if(find(c[i])!=find(d[i])) f[find(c[i])]=find(d[i]);
	  }
	for(i=1;i<=lw;i++)
		if(find(i)==i) ans++;
	cout<<ans;
	return 0;
}
