#include <bits/stdc++.h>
#define ll long long
using namespace std;

struct node {
    int x,id;
}ord[100005],ord2[100005];

int n,flag;
int fa[100005],a[100005],b[100005],c[100005],c2[100005],vis[100005],v[100005];

map<int,int> mp;

bool cmp(node a, node b) {
    return a.x<b.x;
}

int Find(int x){
    if(fa[x]!=x) fa[x]=Find(fa[x]);
    return fa[x];
}
void Unionn(int x,int y) {
    x=Find(x); y=Find(y);
    fa[x]=y;
}

void Discrete(){
    int cnt=1;
    sort(ord+1, ord+2+n, cmp);
    c[ord[1].id]=cnt++;
    for(int i=2; i<=n+1; i++) {
        if(ord[i].x==ord[i-1].x) cnt--;
        c[ord[i].id]=cnt++;
    }
    cnt=1;
    sort(ord2+1, ord2+2+n, cmp);
    c2[ord2[1].id]=cnt++;
    for(int i=2; i<=n+1; i++) {
        if(ord2[i].x==ord2[i-1].x) cnt--;
        c2[ord2[i].id]=cnt++;
    }
}

int main(void)
{   
    int ans=0;
    cin >>n;
    for(int i=1; i<=n; i++) {
        scanf("%d",&a[i]);
        ord[i]=(node){a[i],i};
        a[n+1]^=a[i];
        mp[a[i]]++;
    }
    mp[a[n+1]]++;
    ord[n+1]=(node){a[n+1],n+1};
    for(int i=1; i<=n; i++) {
        scanf("%d",&b[i]);
        ord2[i] =(node){b[i],i};
        b[n+1]^=b[i];
        mp[b[i]]--;
    }
    ord2[n+1]=(node){b[n+1],n+1};
    mp[b[n+1]]--;
    for(map<int,int>::iterator it=mp.begin(); it!=mp.end(); it++) {
        if(it->second !=0) {
            printf("-1\n");
            return 0;
        }
    }
    if(b[n+1]!=a[n+1]) ans++;
    Discrete();

    // for(int i=1; i<=n+1; i++) cout <<c[i]<<" ";
    // cout <<endl;
    // for(int i=1; i<=n+1; i++) cout <<c2[i]<<" ";
    // cout <<endl;

    int flag=1;
    vector<int> n1,n2;
    for(int i=1; i<=n+1; i++) {
        if(c[i]==c2[i]) continue;
        if(flag && c[n+1]==c2[i]) ans++,flag=0;
        n1.push_back(c[i]); n2.push_back(c2[i]);
    }
    for(int i=0; i<n1.size(); i++) fa[n1[i]]=n1[i];
    for(int i=0; i<n1.size(); i++) Unionn(n1[i], n2[i]);

        // for(int i=0; i<n1.size(); i++) cout<<n1[i]<<": "<<fa[n1[i]]<<endl;

    int cnt=0,local[100005];
    // memset(ig,0,sizeof(ig));
    for(int i=0; i<n1.size(); i++){ 
        if(fa[n1[i]]==n1[i] && !local[n1[i]]) {
            local[n1[i]]=1;
            cnt++;
        }
    }
    unique(n1.begin(), n1.end());
    int gg=0;
    for (int i=n1.begin();i<=n1.end();++i) ++gg;
    // cout <<gg<<" "<<ans<<" "<<cnt<<endl;
    printf("%d\n", gg-ans+cnt);

    return 0;
}
