#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <cstring>
#include <cstdlib>
#include <cctype>

#define fo(i, x, y) for (int i = x; i <= y; ++i)
#define fd(i, x, y) for (int i = x; i >= y; --i)

using namespace std;

const int maxn = 1e5 + 5;

int n, cnt, ans;
int a[maxn], b[maxn], tmpa[maxn], tmpb[maxn], fa[maxn], rank[maxn];
map<int, int> mp;

int getint() {
	char ch;
	int res = 0, p;
	while (!isdigit(ch = getchar()) && ch != '-');
	p = ch == '-'? ch = getchar(), -1 : 1;
	while (isdigit(ch))
		res = res * 10 + ch - '0' , ch = getchar();
	return res * p;
}

int getfa(int x) {
	return fa[x]? fa[x] = getfa(fa[x]) : x;
}

void un(int x, int y) {
	int fx = getfa(x), fy = getfa(y);
	if (fx == fy) return;
	if (rank[fx] < rank[fy]) fa[fx] = fy;
		else fa[fy] = fx, rank[fy] += rank[fx] == rank[fy];
}

int main() {
	//freopen("duliu.in", "r", stdin);
	//freopen("duliu.out", "w", stdout);
	
	n = getint();
	fo (i, 1, n) tmpa[i] = a[i] = getint(), a[0] ^= a[i];
	fo (i, 1, n) tmpb[i] = b[i] = getint(), b[0] ^= b[i];
	tmpa[0] = a[0]; tmpb[0] = b[0];
	sort(tmpa, tmpa + 1 + n);
	sort(tmpb, tmpb + 1 + n);
	fo (i, 0, n)
		if (tmpa[i] != tmpb[i]) {
			printf("-1\n");
			return 0;
		}
	if (!mp[a[0]]) mp[a[0]] = ++cnt;
	if (!mp[b[0]]) mp[b[0]] = ++cnt;
	fo (i, 1, n)
		if (a[i] != b[i]) {
			ans++;
			if (!mp[a[i]]) mp[a[i]] = ++cnt;
			if (!mp[b[i]]) mp[b[i]] = ++cnt;
		}
	fo (i, 1, n)
		if (a[i] != b[i]) un(mp[a[i]], mp[b[i]]);
	fo (i, 1, cnt)
		if (!fa[i]) ans++;
	ans--;
	printf("%d\n", ans);
	return 0;
}