#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;
int bit=0;
int a[100001];
int b[100000];
map<int,int> ma,mb;

int calc(int key){

  set<P> st;
  
  for(int i=0;i<n;i++){
    if(a[i]!=b[i]){
      st.insert(P(b[i],i));
      assert( b[i]!=key );
    }
  }
  
  int res=0;
  
  for(int i=0;i<n;i++){
    if(a[i]==b[i])continue;
    
    res++;
    swap(a[i],key);

    while(1){
      set<P> :: iterator it=st.lower_bound( P(key,0) );
      if(it==st.end())break;
      if(it->first != key )break;
      if( a[it->second]==key ){
        st.erase(it);
        continue;
      }
      swap(key,a[it->second]);
      res++;
      st.erase(it);
    }
    
  }
  return res;
}

int solve(){
  for( auto p : mb ){
    int key=p.first;
    int value=p.second;
    if( ma[key] < value ){
      return -1;
    }
  }
  assert(0);
  set<P> st;
  for(int i=0;i<n;i++)
    st.insert( P(b[i],i) );

  int key=bit;
  int count=0;
  
  while(1){
    set<P> :: iterator it = st.lower_bound(P(key,0));
    
    if( it==st.end() )break;
    if( it->first != key ) break;
    
    if( a[it->second] == key ){
      st.erase(it);
      continue;
    }
    
    count++;    
    swap( a[it->second] , key );
    st.erase(it);

  }

  
  return calc(key)+count;
  
}

int main(){
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a[i];
    ma[ a[i] ]++;
    bit^=a[i];
  }
  ma[ bit ]++;
  
  for(int i=0;i<n;i++){
    cin>>b[i];
    mb[ b[i] ]++;
  }
  int ans=solve();
  cout<<ans<<endl;

  return 0;
}
