#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define fr(i,x,y) for(int i=x;i<=y;++i)
#define LL long long
using namespace std;
const int N=1e6+10;
map<int,int>qx;
int n,a[N],b[N],c[N],f[N],vis[N];

int find(int k){
	return f[k]==k?k:f[k]=find(f[k]);
}

void add(int x,int y){
	int xx=find(x),yy=find(y);
	if(xx!=yy){
		f[xx]=yy;vis[yy]=1;
	}
}

void fail(){
	printf("-1\n");
	exit(0);
}

int main(){
	scanf("%d",&n);
	fr(i,1,n) {
		scanf("%d",&a[i]);qx[a[i]]++;
		a[n+1]^=a[i];
	}
	qx[a[n+1]]++;
	fr(i,1,n) {
		scanf("%d",&b[i]);qx[b[i]]--;
		if(qx[b[i]]<0) fail();
		b[n+1]^=b[i];
	}
	++n;
	fr(i,1,n) c[i]=a[i];
	int ans=0;
	sort(c+1,c+1+n);
	int len=unique(c+1,c+1+n)-c-1;
	fr(i,1,n) a[i]=lower_bound(c+1,c+1+len,a[i])-c;
	fr(i,1,n) b[i]=lower_bound(c+1,c+1+len,b[i])-c;
	fr(i,1,n) f[i]=i;
	fr(i,1,n) {
		if(a[i]!=b[i]) {
			add(a[i],b[i]);
			if(i!=n) ans++;
		}
	}
	if(!ans) return printf("0\n"),0;
	vis[a[n]]=vis[b[n]]=1;
	fr(i,1,n) if(f[i]==i&&vis[i]) ans++;
	ans--;
	printf("%d\n",ans);
	return 0;
}
/*
4
0 1 2 3
1 0 3 2
*/