#include <bits/stdc++.h>
#define N 100005
using namespace std;
int bb[N],ct,*x[N];
bool cmp(int *a,int *b)
{
	return (*a) < (*b);
}
void pppd(int *a,int sz)
{
	for(int i = 1; i <= sz; i ++) x[i] = &a[i];
	ct = 0;
	sort(x + 1, x + sz + 1, cmp);
	bb[1] = ++ ct;
	for(int i = 2; i <= sz; i ++) bb[i] = *x[i] == *x[i-1] ? bb[i - 1]:++ ct;
	for(int i = 1; i <= sz; i ++) *x[i] = bb[i];
}
 
int n;
int fa[N];
int find(int x)
{
	return fa[x] == x? x:fa[x] = find(fa[x]);
}
void union(int x,int y)
{
	if(find(x) == find(y)) return;
	fa[find(x)] = find(y);
}
 
int a[N],b[N];
int c[N],d[N],m;
int main()
{
	scanf("%d",&n);
	int ta = 0, tb = 0;
	for(int i = 1; i <= n; i ++) scanf("%d", &a[i]), ta ^= a[i];
	for(int i = 1; i <= n; i ++) scanf("%d", &b[i]), tb ^= b[i];
	n ++;
	a[n] = ta;
	b[n] = tb;
	for(int i = 1; i <= n; i ++)
		if(a[i] != b[i])
			c[++ m] = a[i], d[m] = b[i];
	int fl1 = 0, fl2 = 0;
	if(a[n] == b[n]) fl1 = 1;
	for(int i = 1; i <= m; i ++) 
		if(c[i] == a[n]) fl2 = 1;
	sort(a + 1, a + n + 1);
	sort(b + 1, b + n + 1);
	for(int i = 1; i <= n; i ++) 
		if(a[i] != b[i]) 
			return puts("-1"), 0;
	
	int ans = 0;
	if(fl1 == 1 && fl2 == 1) ans --;
	if(fl1 == 0) ans -= 2;
	ans += m;
	
	pppd(c,m);
	pppd(d,m);
	for(int i = 1; i <= m; i ++) fa[i] = i;
	for(int i = 1; i <= m; i ++) union(c[i], d[i]);
	for(int i = 1; i <= ct; i ++) if(find(i) == i) ans ++;
	
	cout << ans << endl;
	return 0;
} 
