#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

const int N=1e5+5;

map<int,int> h;
int n,a[N],b[N],f[N],cnt[N],x,tot,ans;

int get(int x) {return f[x]?f[x]=get(f[x]):x;}

void merge(int x,int y) {
	x=get(x);y=get(y);
	if (x==y) return;
	f[y]=x;
}

int main() {
	scanf("%d",&n);
	x=0;fo(i,1,n) scanf("%d",&a[i]),x^=a[i];a[n+1]=x;
	x=0;fo(i,1,n) scanf("%d",&b[i]),x^=b[i];b[n+1]=x;
	n++;
	fo(i,1,n) 
		if (a[i]!=b[i]||i==n) {
			if (!h[a[i]]) h[a[i]]=++tot;
			cnt[h[a[i]]]++;
		}
	fo(i,1,n) 
		if (a[i]!=b[i]||i==n) 
			cnt[h[b[i]]]--;
	fo(i,0,tot) 
		if (cnt[i]) {
			puts("-1");
			return 0;
		}
	fo(i,1,n) a[i]=h[a[i]],b[i]=h[b[i]];
	fo(i,1,n) 
		if (a[i]!=b[i]) {
			if (i<n) ans++;
			merge(a[i],b[i]);
		}
	fo(i,1,tot) if (get(i)==i) ans++;
	printf("%d\n",max(ans-1,0));
	return 0;
}