#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 7;

#define inc(i, x, y) for (int i = x; i <= y; ++i)
#define dec(i, x, y) for (int i = x; i >= y; --i)
#define gc getchar

bool chkmin(int &x, int y) { return x > y ? x = y, 1 : 0; }
bool chkmax(int &x, int y) { return x < y ? x = y, 1 : 0; }

int rd() {
	int ret = 0; bool fl = 0; char ch = 0;
	while (!isdigit(ch)) fl |= (ch == '-'), ch = gc();
	while (isdigit(ch)) ret = (ret << 1) + (ret << 3) + (ch ^ 48), ch = gc();
	return fl ? -ret : ret;
}

vector<int> e[N];
bool vis[N], g[N];

void dfs(int u) {
    vis[u] = 1;
    inc(i, 0 ,(signed)e[u].size() - 1) {
        int v = e[u][i];
        if (vis[v]) continue;
        dfs(v);
    }
}

int a[N], b[N], l[N], h[N];

int main() {
	int n = rd(), cnt = 0;
	inc(i, 1, n) a[i] = rd(), l[i] = a[i], a[0] ^= a[i];
	inc(i, 1, n) b[i] = rd(), h[i] = b[i], b[0] ^= b[i];
	l[0] = a[0], h[0] = b[0];
	sort(l, l + n + 1), sort(h, h + n + 1);
	int z = unique(l, l + n + 1) - l, p = unique(h, h + n + 1) - h;
	inc(i, 1, n) if (l[i] != h[i]) return puts("-1"), 0;
	inc(i, 1, n) {
		a[i] = lower_bound(l, l + z, a[i]) - l;
		b[i] = lower_bound(h, h + p, b[i]) - h;
	}
	g[a[0]] = g[b[0]] = 1;
	e[a[0]].push_back(b[0]);
	inc(i, 1, n) {
		if (a[i] == b[i]) continue;
		cnt++; 
		g[a[i]] = g[b[i]] = 1;
		e[a[i]].push_back(b[i]);
	}
	inc(i, 0, n) {
		if (g[i] && (vis[i] == 0)) 
			dfs(i), cnt++;
	}
	printf("%d\n", max(cnt - 1, 0));
}