#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <map>
#define fo(i,a,b) for(register int i=a;i<=b;++i)
#define fod(i,a,b) for(register int i=a;i>=b;--i)
#define efo(i,q) for(int i=A[q];i;i=B[i][0])
using namespace std;
const int N=100500;
int read(int &n)
{
	char ch=' ';int q=0,w=1;
	for(;(ch!='-')&&((ch<'0')||(ch>'9'));ch=getchar());
	if(ch=='-')w=-1,ch=getchar();
	for(;ch>='0' && ch<='9';ch=getchar())q=q*10+ch-48;n=q*w;return n;
}
int m,n,m1,ans;
int a[N],b[N];
map<int,int>zc;
map<int,int>A;
map<int,bool>z;
int B[2*N][2],B0;
void link(int q,int w)
{
	B[++B0][0]=A[q],A[q]=B0,B[B0][1]=w;
	B[++B0][0]=A[w],A[w]=B0,B[B0][1]=q;
}
void dfs(int q)
{
	z[q]=1;
	efo(i,q)if(!z[B[i][1]])dfs(B[i][1]);
}
int main()
{
	int q,w,e=-1;
	read(n);
	q=w=0;
	fo(i,1,n)q=q^read(a[i]),++zc[a[i]];
	fo(i,1,n)
	{
		if(a[i]!=read(b[i]))
		{
			++ans;
			link(b[i],a[i]);
		}
		if((--zc[b[i]])<0)++w,e=b[i];
	}
	if(!ans)return printf("0\n"),0;
	if(w>1)return printf("-1\n"),0;
	fo(i,1,n)if(a[i]!=b[i]&&!z[b[i]])dfs(b[i]),++ans;
	if(z[q]&&!w||w&&e==q)--ans;
	if(w&&e!=q)printf("-1\n");
	else printf("%d\n",ans);
	return 0;
}