// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



const int N = 1e5 + 5;



inline long long read()

{

    long long f = 1, sum = 0;  char ch = getchar();

    while ( !isdigit(ch) ) { if ( ch == '-' ) f = -1; ch = getchar(); }

    while ( isdigit(ch) ) { sum = (sum<<1) + (sum<<3) + (ch^48); ch = getchar(); }

    return f * sum;

}



int n, sa, sb;

int a[N], b[N], ta[N], tb[N], d[N], fa[N];

map < int, int > q;



inline int find ( register int x )

{

    return fa[x] == x ? fa[x] : fa[x] = find ( fa[x] );

}



inline int unionn ( register int x, register int y )

{

    fa[x] = y;

}



int main()

{

    n = read();

    for ( register int i = 1 ; i <= n ; i ++ )  a[i] = read(), sa ^= a[i]; a[n+1] = sa;

    for ( register int i = 1 ; i <= n ; i ++ )  b[i] = read(), sb ^= b[i]; b[n+1] = sb;

    n ++;

    memcpy ( ta, a, sizeof(a) ), memcpy ( tb, b, sizeof(b) );

    sort ( ta + 1, ta + n + 1 ), sort ( tb + 1, tb + n + 1 );

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

        if ( ta[i] != tb[i] )

            return puts ( "-1" ), 0;

    register int ans = 0, num = 0;

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

        if ( a[i] != b[i] || i == n )

        {

            d[++num] = a[i], d[++num] = b[i];

            if ( i < n )  ans ++;

        }

    if ( ans == 0 )  return puts ( "0" ), 0;

    sort ( d + 1, d + num + 1 );

    num = unique ( d + 1, d + num + 1 ) - d - 1;

    for ( register int i = 1 ; i <= num ; i ++ )  fa[i] = i;

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

        if ( a[i] != b[i] )

        {

            a[i] = lower_bound ( d + 1, d + num + 1, a[i] ) - d;

            b[i] = lower_bound ( d + 1, d + num + 1, b[i] ) - d;

            if ( !q[a[i]] )  q[a[i]] = 1;

            if ( !q[b[i]] )  q[b[i]] = 1;

            unionn ( find ( a[i] ), find ( b[i] ) );

        }

    for ( register int i = 1 ; i <= num ; i ++ )

        if ( fa[i] == i )

            ++ ans;

    printf ( "%d", ans - 1 );

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<iostream>

#include<cstring>

#include<cmath>

#include<algorithm>

#define N 262147

#define ll long long

#define reg register

#define p 998244353

using namespace std;



int fa[N],a[N],b[N],sa[N],sb[N],lsh[N];

int n,cnt,ans = -1;



inline int find(int x){

    while(x!=fa[x]) x = fa[x] = fa[fa[x]];

    return x;

}



inline void merge(int u,int v){

    int fu = find(u),fv = find(v);

    if(fu!=fv) fa[fu] = fv;

}



inline int qwq(int x){

    return lower_bound(lsh+1,lsh+1+cnt,x)-lsh;

}



int main(){

    scanf("%d",&n);

    for(reg int i=1;i<=n;++i) scanf("%d",&a[i]),a[n+1] ^= a[i];

    for(reg int i=1;i<=n;++i) scanf("%d",&b[i]),b[n+1] ^= b[i];

    ++n;

    memcpy(sa+1,a+1,n<<2);

    memcpy(sb+1,b+1,n<<2);

    sort(sa+1,sa+n+1);

    sort(sb+1,sb+n+1);

    for(reg int i=1;i!=n;++i){

        if(sa[i]==sb[i]) continue;

        printf("-1");

        return 0;

    }

    for(reg int i=1;i<=n;++i){

        fa[i] = i;

        if(a[i]==b[i]&&i!=n) continue;

        lsh[++cnt] = a[i];

        lsh[++cnt] = b[i];

        ++ans;

    }

    sort(lsh+1,lsh+1+cnt);

    cnt = unique(lsh+1,lsh+1+cnt)-lsh-1;

    for(reg int i=1;i<=n;++i){

        if(a[i]==b[i]) continue;

        merge(qwq(a[i]),qwq(b[i]));

    }

    for(reg int i=1;i<=cnt;++i) ans += fa[i]==i;

    printf("%d",ans-1);

	return 0;	

}

// Fastest known solution for this problem: