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>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=100010, LOG=20;

int n, m, k, u, v, x, y, t, a, b, ans;
int A[MAXN], B[MAXN], AA[MAXN], BB[MAXN];
int par[MAXN], mark[MAXN];

int get(int x){
	return (par[x]==x?x:par[x]=get(par[x]));
}
int join(int x, int y){
	x=get(x);
	y=get(y);
	par[x]=y;
	return x!=y;
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n;
	for (int i=1; i<=n; i++) cin>>A[i], A[0]^=A[i];
	for (int i=1; i<=n; i++) cin>>B[i], B[0]^=B[i];
	for (int i=0; i<=n; i++) AA[i]=A[i], BB[i]=B[i];
	sort(AA, AA+n+1);
	sort(BB, BB+n+1);
	for (int i=0; i<=n; i++) if (AA[i]!=BB[i]) kill(-1)
	m=unique(AA, AA+n+1)-AA;
	//debug(m)
	for (int i=0; i<=n; i++){
		A[i]=lower_bound(AA, AA+m, A[i])-AA;
		B[i]=lower_bound(AA, AA+m, B[i])-AA;
		if (A[i]==B[i] && i) continue ;
		mark[A[i]]=mark[B[i]]=1;
	}
	for (int i=0; i<m; i++) if (mark[i]) ans++, par[i]=i;
	for (int i=1; i<=n; i++) if (A[i]!=B[i]){
		ans++;
		ans-=join(A[i], B[i]);
	}
	ans--;
	cout<<ans<<'\n';
	
	
	return 0;
}