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 <cstring>
#include <algorithm>
#define rep(i,l,r) for (int i=l;i<=r;++i) 
#define mp(x,y) make_pair(x,y)
#define ft first
#define sd second 
#include <map>
#include <string>
#include <iostream>
#define GG return printf("-1"),0;
using namespace std;
const int N=1e5+5;
void read(int &x)
{
	x=0;
	char ch=getchar();int f=1;
	while (ch<'0'||ch>'9') ch=='-'?f=-1:0,ch=getchar();
	while ('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=getchar();
	x*=f;return ;
}

int n,a[N],b[N],c[N],d[N];
int tmp,cnt;
int zzk;
int check(int x)
{
	return (upper_bound(d+1,d+1+n,x)-d)-(lower_bound(d+1,d+1+n,x)-d)>0;
}

struct xx
{
	int V,next;
}B[N<<2];
int head[N],bel[N],G;
void add(int x,int y)
{
	B[++G]=(xx){y,head[x]};
	head[x]=G;
}

#define F(x) for (int i=head[x];i;i=B[i].next)
#define v B[i].V
int dfn[N],low[N],T,In[N],st[N];
void dfs(int x)
{
	low[x]=dfn[x]=++T;
	st[++st[0]]=x;In[x]=1;
	F(x) if (!dfn[v]) dfs(v),low[x]=min(low[x],low[v]);
	else if (In[v]) low[x]=min(low[x],dfn[v]);
	#define now st[st[0]]
	if (dfn[x]==low[x])
	{
		for (++cnt;now^x;--st[0]) In[now]=0,bel[now]=cnt;
		bel[x]=cnt,--st[0],In[x]=0;
	}
	return ;
}
int ans;
int gg;
map <int,int>ma,mb;
int main()
{
	read(n);
	rep(i,1,n) read(a[i]),++ma[a[i]],tmp^=a[i],c[i]=a[i];
	c[n+1]=a[n+1]=tmp;sort(c+1,c+1+n+1);
	tmp=0;
	rep(i,1,n) read(b[i]),++mb[b[i]],tmp^=b[i],d[i]=b[i];
	b[n+1]=d[n+1]=tmp;sort(d+1,d+1+n+1);
	rep(i,1,n+1) if (c[i]^d[i]) GG;
	zzk=unique(c+1,c+1+n+1)-c;
	tmp=lower_bound(c+1,c+zzk,tmp)-c;
	rep(i,1,n+1) a[i]=lower_bound(c+1,c+zzk,a[i])-c;
	rep(i,1,n) b[i]=lower_bound(c+1,c+zzk,b[i])-c;
	rep(i,1,n) if (a[i]!=b[i]) add(a[i],b[i]);
	if (!G) return printf("0"),0;
	cnt=0;
	for (int i=1;i<zzk;++i)
	  if (head[i]&&!bel[i]) 
	    dfs(i);
	ans=cnt;
	rep(i,1,n) if (a[i]!=b[i]&&bel[a[i]]==bel[b[i]]) 
	  ++ans;
	return printf("%d",ans-(bel[a[n+1]]?1:0)),0;
}