#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,t,tot,fflag;
int fa[MAXN],x[MAXN],y[MAXN],a[MAXN],b[MAXN];
int ct[MAXN];
struct node{int data,id;}c[MAXN<<1];
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(c+1,c+1+tot,cmp);
    c[0].data=-1;
    //For(i,1,n+1)printf("%d ",b[i]);puts("");
    For(i,1,tot){
        if(c[i].data!=c[i-1].data)++t;
        int x=c[i].id;
        if(x<=n+1)a[x]=t;
        else b[x-n-1]=t;
    }
    For(i,1,n+1)x[i]=a[i],y[i]=b[i];
    sort(x+1,x+2+n),sort(y+1,y+2+n);
    For(i,1,n)if(x[i]!=y[i])flag=1;
    if(flag)return;
    For(i,1,t)fa[i]=i;
    For(i,1,n){
        if(a[i]!=b[i])merge(b[i],a[i]),++cnt,++ct[a[i]];
    }
    if(a[n+1]!=b[n+1])merge(b[n+1],a[n+1]),++ct[a[n+1]];
}
int main(){
    read(n);
    For(i,1,n)read(a[i]),a[n+1]^=a[i],c[++tot]=(node){a[i],i};
    For(i,1,n)read(b[i]),b[n+1]^=b[i],c[++tot]=(node){b[i],i+n+1};
    c[++tot]=(node){a[n+1],n+1},c[++tot]=(node){b[n+1],2*(n+1)};
    unq();
    For(i,1,t)if(fa[i]==i&&ct[i])++cnt;
    if(a[n+1]==b[n+1]&&!ct[a[n+1]])++cnt,fflag=1;
    cnt--;
    if(flag)printf("-1\n");
    else {
        if(cnt>0)printf("%d\n",cnt);
    }
  return 0;
}