#include <bits/stdc++.h>

#define F(i,a,b) for (int i = a; i <= b; i ++)

const int N = 1e5 + 10;

using namespace std;

int n, m, a[N], b[N], cnt;
int x, y, ans, fa[N], tot; bool bz[N];
struct node {
	int x, num;
	friend bool operator < (node a, node b) { return a.x < b.x; }
} A[N], B[N];

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

int main() {
	scanf("%d", &n);
	F(i, 1, n) scanf("%d", &a[i]), x = x ^ a[i]; a[n + 1] = x; x = 0;
	F(i, 1, n) scanf("%d", &b[i]), x = x ^ b[i]; b[n + 1] = x; x = 0;

	F(i, 1, n + 1) A[i] = {a[i], i};
	F(i, 1, n + 1) B[i] = {b[i], i};
	sort(A + 1, A + n + 2);
	sort(B + 1, B + n + 2);

	F(i, 1, n + 1)
		if (A[i].x ^ B[i].x) { puts("-1"); return 0; }
	F(i, 1, n + 1)
		a[A[i].num] = cnt += A[i].x != A[i - 1].x;
	cnt = 0;
	F(i, 1, n + 1)
		b[B[i].num] = cnt += B[i].x != B[i - 1].x;
	
	F(i, 1, n)
		if (a[i] ^ b[i]) {
			x = get(a[i]), y = get(b[i]), ans += (i <= n);
			if (x ^ y) fa[x] = y;
		}
	F(i, 1, n + 1)
		if (a[i] ^ b[i] && (get(a[i]) == a[i]) && !bz[a[i]])
			ans ++, tot ++, bz[a[i]] = 1;
	if (ans == 0) { puts("0"); return 0; }
	ans ++;
	F(i, 1, n)
		if (b[i] == a[n + 1] && a[i] ^ b[i])
			{ ans --; break; }

	printf("%d\n", ans - (tot > 0));
}