#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#define N 100005
using namespace std;
int n,a[N],b[N],aa[N],bb[N],fa[N],cnt,ans;
bool vis[N<<1];
map<int,int>mp;
template<class T>inline void read(T &x) {
	register T s=0,g=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')g=1;ch=getchar();}
	while(ch>='0'&&ch<='9'){s=(s<<3)+(s<<1)+(ch^48);ch=getchar();}
	x=g?-s:s;
}
int getfa(int k){return fa[k]==k?k:fa[k]=getfa(fa[k]);}
int main() {
	read(n);
	for(register int i=1;i<=n;i=-~i)
		read(a[i]),a[n+1]^=a[i];
	for(register int i=1;i<=n;i=-~i)
		read(b[i]),b[n+1]^=b[i];
	++n;
	memcpy(aa,a,(n+1)<<2),memcpy(bb,b,(n+1)<<2);
	sort(aa+1,aa+n+1);sort(bb+1,bb+n+1);
	for(register int i=1;i<=n;i=-~i) {
		if(aa[i]!=bb[i]) {
			puts("-1");
			return 0;
		}
		if(!mp[aa[i]]) mp[aa[i]]=++cnt;
	}
	for(register int i=1;i<=cnt;i=-~i)
		fa[i]=i;
	for(register int i=1,f1,f2;i<=n;i=-~i) {
		a[i]=mp[a[i]],b[i]=mp[b[i]];
		if(a[i]!=b[i]) {
			ans+=i<n;
			f1=getfa(a[i]),f2=getfa(b[i]);
			if(f1!=f2) {
				fa[f1]=f2;
				vis[f1]=vis[f2]=true;
			}
		}
	}
	if(!ans) {
		puts("0");
		return 0;
	}
	vis[a[n]]=vis[b[n]]=true;
	for(register int i=1;i<=cnt;i=-~i)
		if(vis[i]&&getfa(i)==i) ++ans;
	printf("%d\n",ans-1);
	return 0;
}