#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define N 100005
using namespace std;
int a[N],b[N],n,m,d[N],c[N],n1,f[N];
map <int,int> h;
bool bz[N];
int getf(int k)
{
    if(f[k]==0||f[k]==k) return k;
    return (f[k]=getf(f[k])); 
}
int main()
{
    cin>>n;
    int v=0;
    fo(i,1,n) scanf("%d",&a[i]),v^=a[i],c[i]=a[i];
    a[n+1]=c[n+1]=v;
    bool pd=0;
    v=0;
    fo(i,1,n) scanf("%d",&b[i]),d[i]=b[i],v^=b[i];
    b[++n]=d[n]=v;
    sort(c+1,c+n+1),sort(d+1,d+n+1);
    n1=0;
    fo(i,1,n) 
    {
        if(c[i]!=d[i]) 
        {
            printf("-1\n");
            return 0;
        }
        if(h.count(c[i])==0) h[c[i]]=++n1;
        if(h.count(d[i])==0) h[d[i]]=++n1; 
    }
    int ans=0;
    fo(i,1,n) 
    {
        a[i]=h[a[i]],b[i]=h[b[i]];
        if(a[i]!=b[i]) 
        {
            if(i<n) ans++;
            int fx=getf(a[i]),fy=getf(b[i]);
            f[fx]=fy;
            bz[fx]=1,bz[fy]=1;
        }
    }
    if(ans==0) printf("0\n");
    else
    {
        bz[a[n]]=bz[b[n]]=1;
        fo(i,1,n1) if(bz[i]&&getf(i)==i) ans++;
        printf("%d\n",ans-1);
    }
}