#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define fo(i,a,b) for(int i(a),_E_(b);i<=_E_;++i)
#define REP(i,a,b) for(int i(a),_E_(b);i<_E_;++i)
#define fd(i,a,b) for(int i(a),_E_(b);i>=_E_;--i)
#define pu putchar
#define ge getchar
#define ll long long
#define ul unsigned long long
#define ui unsigned int
#define db double
#define ld long double
#define N 200010
#define L 3189539

using namespace std;

int a[N],n,b[N],hash[L],num[N],cnt,in[N],out[N],fir[N],nex[N],to[N],top;
int ans,vis[N],pass[N];

#define link(x,y) to[++top]=y,nex[top]=fir[x],fir[x]=top,++in[y],++out[x]
int get(int a){
	int y=a%L;
	while(hash[y] && num[hash[y]]!=a)y=(y+1)%L;
	return y;
}
int node(int a){
	int x=get(a);if(!hash[x])num[hash[x]=++cnt]=a;return hash[x];
}

void run(int x){
	vis[x]=1;
	for(int i=fir[x];i;i=nex[i])if(!pass[i])pass[i]=1,run(to[i]);
}

int main(){
	//freopen("duliu.in","r",stdin);
	//freopen("duliu.out","w",stdout);
	scanf("%d",&n);
	int x=0;
	fo(i,1,n)scanf("%d",a+i),x^=a[i];x^=a[n+1]=x;
	fo(i,1,n)scanf("%d",b+i),x^=b[i];x^=b[n+1]=x;
	fo(i,1,++n){
		int u=node(a[i]),v=node(b[i]);if(u^v)link(u,v),++ans;
	}if(a[n]^b[n])ans-=2;
	fo(i,1,cnt)if(in[i]!=out[i])x=1;
	top=0;
	if(x)printf("-1");else{
		fo(i,1,n)
		if(!vis[i] && in[i])++ans,run(i);
		printf("%d",ans);
	}
	fclose(stdin);fclose(stdout);
	return 0;
}