#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define N 100007
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
int i,j,k,n,x,y,gx,gy,cnt,ans;
int a[N],b[N],c[N],d[N];
int fa[N];
bool bz[N];
int get(int x){return fa[x]==x?x:fa[x]=get(fa[x]);}
int read(){
	int fh=1,res=0;char ch;
	while((ch<'0'||ch>'9')&&(ch^'0'))ch=getchar();
	if(ch=='-')fh=-1,ch=getchar();
	while(ch>='0'&&ch<='9')res=(res<<3)+(res<<1)+(ch^'0'),ch=getchar(); 
	return fh*res;
}
int main(){
	n=read();
	fo(i,1,n)a[i]=read(),a[n+1]^=a[i];fo(i,1,n)b[i]=read(),b[n+1]^=b[i];
	memcpy(c,a,sizeof(c));memcpy(d,b,sizeof(d));n++;
	sort(c+1,c+n+1);sort(d+1,d+n+1);
	fo(i,1,n)if(c[i]!=d[i]){printf("-1");return 0;}
	cnt=unique(c+1,c+n+1)-c-1;
	fo(i,1,cnt)fa[i]=i;
	fo(i,1,n)a[i]=lower_bound(c+1,c+cnt+1,a[i])-c,b[i]=lower_bound(c+1,c+cnt+1,b[i])-c;
	fo(i,1,n){
		if(a[i]==b[i]&&i<n)continue;
		ans+=(i<n);
		x=a[i],y=b[i];
		gx=get(x);gy=get(y);
		if(gx!=gy)fa[gy]=gx;
		bz[x]=bz[y]=1;
	}
	fo(i,1,cnt)fa[i]=get(fa[i]);
	fo(i,1,cnt)if(bz[i]&&fa[i]==i)ans++;
	if(ans)ans--;
	printf("%d",ans);
	return 0;
}