#include<bits/stdc++.h>
#define Rint register int
#define mem(a,b) memset(a,(b),sizeof(a))
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
#define For(i,j,k) for(Rint i=(j),i##end=(k);i<=i##end;i++)
#define Fordown(i,j,k) for(Rint i=(j),i##end=(k);i>=i##end;i--)
#define Travel(i,u,v) for(Rint i=beg[u],v=to[i];i;i=nex[i],v=to[i])

using namespace std;
typedef long long LL;

template<typename T>inline void read(T &x){
	x=0;T w=1,ch=getchar();
	while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
	while(isdigit(ch))x=(x<<3)+(x<<1)+(ch^'0'),ch=getchar();
	x=x*w;
}

#define pb push_back

const int N=1e5+10,M=N<<1;
int n,a[N],b[N];
int ta[N],tb[N];

int ls[M],tot;
int fa[M];
bool vis[M];

inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}

int main(){
//	File("duliu");
	read(n);n++;
	For(i,1,n-1)read(a[i]),a[n]^=a[i],ls[++tot]=a[i];
	For(i,1,n-1)read(b[i]),b[n]^=b[i],ls[++tot]=b[i];
	sort(ls+1,ls+tot+1);
	tot=unique(ls+1,ls+tot+1)-ls-1;
	For(i,1,n){
		a[i]=lower_bound(ls+1,ls+tot+1,a[i])-ls;
		b[i]=lower_bound(ls+1,ls+tot+1,b[i])-ls;
	}

	For(i,1,n)if(a[i]!=b[i])fa[a[i]]=a[i],fa[b[i]]=b[i];

	For(i,1,n)ta[i]=a[i],tb[i]=b[i];
	sort(ta+1,ta+n+1);sort(tb+1,tb+n+1);
	For(i,1,n)if(ta[i]!=tb[i]){printf("-1\n");return 0;}

	int Ans=0,sum=0;
	For(i,1,n)fa[find(a[i])]=find(b[i]);
	For(i,1,n-1)if(a[i]!=b[i])Ans++,vis[a[i]]=vis[b[i]]=1;
	For(i,1,tot)if(vis[i]&&find(i)==i)sum++;
	if(sum>0)sum--;
	printf("%d\n",Ans+sum);
	return 0;
}