#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
	{
		 fo(i,1,n)
	        if (a[i]!=b[i]||i==n)
			{
	            if (i<n) ans++;
	            if (!Ref[a[i]]) Ref[a[i]]=++tt;
	            if (!Ref[b[i]]) Ref[b[i]]=++tt;
	        }
	    if (!ans)
		{
	        printf("0\n");
	        return 0;
	    }
	    fo(i,1,tt) fa[i]=i;
	    fo(i,1,n)
	        if (a[i]!=b[i]) fa[get(Ref[a[i]])]=get(Ref[b[i]]);
	    fo(i,1,tt)
	        if (fa[i]==i) ans++;
	    printf("%d\n",ans-1);
	}
}