// 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.4297424844898345x):
//Be Name Khoda, Besmellah Rahmane Rahim, In The Name Of God;

//#include<bits/stdc++.h>

#include<iostream>

#include <vector>

#include <algorithm>

#include <cmath>

#include <set>

#include <queue>

#include <deque>

#include <map>

#include <stack>

#include<bitset>

#include<list>

#include<cassert>

#include<numeric>

#include <stdio.h>

#include <string.h>

#include<iomanip>

#include<unordered_map>

#include<unordered_set>



using namespace std; 





const int N = 1e5 + 5; 

int n;

int s[N];

int t[N];

int ans; 

int s2[N];

int t2[N]; 

map<int, int> cnt1;

map<int, int> cnt2;

map<int, int> par; 



int get(int x)

{

    if (par[x] == x)

    {

        return x; 

    }

    return par[x] = get(par[x]); 

}



void merge(int x, int y)

{

    x = get(x);

    y = get(y); 

    if (x != y)

    {

        par[y] = x; 

    }

}



int main()

{

    cin >> n; 

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

    {

        cin >> s[i]; 

    }

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

    {

        cin >> t[i]; 

    }

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

    {

        s[n] ^= s[i]; 

        t[n] ^= t[i]; 

    }

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

    {

        par[s[i]] = -1; 

        par[t[i]] = -1; 

    }

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

    { 

        cnt1[s[i]]++;

    }

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

    {

        cnt2[t[i]]++;

    }

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

    {

        if (cnt2[t[i]] != cnt1[t[i]])

        {

            cout << -1; 

            return 0; 

        }

    }

    bool check = true; 

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

    {

        if (s[i] != t[i]) 

        {

            check = false; 

        }

    }

    if (check)

    {

        cout << 0; 

        exit(0);

    }

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

    {

        if (s[i] != t[i] || i == n)

        {

            if (i != n) 

            {

                ans++; 

            }

            if (par[s[i]] == -1) 

            {

                par[s[i]] = s[i]; 

            }

            if (par[t[i]] == -1) 

            {

                par[t[i]] = t[i]; 

            }

            if (i != n) 

            {

                merge(s[i], t[i]); 

            }

        }

    }

    for (auto &it : par) 

    {

        if (get(it.first) == it.first) 

        {

            ans++; 

        }

    }

    cout << ans - 1; 

}