#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <multset>
#include <map>
using namespace std;

#define SZ(a) (int)(a).size()
typedef long long ll;

int main() {
	cin.sync_with_stdio(false);
	int n;
	map<int, int> t;
	vector<int> a;
	int xo = 0;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		a.push_back(x);
		xo ^= x;
		t[x]++;
	}
	t[xo]++;
	vector<int> b;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		b.push_back(x);
		t[x]--;
		if (t[x] < 0) {
			puts("-1");
			return 0;
		}
	}
	map<int, multiset<int>> ab, ba;
	for (int i = 0; i < n; i++) {
		if (a[i] != b[i]) {
			ab[a[i]].insert(b[i]);
			ba[b[i]].insert(a[i]);
		}
	}
	int ans = 0;
	while (!ab.empty()) {
		int bx;
		int ax;
		if (ba.find(xo) != end(ba)) {
			bx = xo;
			ax = *begin(ba[bx]);
		}
		else {
			for (auto it = begin(ab); it != end(ab); ++it) {
				if (*it != xo) {
					ax = *it;
					bx = *begin(ab[ax]);
					ab[ax].insert(xo);
					ba[xo].insert(ax);
				}
			}
		}
		ba[bx].erase(begin(ba[bx]));
		if (ba[bx].empty()) {
			ba.erase(bx);
		}
		ab[ax].erase(ab[ax].find(bx));
		if (ab[ax].empty()) {
			ab.erase(ax);
		}
		xo = ax;
	}
	printf("%d\n", ans);
	return 0;
}
