#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;

int a[200000],b[200000];
vector<int>E[200000];
bool used[200000];
int deg[200000];

void dfs(int v){
	used[v]=true;
	for(int u:E[v]){
		if(!used[u])dfs(u);
	}
}
int main(){
	int n;cin>>n;
	map<int,int>mp;
	vector<int>vs;
	int x=0;
	rep(i,n){
		scanf("%d",&a[i]);
		mp[a[i]]++;
		x^=a[i];
		vs.push_back(a[i]);
	}
	a[n]=x;
	mp[x]++;
	vs.push_back(x);
	x=0;
	rep(i,n){
		scanf("%d",&b[i]);
		mp[b[i]]--;
		x^=b[i];
	}
	b[n]=x;
	mp[x]--;
	for(auto p:mp){
		if(p.second){
			puts("-1");return 0;
		}
	}
	map<int,vector<int>>want;
	rep(i,n){
		if(a[i]!=b[i]){
			want[b[i]].push_back(i);
		}
	}
	if(want.empty()){
		puts("0");return 0;
	}
	sort(vs.begin(),vs.end());
	vs.erase(unique(vs.begin(),vs.end()),vs.end());
	int cnt=0;
	rep(i,n+1){
		a[i]=lower_bound(vs.begin(),vs.end(),a[i])-vs.begin();
		b[i]=lower_bound(vs.begin(),vs.end(),b[i])-vs.begin();
		if(i==n||a[i]==b[i])continue;
		cnt++;
		E[b[i]].push_back(a[i]);
		E[a[i]].push_back(b[i]);
		deg[a[i]]++;
		deg[b[i]]++;
	}
	int K=0;
	rep(i,vs.size()){
		if(deg[i]&&!used[i]){
			K++;
			dfs(i);
		}
	}
	if(deg[a[n]]){
		cout<<cnt+K-1<<endl;
	}
	else{
		cout<<cnt+K<<endl;
	}
}