//Heaplax
#include<bits/stdc++.h>
#define N 100005
#define LL long long
#define LOG(x) cerr<<#x<<" = "<<x<<endl
#define add_edge(u,v) nxt[++cnt]=head[u],head[u]=cnt,to[cnt]=v
#define open(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
char ch;bool fs;void re(int& x)
{
	while(ch=getchar(),ch<33);
	if(ch=='-')fs=1,x=0;else fs=0,x=ch-48;
	while(ch=getchar(),ch>33)x=x*10+ch-48;
	if(fs)x=-x;
}
using namespace std;
int n,x,ans,cnt,a[N],b[N],fa[N];
map<int,int> s,dy;
int find(int x)
{
	return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main()
{
	re(n);
	for(int i=1;i<=n;++i)
		re(a[i]),--s[a[i]],x^=a[i];
	for(int i=1;i<=n;++i)
		re(b[i]),++s[b[i]];
	--s[x];
	for(auto i:s)
		if(i.second>0)
			return puts("-1"),0;
	for(int i=1;i<=n;++i)
		if(a[i] != b[i])
		{
			if(!dy[a[i]])dy[a[i]]=++cnt,fa[cnt]=cnt;
			if(!dy[b[i]])dy[b[i]]=++cnt,fa[cnt]=cnt;
			fa[find(dy[a[i]])]=find(dy[b[i]]);
			++ans;
		}
	if(!ans)return puts("0"),0;
	if(!dy[x])++ans;
	--ans;
	for(int i=1;i<=cnt;++i)
		ans+=find(i)==i;
	printf("%d\n",ans);
}
