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;
/***********************************************/
/* Dear online judge:
 * I've read the problem, and tried to solve it.
 * Even if you don't accept my solution, you should respect my effort.
 * I hope my code compiles and gets accepted.
 *  ___  __     _______    _______      
 * |\  \|\  \  |\  ___ \  |\  ___ \     
 * \ \  \/  /|_\ \   __/| \ \   __/|    
 *  \ \   ___  \\ \  \_|/__\ \  \_|/__  
 *   \ \  \\ \  \\ \  \_|\ \\ \  \_|\ \ 
 *    \ \__\\ \__\\ \_______\\ \_______\
 *     \|__| \|__| \|_______| \|_______|
 */
const long long mod = 1000000007;

struct DSU{
	vector<int> p,sz;
	DSU(int N){
		p.resize(N);
		sz.resize(N,1);
		for(int i = 0;i < N;i++)p[i] = i;
	}
	int par(int x){
		if(x == p[x])
			return x;
		return p[x] = par(p[x]);
	}
	bool mrg(int x,int y){
		x = par(x),y = par(y);
		if(x == y)
			return false;
		if(sz[x] < sz[y]){
			swap(x,y);
		}
		p[y] = p[x];
		sz[x] += sz[y];
		return true;
	}
};

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	int N;
	cin>>N;
	vector<int> a(N),b(N),cpy;
	int Exor = 0;
	vector<int> all;
	for(int i = 0;i < N;i++) cin>>a[i],Exor ^= a[i],all.push_back(a[i]);
	all.push_back(Exor);
	
	for(int i = 0;i < N;i++) cin>>b[i];
	
	{ // determine NO
		cpy = b;
		sort(all.begin(),all.end());
		sort(cpy.begin(),cpy.end());
		bool can = true;
		for(int i = 0,j = 0;i < N;i++) {
			while(j < N + 1 && all[j] < cpy[i]) j++;
			if(j == N + 1) can = false;
			if(all[j] > cpy[i]) can = false;
			j++;
		}
		if(!can) {
			cout<<"-1\n";
			return 0;
		}
	}
	
	all.clear();
	all.push_back(Exor);
	for(int i = 0;i < N;i++) {
		if(a[i] != b[i]) all.push_back(a[i]),all.push_back(b[i]);
	}
	
	sort(all.begin(),all.end());
	auto it = unique(all.begin(),all.end());
	all.erase(it,all.end());
	DSU dsu(all.size());
	int com = all.size();
	cerr<<com<<'\n';
	int diff = 0;
	for(int i = 0;i < N;i++) {
		auto x = lower_bound(all.begin(),all.end(),a[i]) - all.begin();
		auto y = lower_bound(all.begin(),all.end(),b[i]) - all.begin();
		diff += x != y;
		com -= dsu.mrg(x,y);
	}
	cerr<<com<<'\n';
	if(com == 1) {
		cout<<diff<<'\n';
	} else {
		cout<<diff+1<<'\n';
	}
	return 0;
}