#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define ri register int
#define For(i,a,b) for(ri i=(a);i<=(b);++i)
#define DFor(i,a,b) for(ri i=(a);i>=(b);--i)
template<class T>inline T Max(register T a,register T b){return a>b?a:b;}
template<class T>inline T Min(register T a,register T b){return a<b?a:b;}
const int MAXN=100005;
int n,cnt,flag;
int fa[MAXN];
struct node{int data,id;}a[MAXN],b[MAXN];
template<class T>inline void read(T&x){
  char ch=getchar();x=0;
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
}
int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);}
inline void merge(int u,int v){
	int fu=getfa(u),fv=getfa(v);
	fa[fu]=fv;
}
inline bool cmp(const node n1,const node n2){return n1.data<n2.data;}
inline void unq(){
	sort(a+1,a+2+n,cmp);sort(b+1,b+2+n,cmp);
	For(i,1,n+1)fa[i]=i;
	For(i,1,n+1){
		if(a[i].id!=b[i].id)merge(a[i].id,b[i].id),++cnt;
		if(a[i].data!=b[i].data)flag=1;
	}
}
int main(){
	read(n);
	For(i,1,n)read(a[i].data),a[n+1].data^=a[i].data,a[i].id=i;
	For(i,1,n)read(b[i].data),b[n+1].data^=b[i].data,b[i].id=i;
	a[n+1].id=b[n+1].id=n+1;
	unq();
	For(i,1,n+1)if(fa[i]==i&&a[i].id!=b[i].id)++cnt;
	For(i,1,n+1)if(a[i].id==n+1&&a[i].id==b[i].id)++cnt;
	cnt-=2;
	if(flag)puts("-1");
	else {
		if(cnt!=-1)printf("%d\n",cnt);
	}
  return 0;
}