#include <bits/stdc++.h>
#define N 1000500
using namespace std;
int a[N],b[N],c[N],d[N],fa[N],n,ans,tot;
map<int,int> mp;
int gf(int x) {return fa[x]==x ? x : fa[x] = gf(fa[x]);}
inline int rd() {int r;scanf("%d",&r);return r;}
int main() {
	n = rd();
	for (int _=1;_<=n;_++) a[_] = rd();
	for (int _=1;_<=n;_++) b[_] = rd();

	for (int _=1;_<=n;_++) a[n+1] ^= a[_], b[n+1] ^= b[_];
	++n;
	for (int _=1;_<=n;_++) c[_] = a[_], d[_] = b[_];

	sort(c+1,c+n+1); sort(d+1,d+n+1);
	for (int _=1;_<=n;_++) if (d[_]!=c[_]) return puts("-1"), 0;
	
	for (int _=1;_<=n;_++) if (a[_] != b[_] || _ == n) {
		_<n ? ++ans :0;
		!mp[ a[_] ] ? mp[ a[_] ] = ++tot :0;
		!mp[ b[_] ] ? mp[ b[_] ] = ++tot :0;
	}
	
	if (!ans) return puts("0"), 0;

	for (int _=1;_<=tot;_++) fa[_] = _;
	for (int _=1;_<n;_++) if (a[_] != b[_])
		fa[ gf( mp[a[_]] ) ] = gf( mp[b[_]] );
	for (int _=1;_<=tot;_++) ans += fa[_] == _;
	cout << ans-1 << endl;

	return 0;
}