#include<bits/stdc++.h>
#define N 400005
using namespace std;
int n,a[N],b[N],cnt,to[N],nxt[N],fst[N],l,vis[N],Ans,s[N],s1[N];
map<int,int>Map;
void link(int x,int y)
{
	to[++l]=y;nxt[l]=fst[x];fst[x]=l;
	to[++l]=x;nxt[l]=fst[y];fst[y]=l;
}
void dfs(int x)
{
	vis[x]=0;
	for (int i=fst[x];i;i=nxt[i])
		if (vis[to[i]])dfs(to[i]);
}
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]),a[0]^=a[i];
	for (int i=1;i<=n;i++)
		scanf("%d",&b[i]),b[0]^=b[i];
	for (int i=0;i<=n;i++)
	{
		if (!Map[a[i]]) Map[a[i]]=++cnt;
		if (!Map[b[i]]) Map[b[i]]=++cnt;
		a[i]=Map[a[i]];b[i]=Map[b[i]];
		s[a[i]]++;s1[b[i]]++;
	}
	for (int i=1;i<=cnt;i++)
		if (s[i]!=s1[i])
		{
			puts("-1");
			return 0;
		}
	for (int i=1;i<=n;i++)
		if (a[i]!=b[i])
		{
			vis[a[i]]=vis[b[i]]=1;
			link(a[i],b[i]);
			Ans++;
		}
	if (a[0]!=b[0]||a[0]==b[0]&&s[a[0]]>1) Ans--;
	for (int i=1;i<=cnt;i++)
	if (vis[i])
	{
		dfs(i);
		Ans++;
	}
	printf("%d\n",max(0,Ans));
}