#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
constexpr int MOD=1000000007;
#define INF (1<<30)
#define LINF (lint)(1LL<<56)
#define endl "\n"
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define reprev(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define Flag(x) (1<<(x))
#define Flagcount(x) __builtin_popcount(x)
#define pint pair<int,int>
#define pdouble pair<double,double>
#define plint pair<lint,lint>
#define fi first
#define se second
typedef long long lint;
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
const int MAX_N=2e5+5;

bool reach[MAX_N];
vector<int> edge[MAX_N];

void dfs(int now){
    if(reach[now]) return;
    reach[now]=true;
    rep(i,edge[now].size()) dfs(edge[now][i]);
}

int main(void){
    int N;
    cin >> N;
    int A[N],B[N];
    rep(i,N) cin >> A[i];
    rep(i,N) cin >> B[i];
    int C[N+1]={};
    int D[N+1]={};
    rep(i,N) C[0]=C[0]^A[i],D[0]=D[0]^B[i];
    rep(i,N) C[i+1]=A[i],D[i+1]=B[i];
    int E[N+1],F[N+1];
    rep(i,N+1) E[i]=C[i],F[i]=D[i];
    rep(i,N) if(E[i+1]!=F[i+1] && F[i+1]==E[0]){
        swap(E[i+1],A[0]);
        break;
    }
    sort(C,C+N+1);
    sort(D,D+N+1);
    bool chk=true;
    rep(i,N+1) if(C[i]!=D[i]) chk=false;
    if(!chk){
        cout << -1 << endl;
        return 0;
    }
    sort(A,A+N);
    sort(B,B+N);
    bool m1=false;
    int m2=0;
    int base=N;
    bool m4=false;
    rep(i,N) if(A[i]!=B[i]) m1=true;
    if(E[0]!=F[0]){
        m4=true;
        rep(i,N) if(E[i+1]!=F[i+1] && F[i+1]==E[0]){
            swap(E[i+1],E[0]);
            break;
        }
    }
    map<int,int> place;
    rep(i,N+1) if(E[i]!=F[i]) place[E[i]]=i;
    rep(i,N+1){
        if(E[i]==F[i]) base--;
        else{
            edge[place[E[i]]].push_back(place[F[i]]);
            edge[place[F[i]]].push_back(place[E[i]]);
        }
        if(i==0 && E[i]==F[i]) base++;
    }
    int ans=base;
    rep(i,N+1){
        if(!reach[i]) if(E[i]!=F[i]) dfs(i),ans++;
    }
    rep(i,N) if(E[i+1]!=F[i+1] && F[i+1]==E[0]){
        ans--;
        break;
    }
    if(m4) ans++;
    cout << ans << endl;
}
