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: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;

int n, m, tot, sum, flag, t1 = -1, t2 = -1;
int a[N], b[N], arr[N], num[N], vis[N], now[N];
map<int, int> Map;
vector<int> vec[N];

void check() {
	for (int i = 1; i <= n; ++i)
		Map[a[i]]++;
	for (int i = 1; i <= n; ++i)
		Map[b[i]]--;
	map<int,int>::iterator it;
	for (it = Map.begin(); it != Map.end(); ++it) {
		if (it -> second > 0) {
			if (it -> second > 1) {
				puts("-1"), exit(0);
			}
			if (t1 == -1) t1 = it -> first;
			else puts("-1"), exit(0);
		}
		if (it -> second < 0) {
			if (it -> second < -1) {
				puts("-1"), exit(0);
			}
			if (sum != it -> first)
				puts("-1"), exit(0);
			if (t2 == -1) t2 = it -> first;
			else puts("-1"), exit(0);
		}
	}
	if (t1 != -1) {
		flag = 1;
		for (int i = 1; i <= n; ++i)
			if (b[i] == sum) {
				b[i] = t1;
				break;
			}
	}
}

void Solve() {
	for (int i = 1; i <= n; ++i) num[i] = a[i];
	for (int i = 1; i <= n; ++i) num[i + n] = b[i];
	sort(num + 1, num + n + n + 1);
	tot = unique(num + 1, num + n + n + 1) - num - 1;
	for (int i = 1; i <= n; ++i)
		a[i] = lower_bound(num + 1, num + tot + 1, a[i]) - num;
	for (int i = 1; i <= n; ++i)
		b[i] = lower_bound(num + 1, num + tot + 1, b[i]) - num;
	for (int i = 1; i <= n; ++i) {
		vec[b[i]].push_back(i);
	}
	for (int i = 1; i <= n; ++i) {
		arr[i] = vec[a[i]][now[a[i]]++];
	}
}

int dfs(int x) {
	vis[x] = 1;
	if (vis[arr[x]]) return 1;
	else return 1 + dfs(arr[x]);
}

int main() {
//	freopen("duliu.in", "r", stdin);
//	freopen("duliu.out", "w", stdout);
	cin >> n;
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[i]), sum ^= a[i];
	for (int i = 1; i <= n; ++i)
		scanf("%d", &b[i]);
	int m = 0;
	for (int i = 1; i <= n; ++i) {
		if (b[i] == sum) flag = 1;
		if (a[i] == b[i]) continue;
		++m;
		a[m] = a[i];
		b[m] = b[i];
	}
	n = m;
//	for (int i = 1; i <= n; ++i)
//		cout << a[i] << ' ' << b[i] << endl;
	check();
	Solve();
	int ans = 0;
	for (int i = 1; i <= n; ++i) {
		if (!vis[i]) ans += dfs(i) + 1;
	}
	cout << ans - flag << endl;
	return 0;
}