#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
multiset<int> A,B;
int n,a[N],b[N],c[N];
int rank(int x){
	int l=0,r=n;
	while (l<r){
		int mid=(l+r)>>1;
		if (c[mid]>=x) r=mid;else l=mid+1; 
	}
	return l;
}
//S[x]表示b中权为x的未匹配的位置 
set<int> S[N],T;
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		a[0]^=a[i];
	}
	for (int i=0;i<=n;i++)
		A.insert(a[i]),c[i]=a[i];
	for (int i=1;i<=n;i++){
		scanf("%d",&b[i]);
		b[0]^=b[i];
	}
	for (int i=0;i<=n;i++)
		B.insert(b[i]);
	if (A!=B) return puts("-1"),0;
	sort(c,c+n+1);
	for (int i=0;i<=n;i++){
		a[i]=rank(a[i]);
		b[i]=rank(b[i]);
		if (i&&a[i]!=b[i]){
			S[b[i]].insert(i);
			T.insert(i);
		}
	}
	int ans=0;
	while (!T.empty()){
		while (!S[a[0]].empty()){
			int p=*S[a[0]].begin();
			S[a[0]].erase(p);
			T.erase(p);
			swap(a[0],a[p]);
			ans++;
		}
		if (T.empty()) break;
		int p=*T.begin();
		swap(a[0],a[p]);
		ans++;
	}
	/*for (int i=0;i<=n;i++) printf("%d ",a[i]);puts("");
	for (int i=0;i<=n;i++) printf("%d ",b[i]);puts("");*/
	printf("%d\n",ans);
	return 0;
}