#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>

using namespace std;
using pii = pair<int, int>;

int main(){
	ios_base::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n), b(n);
	for(int i = 0; i < n; ++i){ cin >> a[i]; }
	for(int i = 0; i < n; ++i){ cin >> b[i]; }
	int a_sum = 0, b_sum = 0;
	for(int i = 0; i < n; ++i){
		a_sum ^= a[i];
		b_sum ^= b[i];
	}
	a.push_back(a_sum);
	b.push_back(b_sum);
	vector<int> c, d;
	unordered_multimap<int, int> d_map;
	for(int i = 0; i <= n; ++i){
		if(a[i] == b[i]){ continue; }
		c.push_back(a[i]);
		d.push_back(b[i]);
		d_map.emplace(d.back(), d.size() - 1);
	}
	const int m = c.size();
	if(m == 0){
		cout << 0 << endl;
		return 0;
	}
	vector<int> g(m);
	iota(g.begin(), g.end(), 0);
	for(int i = 0; i < m; ++i){
		if(c[i] == d[i]){ continue; }
		int last = c[i];
		g[i] = i;
		while(d_map.find(last) != d_map.end()){
			const auto it = d_map.find(last);
			const int t = c[it->second];
			c[it->second] = last;
			g[it->second] = i;
			last = t;
			d_map.erase(it);
		}
	}
	if(!d_map.empty()){
		cout << -1 << endl;
		return 0;
	}
	vector<vector<int>> h(m);
	for(int i = 0; i < m; ++i){
		h[g[i]].push_back(c[i]);
	}
	int answer = m - 1;
	unordered_set<int> occ;
	for(const auto& v : h){
		if(v.empty()){ continue; }
		bool found = false;
		for(const auto x : v){
			if(occ.find(x) != occ.end()){
				found = true;
				break;
			}
		}
		if(!found){ ++answer; }
		for(const auto x : v){ occ.insert(x); }
	}
	if(a_sum != b_sum){ --answer; }
	cout << answer << endl;
	return 0;
}

