#include<iostream>
#include<stdio.h>
#include<string.h>
#include<map>
#include<set>
using namespace std;
struct node{
	int x,y;
	node(int _x,int _y){x=_x,y=_y;}
};
operator <(node x,node y){
	if(x.x==y.x)return x.y<y.y;
	return x.x<y.x;
}
set<node>s;
int n,a[120000],b[120000];
int x;
map<int,int>p;
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]),x^=a[i],p[a[i]]++;
	p[x]++;
	for(int i=1;i<=n;i++)scanf("%d",&b[i]),p[b[i]]--;
	for(int i=1;i<=n;i++)if(p[b[i]]<0){printf("-1\n");return 0;}
	for(int i=1;i<=n;i++)if(a[i]!=b[i])s.insert(node(b[i],a[i]));//,printf("%d %d\n",b[i],a[i]);
	int ans=0;
//	printf("%d\n",s.size());
	while(!s.empty()){
		set<node>::iterator it=s.lower_bound(node(x,-1));
		if(it==s.end())it=s.begin();
		node o=*it;
		s.erase(it);
		swap(x,o.y);
		ans++;
		if(o.x!=o.y)s.insert(o);
	}
	printf("%d\n",ans);
}