#include<algorithm>
#include<cstring>
#include<cstdio>
#define mxn 1000010
using namespace std;
int n,m,sl,fh,ans,a[mxn],b[mxn],s[mxn],A[mxn],B[mxn],fa[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
int getfa(int x)
{
	if(fa[x]!=x) fa[x]=getfa(fa[x]);
	return fa[x];
}
int main()
{
	n=rd();
	for(int i=1;i<=n;++i) a[n+1]^=(a[i]=rd());
	for(int i=1;i<=n;++i) b[n+1]^=(b[i]=rd());
	++n;memcpy(A+1,a+1,n<<2);memcpy(B+1,b+1,n<<2);
	sort(A+1,A+n+1);sort(B+1,B+n+1);
	for(int i=1;i<=n;++i) if(A[i]!=B[i]) return puts("-1"),0;
	for(int i=1;i<n;++i)
		if(a[i]!=b[i])
			++ans,s[++m]=a[i],s[++m]=b[i];
	if(!ans) return puts("0"),0;
	--ans;s[++m]=a[n];s[++m]=b[n];
	sort(s+1,s+m+1);m=unique(s+1,s+m+1)-s-1;
	for(int i=1;i<=m;++i) fa[i]=i;
	int x,y,fx,fy;
	for(int i=1;i<=n;++i)
		if(a[i]!=b[i])
		{
			x=lower_bound(s+1,s+m+1,a[i])-s;
			y=lower_bound(s+1,s+m+1,b[i])-s;
			fx=getfa(x);fy=getfa(y);
			if(fx!=fy) fa[fy]=fx;
		}
	for(int i=1;i<=m;++i)
		if(getfa(i)==i)
			ans++;
	printf("%d\n",ans);
	return 0;
}