#include<cstdio>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#define neko 100010
#define orz return printf("-1\n"),0
#define f(i,a,b) for(register int i=(a);i<=(b);i=-(~i))
int n,a[neko],b[neko],x,now,pos,num,leap;
__gnu_pbds::gp_hash_table<int,int>ht,id;
int main()
{
	scanf("%d",&n);
	f(i,1,n)scanf("%d",&a[i]),x^=a[i],++ht[a[i]];
	f(i,1,n)
	{
		scanf("%d",&b[i]),id[b[i]]=i;
		if((--ht[b[i]])<0)++now,num=b[i];
		if(a[i]^b[i])++pos;
	}
	if(now>1)orz;
	else if(now==1&&x^num)orz;
	else
	{
		if(!now)num=x;
		f(i,1,pos)
		{
			leap=id[x];
			if(i<n&&a[leap]==num){++pos;break;}
			x=a[id[x]];
		}return printf("%d\n",pos),0;
	}
}