#include <bits/stdc++.h>
#define sz(c)      int(c.size())
#define rep(i,a,b) for (int i=a; i<(b); ++i)
#define per(i,a,b) for (int i=(b)-1; i>=(a); --i)
using namespace std;
using ll = long long;

int const MAXN=110000;
int N;
int a[MAXN];
int b[MAXN];
map<int,vector<int>> posB;
bool was[MAXN];

void dfs(int v) {
	was[v]=true;
	for (int u:posB[a[v]]) if (!was[u]) dfs(u);
}

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	cout<<fixed<<setprecision(10);

	cin>>N;
	rep(i,0,N) cin>>a[i];
	rep(i,0,N) cin>>b[i];
	
	rep(i,0,N) a[N]^=a[i];
	rep(i,0,N) b[N]^=b[i];
	
	vector<int> vA(a,a+N+1);
	vector<int> vB(b,b+N+1);
	
	if (vA==vB) {
		cout<<"0\n";
		return 0;
	}
	
	sort(vA.begin(),vA.end());
	sort(vB.begin(),vB.end());
	
	if (vA!=vB) {
		cout<<"-1\n";
		return 0;
	}
	
//~ cerr<<a[N]<<endl;
//~ cerr<<b[N]<<endl;
	
	int res=0;
	rep(i,0,N+1) if (a[i]!=b[i]) {
		res++;
		posB[b[i]].push_back(i);
	}
	
	if (a[N]!=b[N]) res--;
	
	int cc=0;
	rep(i,0,N+1) if (a[i]!=b[i] && !was[i]) {
		cc++;
		dfs(i);
	}
	
	res+=cc;
	
	if (a[N]!=b[N] || posB.count(b[N])) {
		res--;
	}

	cout<<res<<"\n";
}
