#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
#define mp(a,b) std::make_pair(a,b)
#define ITR(a,b) for(auto a:b)
#define REP(a,b,c) for(register int a=(b),a##end=(c);a<=a##end;++a)
#define DEP(a,b,c) for(register int a=(b),a##end=(c);a>=a##end;--a)
const int MAXN=100000+10;
int n,m,A[MAXN],B[MAXN],fa[MAXN<<1],cnt,vA,vB;
std::vector<int> V;
std::map<int,int> M;
template<typename T> inline void read(T &x)
{
	T data=0,w=1;
	char ch=0;
	while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
	if(ch=='-')w=-1,ch=getchar();
	while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
	x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
	if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void discretization()
{
	REP(i,1,n)V.push_back(A[i]);
	std::sort(V.begin(),V.end());
	V.erase(std::unique(V.begin(),V.end()),V.end());
	REP(i,0,V.size()-1)M[V[i]]=i+1;m=V.size();
	REP(i,1,n)A[i]=M[A[i]];
	V.clear();M.clear();
	REP(i,1,n)V.push_back(B[i]);
	std::sort(V.begin(),V.end());
	V.erase(std::unique(V.begin(),V.end()),V.end());
	REP(i,0,V.size()-1)M[V[i]]=i+1;m=V.size();
	REP(i,1,n)B[i]=M[B[i]];
}
inline int found(int x)
{
	if(fa[x]!=x)fa[x]=found(fa[x]);
	return fa[x];
}
inline bool same()
{
	REP(i,1,n)if(A[i]!=B[i])return false;
	return true;
}
int main()
{
	read(n);
	REP(i,1,n)read(A[i]),vA^=A[i];
	REP(i,1,n)read(B[i]),vB^=B[i];
	if(same())
	{
		puts("0");
		return 0;
	}
	A[++n]=vA;B[n]=vB;
	discretization();
	REP(i,1,m)fa[i]=i;
	cnt=m;
	REP(i,1,n)if(A[i]!=B[i]||i==n)
	{
		if(i!=n)cnt++;
		int u=found(A[i]),v=found(B[i]);
		if(u!=v)fa[u]=v,cnt--;
	}
	std::sort(A+1,A+n+1);
	std::sort(B+1,B+n+1);
	REP(i,1,n)if(A[i]!=B[i])
	{
		puts("-1");
		return 0;
	}
	printf("%d\n",cnt-1);
	return 0;
}
