#include<iostream>
#include<algorithm>
#include<vector>
#include<map>

using namespace std;

#define sz(x) (int)(x.size())
#define rep(i,a,b) for(int i=a;i<b;++i)
#define pb push_back
///////////////////

int const N = 2e5 + 41;

int n;
vector<int> a, b, ha, hb;
map<int, int> h, bal;
int x, w[N];
vector<int> e[N];
int ans;

void print(int v){
	cout << v << endl;
	exit(0);
}

int getHash(int v){
	if(h.find(v) == h.end()){
		int val = sz(h);
		h[v] = val;
	}
	return h[v];
}

void read(){
	cin >> n;
	rep(i, 0, n){
		int v;
		cin >> v;
		a.pb(v);
	}
	rep(i, 0, n){
		int v;
		cin >> v;
		b.pb(v);
	}
}

void hashIt(){
	rep(i, 0, n) x ^= (a[i]);
	getHash(x);
	rep(i, 0, n) getHash(a[i]), getHash(b[i]);
}

void checkBad(){
	bal[getHash(x)] = 1;
	rep(i, 0, n){
		++bal[getHash(a[i])];
		--bal[getHash(b[i])];
	}
	rep(i, 0, n){
		if(a[i] == b[i]) continue;
		ha.pb(getHash(a[i]));
		hb.pb(getHash(b[i]));
	}
	int bad = -1;
	for(auto it : bal){
		if(it.second != 0){
			if(bad != -1) print(-1);
			bad = it.first;
		}
	}
	ha.pb(getHash(x));
	hb.pb(bad);
}

void pushEdges(){
	ans = sz(ha);
	rep(i, 0, sz(ha)){
		e[ha[i]].pb(hb[i]);
		e[hb[i]].pb(ha[i]);
	}
}

void dfs(int u){
	if(w[u]) return;
	w[u] = 1;
	for(int to : e[u]) dfs(to);
}

void solve(){
	read();
	hashIt();
	checkBad();
	pushEdges();
	rep(i, 0, sz(ha)){
		if(!w[ha[i]]){
			++ans;
			dfs(ha[i]);
		}
	}
	cout << print(ans - 2) << endl;
}

int main(){
#ifdef _DEBUG
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif

	solve();

	return 0;
}