#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#define rep(a,b,c) for (int a=b;a<=c;a++)
#define per(a,b,c) for (int a=b;a>=c;a--)
#define go(u) for (int o=ft[u],v;v=E[o].t;o=E[o].n)
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> par;
const int N=400010;
int vis[N],ft[N],n,ans,sa,sb,tot,a[N],b[N],cnt[N];
map <int,int> mp;
map <int,int> :: iterator it;
struct edge{int t,n;}E[N<<2];
void dfs(int u){
	vis[u]=1;
	go(u) if (!vis[v]) dfs(v);
}
void add(int u,int v){
	E[++tot]=(edge){v,ft[u]},ft[u]=tot;
}
int main(){
//	freopen("t.txt","r",stdin);
	scanf("%d",&n);
	rep(i,1,n) scanf("%d",a+i),sa^=a[i],mp[a[i]];
	rep(i,1,n) scanf("%d",b+i),sb^=b[i],mp[b[i]];
	mp[sa],mp[sb];
	int __=0; for (it=mp.begin();it!=mp.end();it++) it->se=++__;
	rep(i,1,n) a[i]=mp[a[i]],b[i]=mp[b[i]];
	sa=mp[sa],sb=mp[sb];
	add(sa,sb),add(sb,sa);
	rep(i,1,n) add(a[i],b[i]),add(b[i],a[i]),ans-=a[i]==b[i];
	ans+=n;
	rep(i,1,n) if (a[i]!=b[i]) --cnt[b[i]];
	if (cnt[sa]) ans--;
	rep(i,1,n) if (a[i]!=b[i]) ++cnt[a[i]];
	++cnt[sa],--cnt[sb];
	rep(i,1,n*2+3) if (cnt[i]) return puts("-1"),0;
	rep(i,1,__) if (!vis[i]){
		go(i) if (v!=i) goto cc;
		continue;
	cc:	++ans;
		dfs(i);
	}
	printf("%d\n",ans);
	return 0;
}
