#include<cstdio> 
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define fo(i,j,k) for(i=j;i<=k;i++)
#define fd(i,j,k) for(i=j;i>=k;i--)
#define cmax(a,b) (a=(a>b)?a:b)
#define cmin(a,b) (a=(a<b)?a:b)
typedef long long ll;
const int N=1e6+5,M=2e6+5,mo=1e9+7;
map<int,int> Ref;
int n,i,x,a[N],b[N],c[N],d[N],pp,ans,y,fa[N],tt,act[N];
int get(int x)
{
	if (fa[x]==x) return x;
	return fa[x]=get(fa[x]);
}
int main()
{
	scanf("%d",&n);
	fo(i,1,n)
	{
		scanf("%d",a+i);
		x^=a[i];
	}
	a[n+1]=x;x=0;
	fo(i,1,n) 
	{
		scanf("%d",b+i);
		x^=b[i];
	}
	b[++n]=x;
	fo(i,1,n) c[i]=a[i],d[i]=b[i];
	sort(c+1,c+1+n);
	sort(d+1,d+1+n);
	pp=1;
	fo(i,1,n) if (c[i]!=d[i]) break;
	if (i<=n)
		printf("-1\n");
	else
	{
		c[0]=-1;
		fo(i,1,n)
		{
			if (c[i]!=c[i-1]) tt++;
			Ref[c[i]]=tt;
		}
		fo(i,1,tt) fa[i]=i;
		fo(i,1,n)
			if (a[i]!=b[i]||i==n)
			{
				ans+=(i<n);
				act[Ref[a[i]]]=act[Ref[b[i]]]=1;
				fa[get(Ref[a[i]])]=get(Ref[b[i]]);
			}
		if (ans)
		{
			fo(i,1,tt) if (act[i]&&fa[i]==i)
				ans++;
			ans--;
		}	
		printf("%d\n",ans);
	}
}