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++ (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>
#include <queue>
using namespace std;
typedef long long LL;

LL n,m,k,tot;
LL a[500005],b[500005],c[500005],d[500005];
LL vis[500005] = {0};
string s;
map <LL,LL> mp;

LL fa[500005];
LL getf(LL x){
	if(fa[x] == x) return x;
	return fa[x] = getf(fa[x]);
}
void unionn(LL x,LL y){
	x = getf(x); y = getf(y);
	if(x == y) return;
	tot --; fa[x] = y;
}

int main(){
	cin >> n;
	for(LL i = 1;i <= n;i ++){ cin >> a[i]; c[i] = a[i]; a[n + 1] ^= a[i]; }
	for(LL i = 1;i <= n;i ++){ cin >> b[i]; d[i] = b[i]; b[n + 1] ^= b[i]; }
	n ++; c[n] = a[n]; d[n] = b[n];
	sort(c + 1,c + 1 + n); sort(d + 1,d + 1 + n);
	for(LL i = 1;i <= n;i ++){
		if(c[i] != d[i]){
			cout << -1 << endl;
			return 0;
		}
	}
	
	tot = 0;
	for(LL i = 1;i < n;i ++){
		if(a[i] != b[i]){
			if(mp.find(a[i]) == mp.end()) mp[a[i]] = ++ tot;
			if(mp.find(b[i]) == mp.end()) mp[b[i]] = ++ tot;
		}
	}
	if(mp.find(a[n]) == mp.end()) mp[a[n]] = ++ tot;
	if(mp.find(b[n]) == mp.end()) mp[b[n]] = ++ tot;
	for(LL i = 1;i <= n;i ++){
		a[i] = mp[a[i]];
		b[i] = mp[b[i]];
	}
	
//	for(LL i = 1;i <= n;i ++) cout << a[i] << (i == n ? '\n' : ' ');
//	for(LL i = 1;i <= n;i ++) cout << b[i] << (i == n ? '\n' : ' ');
	
	for(LL i = 1;i <= tot;i ++) fa[i] = i;
	for(LL i = 1;i < n;i ++){
		if(a[i] != b[i]){
			tot ++;
			unionn(a[i],b[i]);
			vis[a[i]] = 1; vis[b[i]] = 1;
		}
	}
	unionn(a[n],b[n]);
	if(vis[a[n]]) tot --;
	cout << tot << endl;
	return 0;
}