#include <cstdio>
#include <algorithm>
#define rep(i,j,k) for (i=j;i<=k;i++)
using namespace std;
const int N=1e5+5;
typedef pair<int,int> Pair;
int n,lst,cn,i,ans,u,v,fa[N],A[N],B[N],C[N*2],a[N],b[N],sz[N];
int find(int x) { return fa[x]==x?x:fa[x]=find(fa[x]); }
void link(int u,int v)
{
	int fx=find(u),fy=find(v);
	if (fx==fy) return;
	fa[fy]=fx;
	sz[fx]+=sz[fy];
}
int main()
{
//	freopen("xor.in","r",stdin);
//	freopen("xor.out","w",stdout);
	scanf("%d",&n);
	rep(i,1,n) scanf("%d",&A[i]),C[++cn]=A[i],A[n+1]^=A[i];
	rep(i,1,n) scanf("%d",&B[i]),C[++cn]=B[i],B[n+1]^=B[i];
	C[++cn]=A[n+1]; C[++cn]=B[n+1];
	sort(C+1,C+1+cn);
	cn=unique(C+1,C+1+cn)-C-1;
	
	rep(i,1,cn) fa[i]=i,sz[i]=1;

	n++;
	lst=lower_bound(C+1,C+1+cn,A[n])-C;
	rep(i,1,n)
		if (A[i]!=B[i]) {
			u=lower_bound(C+1,C+1+cn,A[i])-C;
			v=lower_bound(C+1,C+1+cn,B[i])-C;
			link(u,v);
			if (i!=n) ans++;
		}

	sort(A+1,A+1+n); sort(B+1,B+1+n);
	rep(i,1,n)
		if (A[i]!=B[i]) {
			printf("-1\n"); return 0;
		}
		
	rep(i,1,cn)
		if (fa[i]==i && sz[i]>1) ans++;
	if (fa[lst]==lst && sz[lst]==1) ans++;
	printf("%d\n",ans-1);
	return 0;
}