#include <cstdio>
#include <cctype>
#include <algorithm>
#define repu(i,x,y) for (int i=x; i<=y; ++i)
#define w(x) (x<=n?a[x]:b[x-n-1])
using namespace std;

int n,m,tot,a[100100],b[100100],c[200100],fa[200100],cnt[200100],ans;

int getint()
{
    char ch;
    while (!isdigit(ch=getchar()));
    int x=ch-'0';
    for (; isdigit(ch=getchar()); x=x*10+ch-'0');
    return x;
}

bool cmp(int x,int y)
{
    return w(x)<w(y);
}

int find(int x)
{
    if (fa[x]==x)
        return x;
    return fa[x]=find(fa[x]);
}

int main()
{
    n=getint();
    repu(i,1,n)
        a[0]^=a[i]=getint();
    repu(i,1,n)
        b[0]^=b[i]=getint();
    repu(i,0,n)
        if (!i || a[i]!=b[i])
            c[++tot]=i,c[++tot]=i+n+1;
    sort(c+1,c+1+tot,cmp);
    int lst=-1;
    repu(i,1,tot)
        m+=w(c[i])!=lst,lst=w(c[i]),w(c[i])=m;
    repu(i,1,m)
        fa[i]=i;
    repu(i,0,n)
        if (a[i]!=b[i])
        {
            ++cnt[a[i]],--cnt[b[i]];
            fa[find(a[i])]=find(b[i]);
        }
    ans=tot/2-2;
    repu(i,1,m)
    {
        ans+=fa[i]==i;
        if (cnt[i])
        {
            puts("-1");
            return 0;
        }
    }
    printf("%d\n",ans);
    return 0;
}