Problem:
Score : 1000 points

Problem Statement

There is a sequence of length N: a = (a_1, a_2, ..., a_N).
Here, each a_i is a non-negative integer.

Snuke can repeatedly perform the following operation:

Let the XOR of all the elements in a be x. Select an integer i (1 ≤ i ≤ N) and replace a_i with x.

Snuke's objective is to match a with another sequence b = (b_1, b_2, ..., b_N).
Here, each b_i is a non-negative integer.

Determine whether the objective is achievable, and find the minimum necessary number of operations if the answer is positive.

Constraints

2 ≤ N ≤ 10^5

a_i and b_i are integers.

0 ≤ a_i, b_i < 2^{30}

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N
b_1 b_2 ... b_N

Output

If the objective is achievable, print the minimum necessary number of operations.
Otherwise, print -1 instead.

Sample Input 1

3
0 1 2
3 1 0

Sample Output 1

2

At first, the XOR of all the elements of a is 3.
If we replace a_1 with 3, a becomes (3, 1, 2).

Now, the XOR of all the elements of a is 0.
If we replace a_3 with 0, a becomes (3, 1, 0), which matches b.

Sample Input 2

3
0 1 2
0 1 2

Sample Output 2

0

Sample Input 3

2
1 1
0 0

Sample Output 3

-1

Sample Input 4

4
0 1 2 3
1 0 3 2

Sample Output 4

5

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>

#define For(i, l, r) for(register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for(register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)

using namespace std;

template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }

inline int read() {
	int x(0), sgn(1); char ch(getchar());
	for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
	for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
	return x * sgn;
}

void File() {
#ifdef zjp_shadow
	freopen ("duliu.in", "r", stdin);
	freopen ("duliu.out", "w", stdout);
#endif
}

const int N = 1e5 + 1e3;

int n, a[N], b[N], va[N], vb[N], fa[N];

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

int tot;
inline int find_pos(int x) {
	return lower_bound(va + 1, va + tot + 1, x) - va;
}

bitset<N> vis;

int main () {

	File();

	n = read() + 1;

	For (i, 1, n - 1)
		a[i] = read(), a[n] ^= a[i];
	For (i, 1, n - 1)
		b[i] = read(), b[n] ^= b[i];

	For (i, 1, n)
		va[i] = a[i], vb[i] = b[i];
	sort(va + 1, va + n + 1);
	sort(vb + 1, vb + n + 1);

	For (i, 1, n)
		if(va[i] != vb[i]) return puts("-1"), 0;

	tot = unique(va + 1, va + n + 1) - va - 1;
	For (i, 1, tot) fa[i] = i;

	For (i, 1, n) {
		a[i] = find_pos(a[i]); b[i] = find_pos(b[i]);
		int rtx = find(a[i]), rty = find(b[i]);
		if (rtx != rty) fa[rtx] = rty;
	}

	int ans = 0, sum = 0;
	For (i, 1, n - 1) if (a[i] != b[i])
		++ ans, vis[a[i]] = vis[b[i]] = true;
	vis[a[n]] = vis[b[n]] = true;

	For (i, 1, n) if (vis[i] && find(i) == i) ++ sum;

	printf ("%d\n", ans + max(0, sum - 1));

	return 0;

}