// Slower C++ solution:
#include<cstdio>

#include<algorithm>

#include<map>

#include<vector>

using namespace std;



const int MAXN=1e5+5;



int n,ncnt,tot,ans;

int a[MAXN],b[MAXN];

int in[MAXN],out[MAXN];

bool tag[MAXN];

map<int,int> vis;

vector<int> E[MAXN];



void dfs(int u){

    tag[u]=1;

    for(int i=0;i<(int)E[u].size();i++){

        int v=E[u][i];

        if(tag[v]) continue;

        dfs(v);

    }

}



int main(){

    scanf("%d",&n);

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

        scanf("%d",&a[i]);

        tot^=a[i];

        if(vis.find(a[i])==vis.end()) vis[a[i]]=++ncnt;

        a[i]=vis[a[i]];

    }

    if(vis.find(tot)==vis.end()) vis[tot]=++ncnt;

    tot=vis[tot];

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

        scanf("%d",&b[i]);

        if(vis.find(b[i])==vis.end()) vis[b[i]]=++ncnt;

        b[i]=vis[b[i]];

    }

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

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

        ans++;

        E[b[i]].push_back(a[i]);

        in[a[i]]++,out[b[i]]++;

    }

    bool flag=0;

    for(int i=1;i<=ncnt;i++){

        if(in[i]==out[i]) continue;

        if(in[i]<out[i]){

            if(in[i]!=out[i]-1||i!=tot)

                return puts("-1"),0;

        }

        else{

            if(in[i]!=out[i]+1||flag) return puts("-1"),0;

            flag=1;

        }

    }

    dfs(tot);

    for(int i=1;i<=ncnt;i++){

        if(tag[i]||!out[i]) continue;

        ans++;

        dfs(i);

    }

    printf("%d\n",ans);

}


// Faster C++ solution for the same problem (measured speedup: 3.4826274037654517x):
#include<bits/stdc++.h>

clock_t t=clock();

namespace my_std{

	using namespace std;

	#define pii pair<int,int>

	#define fir first

	#define sec second

	#define MP make_pair

	#define rep(i,x,y) for (int i=(x);i<=(y);i++)

	#define drep(i,x,y) for (int i=(x);i>=(y);i--)

	#define go(x) for (int i=head[x];i;i=edge[i].nxt)

	#define templ template<typename T>

	#define sz 201001

	typedef long long ll;

	typedef double db;

	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

	templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}

	templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}

	templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}

	templ inline void read(T& t)

	{

		t=0;char f=0,ch=getchar();double d=0.1;

		while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();

		while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();

		if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}

		t=(f?-t:t);

	}

	template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}

	char __sr[1<<21],__z[20];int __C=-1,__zz=0;

	inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}

	inline void print(register int x)

	{

		if(__C>1<<20)Ot();if(x<0)__sr[++__C]='-',x=-x;

		while(__z[++__zz]=x%10+48,x/=10);

		while(__sr[++__C]=__z[__zz],--__zz);__sr[++__C]='\n';

	}

	void file()

	{

		#ifdef NTFOrz

		freopen("a.in","r",stdin);

		#endif

	}

	inline void chktime()

	{

		#ifdef NTFOrz

		cout<<(clock()-t)/1000.0<<'\n';

		#endif

	}

	#ifdef mod

	ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}

	ll inv(ll x){return ksm(x,mod-2);}

	#else

	ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}

	#endif

//	inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}

}

using namespace my_std;



int n,m;

int a[sz],b[sz],p[sz];

int cur,_cur;

int cnt[sz],cnt1[sz];



int fa[sz];

int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);}



int main()

{

	file();

	read(n);

	rep(i,1,n) read(a[i]),cur^=a[i];

	rep(i,1,n) read(b[i]);

	rep(i,1,n) if (a[i]!=b[i]) p[++m]=a[i],p[++m]=b[i];

	if (!m) return puts("0"),0;

	sort(p+1,p+m+1);m=unique(p+1,p+m+1)-p-1;

	int ans=0;

	rep(i,1,n) if (a[i]!=b[i]) ++ans;

	rep(i,1,n) cnt[a[i]=lower_bound(p+1,p+m+1,a[i])-p]++;

	rep(i,1,n) cnt1[b[i]=lower_bound(p+1,p+m+1,b[i])-p]++;

	if (*lower_bound(p+1,p+m+1,cur)==cur) ++cnt[lower_bound(p+1,p+m+1,cur)-p]; else ++ans;

	rep(i,1,n+n) if (cnt[i]<cnt1[i]) return puts("-1"),0;

	rep(i,1,m) fa[i]=i;

	rep(i,1,m) if (fa[i]==i) ++ans;--ans;

	rep(i,1,n) if (a[i]!=b[i]&&getfa(a[i])!=getfa(b[i]))

		--ans,fa[fa[a[i]]]=fa[b[i]];

//	m=0;rep(i,1,n) if (a[i]!=b[i]) p[++m]=a[i],p[++m]=b[i];

//	

	cout<<ans;

	return 0;

}