#include<bits/stdc++.h> 
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
typedef long long ll;
using namespace std;
inline int read(){
	int n=0,f=1;char c;
	for(c=getchar();(c<'0'||c>'9')&&c!='-';c=getchar());
	if (c=='-') c=getchar(),f=-1;
	for(;c>='0'&&c<='9';c=getchar()) n=n*10+c-48;
	return n*f;
}
const int maxn=1e5+5;
int i,j,n,a[maxn],b[maxn],x,ans;bool p;
int f[maxn],cnt;bool bz[maxn];
map<int,int>d;
map<int,int>c;
int get(int x){return (f[x])?f[x]=get(f[x]):x;}
void hb(int x,int y){
	x=get(d[x]),y=get(d[y]);
	if (x!=y) f[x]=y;
}
int main(){
	n=read();
	fo(i,1,n) x^=(a[i]=read()),c[a[i]]++;
	c[x]++;
	fo(i,1,n) {
		b[i]=read();
		if (!d[b[i]]) d[b[i]]=++cnt;
		if (c[b[i]]) c[b[i]]--;else return puts("-1"),0;
	}
	fo(i,1,n) if (a[i]!=b[i]) hb(a[i],b[i]),p|=b[i]==x,ans++;
	fo(i,1,n) if (a[i]!=b[i]) ans+=!bz[get(d[a[i]])],bz[get(d[a[i]])]=1;
	printf("%d\n",ans-p);
}