//2018-1-14
//miaomiao
//
#include <bits/stdc++.h>
using namespace std;

#define For(i, a, b) for(int i = (a); i <= (int)(b); ++i)
#define N (100000+5)

int a[N], b[N], m, f[N];
bool num[N];

map<int, int> ms, id;
int Find(int x){return x == f[x]? x: (f[x] = Find(f[x]));}

int main(){
	int n, axor = 0;
	
	scanf("%d", &n);
	For(i, 1, n){
		scanf("%d", &a[i]); axor ^= a[i];
		if(ms.count(a[i])) ++ms[a[i]]; else ms[a[i]] = 1;
	}
	if(ms.count(axor)) ++ms[axor]; else ms[axor] = 1;

	int ans = 0;
	For(i, 1, n){
		scanf("%d", &b[i]);
		if(!ms.count(b[i]) || !ms[b[i]]){puts("-1"); return 0;}
		--ms[b[i]]; if(b[i] != a[i]) ++ans;
	}
	
	For(i, 1, n) if(a[i] != b[i]){
		if(!id.count(a[i])) id[a[i]] = ++m;
		if(!id.count(b[i])) id[b[i]] = ++m;
	}
	if(!id.count(axor)) id[axor] = ++m;

	int u, v;
	For(i, 1, m) f[i] = i;
	For(i, 1, n) if(a[i] != b[i]){
		u = Find(id[a[i]]), v = Find(id[b[i]]);
		if(u != v) f[u] = v;
	}

	num[Find(id[axor])] = true;
	For(i, 1, m) if(!num[Find(i)]){
		num[Find(i)] = true; ++ans;
	}
	printf("%d\n", ans);

	return 0;
}