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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define N 100005
using namespace std;
int a[N],b[N],n,m,d[N],c[N],n1,f[N];
map <int,int> h;
bool bz[N];
int getf(int k)
{
	if(f[k]==0||f[k]==k) return k;
	return (f[k]=getf(f[k])); 
}
int main()
{
	cin>>n;
	int v=0;
	fo(i,1,n) scanf("%d",&a[i]),v^=a[i],c[i]=a[i];
	a[n+1]=c[n+1]=v;
	bool pd=0;
	v=0;
	fo(i,1,n) scanf("%d",&b[i]),d[i]=b[i],v^=b[i];
	b[++n]=d[n]=v;
	sort(c+1,c+n+1),sort(d+1,d+n+1);
	n1=0;
	fo(i,1,n) 
	{
		if(c[i]!=d[i]) 
		{
			printf("-1\n");
			return 0;
		}
		if(h.count(c[i])==0) h[c[i]]=++n1;
		if(h.count(d[i])==0) h[d[i]]=++n1; 
	}
	int ans=0;
	fo(i,1,n) 
	{
		a[i]=h[a[i]],b[i]=h[b[i]];
		if(a[i]!=b[i]) 
		{
			if(i<n) ans++;
			int fx=getf(a[i]),fy=getf(b[i]);
			f[fx]=fy;
			bz[fx]=1,bz[fy]=1;
		}
	}
	if(ans==0) printf("0\n");
	else
	{
		bz[a[n]]=bz[b[n]]=1;
		fo(i,1,n1) if(bz[i]&&getf(i)==i) ans++;
		printf("%d\n",ans-1);
	}
}