#include<bits/stdc++.h>
#define N 200010
#define pp pair<int,int>
#define P pair<pp,int>
using namespace std;
int a[N],b[N],s[N],tot;
int deg1[N],deg2[N];
int fa[N];
int ans;
int Find(int x){return fa[x]==x?x:fa[x]=Find(fa[x]);}
void merge(int x,int y){
	if(fa[x]==-1)fa[x]=x;
	if(fa[y]==-1)fa[y]=y;
	int g1=Find(x),g2=Find(y);
	if(g1!=g2)fa[g1]=g2;
}
vector<int>q[N];
bool ok[N];
int main(){
	int n,sum=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]),sum^=a[i],s[++tot]=a[i];
	for(int i=1;i<=n;i++)scanf("%d",&b[i]),s[++tot]=b[i];
	s[++tot]=sum;
	sort(s+1,s+tot+1);
	tot=unique(s+1,s+tot+1)-s-1;
	memset(fa,-1,sizeof fa);
	for(int i=1;i<=n;i++){
		if(a[i]!=b[i]){
			int g1=lower_bound(s+1,s+tot+1,a[i])-s;
			int g2=lower_bound(s+1,s+tot+1,b[i])-s;
			deg1[g2]++,deg2[g1]++;
			merge(g1,g2);
			ans++;
		}
	}
	int gg=lower_bound(s+1,s+tot+1,sum)-s;
	if(fa[gg]==-1)fa[gg]=gg;
	for(int i=1;i<=tot;i++){
//		cout<<"!!"<<i<<" "<<deg1[i]<<" "<<deg2[i]<<endl;
		if(deg1[i]>deg2[i]){
			int r=deg1[i]-deg2[i];
			if(r>=2)return 0*printf("-1\n");
			else {
				if(gg!=i)return 0*printf("-1\n");
			}
		}
	}
	int num=0;
	for(int i=1;i<=tot;i++)if(fa[i]==i&&Find(i)!=Find(gg))num++;
	printf("%d\n",num+ans);
	return 0;
}