// Slower C++ solution:
#include<cstdio>

#include<algorithm>

#include<map>

#include<vector>

using namespace std;



const int MAXN=1e5+5;



int n,ncnt,tot,ans;

int a[MAXN],b[MAXN];

int in[MAXN],out[MAXN];

bool tag[MAXN];

map<int,int> vis;

vector<int> E[MAXN];



void dfs(int u){

    tag[u]=1;

    for(int i=0;i<(int)E[u].size();i++){

        int v=E[u][i];

        if(tag[v]) continue;

        dfs(v);

    }

}



int main(){

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d",&a[i]);

        tot^=a[i];

        if(vis.find(a[i])==vis.end()) vis[a[i]]=++ncnt;

        a[i]=vis[a[i]];

    }

    if(vis.find(tot)==vis.end()) vis[tot]=++ncnt;

    tot=vis[tot];

    for(int i=1;i<=n;i++){

        scanf("%d",&b[i]);

        if(vis.find(b[i])==vis.end()) vis[b[i]]=++ncnt;

        b[i]=vis[b[i]];

    }

    for(int i=1;i<=n;i++){

        if(a[i]==b[i]) continue;

        ans++;

        E[b[i]].push_back(a[i]);

        in[a[i]]++,out[b[i]]++;

    }

    bool flag=0;

    for(int i=1;i<=ncnt;i++){

        if(in[i]==out[i]) continue;

        if(in[i]<out[i]){

            if(in[i]!=out[i]-1||i!=tot)

                return puts("-1"),0;

        }

        else{

            if(in[i]!=out[i]+1||flag) return puts("-1"),0;

            flag=1;

        }

    }

    dfs(tot);

    for(int i=1;i<=ncnt;i++){

        if(tag[i]||!out[i]) continue;

        ans++;

        dfs(i);

    }

    printf("%d\n",ans);

}


// Faster C++ solution for the same problem (measured speedup: 3.484925791931266x):
#include<bits/stdc++.h> //Ithea Myse Valgulious

namespace chtholly{

typedef long long ll;

#define re0 register int

#define rel register ll

#define rec register char

#define gc getchar

//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?-1:*p1++)

#define pc putchar

#define p32 pc(' ')

#define pl puts("")

/*By Citrus*/

char buf[1<<23],*p1=buf,*p2=buf;

inline int read(){

  int x=0,f=1;char c=gc();

  for (;!isdigit(c);c=gc()) f^=c=='-';

  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');

  return f?x:-x;

  }

template <typename mitsuha>

inline bool read(mitsuha &x){

  x=0;int f=1;char c=gc();

  for (;!isdigit(c)&&~c;c=gc()) f^=c=='-';

  if (!~c) return 0;

  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');

  return x=f?x:-x,1;

  }

template <typename mitsuha>

inline int write(mitsuha x){

  if (!x) return 0&pc(48);

  if (x<0) pc('-'),x=-x;

  int bit[20],i,p=0;

  for (;x;x/=10) bit[++p]=x%10;

  for (i=p;i;--i) pc(bit[i]+48);

  return 0;

  }

inline char fuhao(){

  char c=gc();

  for (;isspace(c);c=gc());

  return c;

  }

}using namespace chtholly;

using namespace std;

const int yuzu=2e5;

typedef int fuko[yuzu|10];

fuko a,b,ta,tb,vis;

namespace dsu{

fuko fa;

void init(int n){for (int i=0;i<=n;++i) fa[i]=i;}

int find(int x){return x^fa[x]?fa[x]=find(fa[x]):x;}

int mg(int u,int v){

  int fu=find(u),fv=find(v);

  return fu^fv?fa[fu]=fv,1:0;

  }

}using dsu::find;



int main() {

int i,n=read();

for (i=1;i<=n;++i) *a^=a[i]=ta[i]=read();

for (i=1;i<=n;++i) *b^=b[i]=tb[i]=read();

*ta=*a,*tb=*b,sort(ta,ta+n+1),sort(tb,tb+n+1);

for (i=0;i<=n;++i) if (ta[i]^tb[i]) return write(-1);

int m=unique(ta,ta+n+1)-ta;

for (i=0;i<=n;++i) 

  a[i]=lower_bound(ta,ta+m,a[i])-ta,

  b[i]=lower_bound(ta,ta+m,b[i])-ta;

dsu::init(m);

int llx=0;

for (i=0;i<=n;++i) dsu::mg(a[i],b[i]),llx+=a[i]!=b[i]&&i;

for (i=0;i<m;++i) if (find(i)^i) vis[find(i)]=1;

for (i=0;i<m;++i) if (find(i)==i) llx+=vis[i]&&i!=find(*a);

write(llx);

}