#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,b,a) for(int i=b;i>=a;i--)
#define mset(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
typedef double db;
char ch;
int read(){int n=0,p=1;for(ch=getchar();ch<'0' || '9'<ch;ch=getchar())if(ch=='-') p=-1;for(;'0'<=ch && ch<='9';ch=getchar()) n=n*10+ch-'0';return n*p;}
const int N=1e5+5;
int n,a[N],b[N];
map<int,int> ok,cb,fa,exi;
int getfa(int x){return fa[x]?fa[x]=getfa(fa[x]):x;}
int main()
{
	n=read();
	int x=0;
	fo(i,1,n) a[i]=read(),x^=a[i];
	a[0]=x;
	x=0;
	fo(i,1,n) b[i]=read(),x^=b[i];
	b[0]=x;
	fo(i,0,n) ok[a[i]]++,cb[b[i]]++;
											bool same=1;
											fo(i,1,n) if(a[i]!=b[i]) same=0;
											if(same) return puts("0"),0;
	fo(i,0,n)
		if(ok[a[i]]!=cb[a[i]]) return puts("-1"),0;
	int ans=0;
	fo(i,0,n) if(a[i]!=b[i])
	{
		exi[a[i]]=1;
		if(i) ++ans;
		int f1=getfa(a[i]),f2=getfa(b[i]);
		if(f1!=f2) fa[f2]=f1;
	}
	fo(i,0,n) if(exi[a[i]] && getfa(a[i])==a[i]) ++ans,exi[a[i]]=0;
	if(!(a[0]==b[0] && ok[a[0]]==1)) --ans;
	printf("%d",ans);
	return 0;
}