#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
 
using namespace std;
 
const int N=100005;
 
int n,s1[N],m1,s2[N],m2,vec[N];
int A[N],B[N],ans,fa[N],m,sz[N];
bool fl[N];
map<int,int> D;

inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
inline void link(int u,int v){if(find(u)!=find(v))sz[find(v)]+=sz[find(u)],fa[find(u)]=find(v);}
 
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&s1[i]),m1^=s1[i],D[s1[i]]++;
	D[m1]++;
	for(int i=1;i<=n;i++)scanf("%d",&s2[i]),m2^=s2[i],D[s2[i]]--;
	for(auto&i:D)if(i.se<0)puts("-1"),exit(0);
	s1[0]=m1;s2[0]=m2;
	memcpy(vec,s1,(n+1)<<2);
	sort(vec,vec+n+1);
	m=unique(vec,vec+n+1)-vec;
	for(int i=0;i<m;i++)fa[i]=i,sz[i]=1;
	for(int i=0;i<=n;i++){
		A[i]=lower_bound(vec,vec+m,s1[i])-vec;
		B[i]=lower_bound(vec,vec+m,s2[i])-vec;
		if(A[i]!=B[i])link(A[i],B[i]),ans++;
	}
	if(!ans)puts("0"),exit(0);
	if(A[0]!=B[0])ans--;
	for(int i=0;i<m;i++){
		if(fa[i]==i&&sz[i]>1){
			fl[find(i)]=true;
			if(find(i)!=find(A[0])&&find(i)!=find(B[0]))ans++;
		}
	}
	printf("%d\n",ans);
	return 0;
}