#include<bits/stdc++.h>
using namespace std;
#define maxn 200008
#define int long long
int n;
map<int,int> vis;
map<int,int> tar;
bool ok[maxn];
int ans=0;
bool check[maxn];
struct node{
    int a,b,pos;
    bool operator > (const node & x)const{
        return a>x.a;
    }
    node operator = (const node & x){
        a=x.a;
        b=x.b;
        pos=x.pos;
        return *this;
    }
}arr[maxn];
node c[maxn];
bool cmp(node x,node y){
    return x.b<y.b;
}
void mergesort(int l,int r){
    if(r>l){
        int mid=(l+r)/2;
        mergesort(l,mid);
        mergesort(mid+1,r);
        int tmp=l;
        int i=l,j=mid+1;
        for(;i<=mid&&j<=r;){
            if(arr[i]>arr[j]){
                c[tmp++]=arr[j++];
            }else{
                c[tmp++]=arr[i++];
            }
        }
        while(i<=mid)c[tmp++]=arr[i++];
        while(j<=r)c[tmp++]=arr[j++];
        for(int i=l;i<=r;i++)arr[i]=c[i];
    }
}
bool dfs(int a){
    if(check[arr[a].pos])return true;
    check[a]=1;
    return dfs(arr[a].pos);
}
main(){
 //   freopen("duliu.in","r",stdin);
  //  freopen("duliu.out","w",stdout);
    scanf("%lld",&n);
    int sum=0;
    for(int i=1;i<=n;i++){
        scanf("%lld",&arr[i].a);
        vis[arr[i].a]++;
        sum^=arr[i].a;
    }
    vis[sum]++;
    int ans=0;
    int cnt=0;
    for(int i=1;i<=n;i++){
        scanf("%lld",&arr[i].b);
        tar[arr[i].b]++;
    }
    map<int,int>::iterator iter;
    for(iter=tar.begin();iter!=tar.end();iter++){
        if(vis[iter->first]<iter->second){
            cout<<-1<<endl;
            return 0;
        }
    }
    while(vis[sum]-tar[sum]==0){
        for(int i=1;i<=n;i++){
            if(arr[i].b==sum&&arr[i].a!=sum){
                int tmp=arr[i].a;
                arr[i].a=sum;
                sum=tmp;
                ans++;
            }
        }
    }
    int tot=0;
    sort(arr+1,arr+1+n,cmp);
    for(int i=1;i<=n;i++){
        arr[i].pos=i;
        if(arr[i].a!=arr[i].b)tot++;
    }
    mergesort(1,n);
    for(int i=1;i<=n;i++){
        if(dfs(i)&&!check[i]){
            cnt++;
        }
    }
    cout<<(tot-cnt)*2+ans<<endl;
    return 0;
}