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<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
typedef long long ll;
typedef double dd;
#define For(i,j,k) for (int i=(int)j;i<=(int)k;++i)
#define Forr(i,j,k) for (int i=(int)j;i>=(int)k;--i)
#define Set(a,p) memset(a,p,sizeof(a))
using namespace std;

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

const int maxn=1e5+1e2;
int n;
int Ans;
int a[maxn],b[maxn],c[maxn],d[maxn];
int fa[maxn];
map<int,int>e;

inline void file() {
	freopen("AGC016D.in","r",stdin);
	freopen("AGC016D.out","w",stdout);
}

inline int read() {
	int x=0,p=1;
	char c=getchar();
	while (!isdigit(c)) { if (c=='-') p=-1; c=getchar();}
	while (isdigit(c)) { x=(x<<1)+(x<<3)+(c-'0'); c=getchar();}
	return x*p;
}

int find(int x) {
	if (x!=fa[x]) fa[x]=find(fa[x]);
	return fa[x];
}

int main() {
//	file();

	n=read();
	For (i,1,n) a[i]=read(),a[n+1]^=a[i];
	For (i,1,n) b[i]=read(),b[n+1]^=b[i];
	int flag=1;
	For (i,1,n) if (a[i]!=b[i]) flag=0;
	if (flag) {
		puts("0"); return 0;
	}

	For (i,1,n+1) c[i]=a[i],d[i]=b[i];
	sort(c+1,c+n+2); sort(d+1,d+n+2);
	For (i,1,n+1) if (c[i]!=d[i]) {
		puts("-1"); return 0;
	}
	int N=unique(c+1,c+n+2)-(c+1);
	For (i,1,N) e[c[i]]=i; Set(d,0);
	For (i,1,N) fa[i]=i;
	
	For (i,1,n)
		if (a[i]!=b[i]) {
			int x=e[a[i]],y=e[b[i]]; d[e[a[i]]]=d[e[b[i]]]=1;
			x=find(x); y=find(y); ++Ans;
			if (x!=y) fa[x]=y;
		}
	
	For (i,1,N) if (d[i] && find(i)==i) ++Ans;
	printf("%d\n",Ans-1);
	return 0;
}