#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int N=1e5+50;
int n,a[N],b[N],c[N],c2[N],m,ans;bool vis[N];
vector<int>v[N];
void dfs(int x){
	vis[x]=1;
	for(int i=0,y;i<v[x].size();i++)
		if(!vis[y=v[x][i]])dfs(y);
}
int main(){
	scanf("%d",&n);n++;
	for(int i=1;i<n;i++)scanf("%d",&a[i]),a[n]^=a[i];
	for(int i=1;i<n;i++)scanf("%d",&b[i]),b[n]^=b[i];
	for(int i=1;i<=n;i++)c[i]=a[i],c2[i]=b[i];
	sort(c+1,c+n+1);sort(c2+1,c2+n+1);
	for(int i=1;i<=n;i++)if(c[i]!=c2[i])puts("-1"),exit(0);
	m=unique(c+1,c+n+1)-c-1;
	for(int i=1;i<=n;i++)a[i]=lower_bound(c+1,c+m+1,a[i])-c,b[i]=lower_bound(c+1,c+m+1,b[i])-c;
	for(int i=1;i<=n;i++)if(a[i]!=b[i])v[a[i]].pb(b[i]),ans++;
	for(int i=1;i<=m;i++)if(!vis[i]&&v[i].size())dfs(i),ans++;ans-=2;
	if(a[n]==b[n]){
		if(!v[a[n]].size())ans+=2;
		else ans++;
	}
	cout<<ans;
	return 0;
}