#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define inf 1e9
#define pb push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

inline int read()
{
	int x=0,w=1; char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') w=-1; c=getchar();}
	while(c<='9'&&c>='0') {x=(x<<1)+(x<<3)+c-'0'; c=getchar();}
	return w==1?x:-x;
}

int n,a[maxn],b[maxn],c[maxn],d[maxn],cnt,n1,n2,nw,fl,f[maxn],t1,t2,ans;

inline int F(int x)
{
	if(f[x]==x) return x;
	return f[x]=F(f[x]);
}

int main()
{
	n=read(); rep(i,1,n) a[i]=read(),n1^=a[i]; rep(i,1,n) b[i]=read(),n2^=b[i];
	a[n+1]=n1; b[n+1]=n2;
	rep(i,1,n) if(a[i]!=b[i]) c[++cnt]=a[i],d[cnt]=b[i],ans++; c[++cnt]=n1; d[cnt]=n2;
	sort(c+1,c+cnt+1); sort(d+1,d+cnt+1);
	rep(i,1,cnt) if(c[i]!=d[i]) {fl=1; break;}
	if(fl) return puts("-1"),0;
	t1=unique(c+1,c+cnt+1)-c-1; rep(i,1,t1) f[i]=i;
	//rep(i,1,t1) cout<<c[i]<<" "; cout<<endl;
	rep(i,1,(n+1))
	{
		if(a[i]!=b[i])
		{
			int x=lower_bound(c+1,c+t1+1,a[i])-c;
			int y=lower_bound(c+1,c+t1+1,b[i])-c;
			int fx=F(x),fy=F(y); if(fx!=fy) f[fx]=fy;
		}
	}
	rep(i,1,t1) if(f[i]==i) ans++; cout<<ans-1<<endl;
	return 0;
}