#include<bits/stdc++.h>
#include<tr1/unordered_map>
#define Rep(i,a,b) for(register int i=(a);i<=(b);++i)
#define Repe(i,a,b) for(register int i=(a);i>=(b);--i)
#define pb push_back
#define mp make_pair
#define Chkmax(a,b) a=a>b?a:b
#define Chkmin(a,b) a=a<b?a:b
#define mx(a,b) (a>b?a:b)
#define mn(a,b) (a<b?a:b)
typedef unsigned long long uint64;
typedef unsigned int uint32;
typedef long long ll;
using namespace std;

namespace IO
{
    const uint32 Buffsize=1<<15,Output=1<<23;
    static char Ch[Buffsize],*S=Ch,*T=Ch;
    inline char getc()
	{
		return((S==T)&&(T=(S=Ch)+fread(Ch,1,Buffsize,stdin),S==T)?0:*S++);
	}
    static char Out[Output],*nowps=Out;
    
    inline void flush(){fwrite(Out,1,nowps-Out,stdout);nowps=Out;}

    template<typename T>inline void read(T&x)
	{
		x=0;static char ch;T f=1;
		for(ch=getc();!isdigit(ch);ch=getc())if(ch=='-')f=-1;
		for(;isdigit(ch);ch=getc())x=x*10+(ch^48);
		x*=f;
	}

	template<typename T>inline void write(T x,char ch='\n')
	{
		if(!x)*nowps++='0';
		if(x<0)*nowps++='-',x=-x;
		static uint32 sta[111],tp;
		for(tp=0;x;x/=10)sta[++tp]=x%10;
		for(;tp;*nowps++=sta[tp--]^48);
		*nowps++=ch;
	}
}
using namespace IO;

inline void file()
{
#ifndef ONLINE_JUDGE
	FILE*DSD=freopen("duliu.in","r",stdin);
	FILE*FAKE=freopen("duliu.out","w",stdout);
#endif
}

const int MAXN=1e5+7;

static int n;

static int a[MAXN],b[MAXN];

inline void init()
{
	read(n);
	static int x;x=0;
	Rep(i,1,n)read(a[i]),x^=a[i];a[n+1]=x;
	x=0;
	Rep(i,1,n)read(b[i]),x^=b[i];b[n+1]=x;
}

static int ans,e;

static tr1::unordered_map<int,int>G;

static int fa[MAXN<<1];

inline int Find(int u){return u==fa[u]?u:fa[u]=Find(fa[u]);}

static int vis[MAXN<<1];

inline void solve()
{
	Rep(i,1,n+1)if(!G[a[i]])G[a[i]]=++e;
	Rep(i,1,n+1)if(!G[b[i]])G[b[i]]=++e;
	Rep(i,1,e)fa[i]=i;
	Rep(i,1,n)if(a[i]^b[i])
	{
		vis[G[a[i]]]=vis[G[b[i]]]=1,++ans;
		if(Find(G[a[i]])^Find(G[b[i]]))fa[fa[G[a[i]]]]=fa[G[b[i]]];
	}
	sort(a+1,a+n+2),sort(b+1,b+n+2);
	Rep(i,1,n+1)if(a[i]^b[i])return(void)puts("-1");
	if(!vis[G[a[n+1]]])++ans;
	Rep(i,1,e)if(i==Find(i)&&vis[i])++ans;
	printf("%d\n",ans-1);
}

int main()
{
    init();
    solve();
    return 0;
}
