#define DEB
#include<bits/stdc++.h>
#define REP(i,m) for(int i=0;i<(m);++i)
#define REPN(i,m,in) for(int i=(in);i<(m);++i)
#define ALL(t) (t).begin(),(t).end()
#define CLR(a) memset((a),0,sizeof(a))
#define pb push_back
#define mp make_pair
#define fr first
#define sc second

using namespace std;


#ifdef DEB
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
#define dumpR(x) cerr<<"\x1b[31m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
#define dumpY(x) cerr<<"\x1b[33m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
#define dumpG(x) cerr<<"\x1b[32m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
#else
#define dump(x) ;
#define dumpR(x) ;
#define dumpY(x) ;
#define dumpG(x) ;
#define prl ;
template<class T> void debug(T a,T b){ ;}
#endif

template<class T> void chmin(T& a,const T& b) { if(a>b) a=b; }
template<class T> void chmax(T& a,const T& b) { if(a<b) a=b; }

typedef long long int lint;
typedef pair<int,int> pi;

namespace std{
  template<class S,class T>
  ostream &operator <<(ostream& out,const pair<S,T>& a){
    out<<'('<<a.fr<<','<<a.sc<<')';
    return out;
  }
}

//const int INF=5e8;
int n;
int ar[100005],br[100005];
void ng(){
  puts("-1");
  exit(0);
}


struct uf{
  static const int MAXN=100005;
  int par[MAXN];
  int size[MAXN];
  void init(){
    memset(par,-1,sizeof(par));
    REP(i,MAXN) size[i]=1;
  }
  int root(int a){
    if(par[a]==-1) return a;
    return par[a]=root(par[a]);
  }
  void unite(int a,int b){
    a=root(a);b=root(b);
    if(a==b) return;
    if(size[a]<size[b]) swap(a,b);

    par[b]=a;
    size[a]+=size[b];
  }
  bool same(int a,int b){
    return root(a)==root(b);
  }
};

uf u;
int mv[100005];
bool done[100005];
bool mark[100005];

int solve2(){
  u.init();
  set<pi> rest;
  int all=0;
  REP(i,n) all^=ar[i];
  REP(i,n) if(ar[i]!=br[i]) rest.insert({br[i],i});
  auto it=rest.begin();
  while(it!=rest.end()){
    int v=it->sc,val=it->fr;
    while(it!=rest.end() && it->fr==val) u.unite(it->sc,v),++it;
  }
  REP(i,n) if(ar[i]!=br[i]){
    auto it=rest.lower_bound(mp(ar[i],-1));
    if(it!=rest.end() && it->fr==ar[i]){
      mv[i]=it->sc;
      rest.erase(it);
    }else{
      mv[i]=-1;
    }
  }
  REP(i,n) if(mv[i]==-1){
    assert(rest.size()==1);
    mv[i]=rest.begin()->sc;
    assert(rest.begin()->fr==all);
  }
  REP(i,n) u.unite(i,mv[i]);
  int res=0;
  REP(i,n) if(br[i]==all) mark[u.root(i)]=1;
  REP(i,n) if(ar[i]!=br[i]) ++res;
  REP(i,n) if(u.root(i)==i && !mark[i]) ++res;
  return res;
}
int main(){
  cin>>n;
  REP(i,n) scanf("%d",&ar[i]);
  REP(i,n) scanf("%d",&br[i]);
  {
    bool suc=true;
    REP(i,n) if(ar[i]!=br[i]) suc=false;
    if(suc){
      puts("0");
      return 0;
    }
  }
  int all=0;
  REP(i,n) all^=ar[i];
  map<int,int> X,Y;
  REP(i,n) X[ar[i]]++;
  REP(i,n) X[br[i]]--;
  bool allzero=true;
  for(auto e:X) if(e.sc!=0) allzero=false;
  if(allzero){
    int res=solve2();
    cout<<res<<endl;
  }else{
    int p=-1,q=-1;
    for(auto e:X){
      if(e.sc!=0){
        Y[e.fr]+=e.sc;
        if(abs(e.sc)!=1) ng();
        if(e.sc>0) p=e.fr;
        else q=e.fr;
      }
    }
    if(Y.size()!=2) ng();
    if(q!=all) ng();
    int res=solve2();
    cout<<res<<endl;
  }
  return 0;
}





Measured memory_bytes: 11520