#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],bas;
int cnt,p[N],q[N];
int ls[M],tot;
int visp[M],visq[M];

int main(){
//	File("duliu");
	read(n);
	For(i,1,n)read(a[i]),bas^=a[i],ls[++tot]=a[i];
	For(i,1,n)read(b[i]),ls[++tot]=b[i];
	ls[++tot]=bas;
	sort(ls+1,ls+tot+1);
	tot=unique(ls+1,ls+tot+1)-ls-1;
	bas=lower_bound(ls+1,ls+tot+1,bas)-ls;
	For(i,1,n)if(a[i]!=b[i]){
		p[++cnt]=lower_bound(ls+1,ls+tot+1,a[i])-ls;
		q[cnt]=lower_bound(ls+1,ls+tot+1,b[i])-ls;
	}
	
	visp[bas]++;
	For(i,1,cnt)visp[p[i]]++;
	For(i,1,cnt)visq[q[i]]++;
	For(i,1,tot)if(visq[i]>visp[i]){
		printf("-1\n");
		return 0;
	}
	int fuck=0;
	For(i,1,n)if(p[i]!=q[i])fuck++;
	if(fuck==0){
		printf("0\n");
		return 0;
	}
	sort(p+1,p+cnt+1);
	sort(q+1,q+cnt+1);
	int flag=1;
	For(i,1,n)if(p[i]!=q[i]){flag=0;break;}
	printf("%d\n",cnt+flag);
	return 0;
}