#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline ll read(){
  char ch;ll f=1;
  while((ch=getchar())<'0'||ch>'9')
   if(ch=='-')f=-1;
  ll res=ch-48;
  while((ch=getchar())>='0'&&ch<='9')
   res=res*10+ch-48;
  return res*f;
} 
const int maxn=100005;
ll n,ans,v,m;
ll a[maxn],b[maxn],c[maxn],d[maxn],fa[maxn];
bool book[maxn];
ll getfa(ll x){
 if(fa[x]==0)return x;
 if(x!=fa[x])fa[x]=getfa(fa[x]);
 return fa[x];
}
map<ll,ll>mp;
int main(){
 n=read();
 for(ll i=1;i<=n;i++)a[i]=read(),v^=a[i],c[i]=a[i];
 a[n+1]=c[n+1]=v;v=0;
 for(ll i=1;i<=n;i++)b[i]=read(),v^=b[i],d[i]=b[i];
 b[++n]=d[n]=v;sort(c+1,c+1+n);sort(d+1,d+1+n);
 for(ll i=1;i<=n;i++){
  if(c[i]!=d[i]){puts("-1");return 0;}
  if(mp[c[i]]==0)mp[c[i]]=++m;
 }
 for(ll i=1;i<=n;i++){
  a[i]=mp[a[i]],b[i]=mp[b[i]];
  if(a[i]!=b[i]){
   if(i<n)ans++;
   ll fx=getfa(a[i]),fy=getfa(b[i]);
   fa[fx]=fy;book[a[i]]=1;book[b[i]]=1;
  }
 }
 if(ans==0)puts("0");
 else{
  book[a[n]]=book[b[n]]=1;
  for(ll i=1;i<=n;i++)
   if(book[i]&&i==getfa(i))
    ans++;
  cout<<ans-1;
 }
 return 0;
}
