#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define Re return
#define In inline
#define inc(l, i, r) for(i=l; i<r; ++i)


const int mxn=1<<17;

int n, a[mxn], b[mxn], f[mxn];
#define Pair pair<int, int>
#define fst first
#define snd second
#define mP make_pair
#define lB lower_bound
Pair a1[mxn], b1[mxn];
char bo[mxn], vis[mxn];

int get(int x){Re f[x]? f[x]=get(f[x]): x;}

In void link(int a, int b)
{
//    printf("%d %d\n", a, b);
    if(get(a+1)!=get(b+1))
        f[get(a+1)]=get(b+1);
}

int main()
{
    int i, j, k, l;
    scanf("%d", &n);
    inc(0, i, n)
        scanf("%d", a+i), a[n]^=a[i];
    inc(0, i, n)
        scanf("%d", b+i), b[n]^=b[i];
    k=n-1;
    inc(j=0, i, n+1)
        if(i<n && a[i]==b[i]) bo[i]=1, --k;
        else
            a1[j  ]=mP(a[i], i),
            b1[j++]=mP(b[i], i);
    
    sort(a1, a1+j), sort(b1, b1+j);
    inc(0, i, j)
        if(a1[i].fst!=b1[i].fst)
            goto NO;

    inc(0, i, n+1)
        if(!bo[i])
            link(i, lB(b1, b1+j, mP(a[i], 0))->snd);
    inc(0, i, j-1)
        if(b1[i].fst==b1[i+1].fst)
            link(b1[i].snd, b1[i+1].snd);

    inc(0, i, n+1)
        if(!bo[i] && !f[i+1])
            ++k;
    
    printf("%d\n", k);
    Re 0;
NO: Re puts("-1"), 0;
}
