// Slower C++ solution:
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

 

using namespace std;

using namespace __gnu_pbds;

 

#define fi first

#define se second

#define mp make_pair

#define pb push_back

#define fbo find_by_order

#define ook order_of_key

 

typedef long long ll;

typedef pair<ll,ll> ii;

typedef vector<ll> vi;

typedef long double ld; 

typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;

typedef set<ll>::iterator sit;

typedef map<ll,ll>::iterator mit;



ll a[100011];

ll b[100011];



struct DSU

{

	int S;

	

	struct node

	{

		int p; ll sum;

	};

	vector<node> dsu;

	

	DSU(int n)

	{

		S = n;

		for(int i = 0; i < n; i++)

		{

			node tmp;

			tmp.p = i; tmp.sum = 0;

			dsu.pb(tmp);

		}

	}

	

	void reset(int n)

	{

		dsu.clear();

		S = n;

		for(int i = 0; i < n; i++)

		{

			node tmp;

			tmp.p = i; tmp.sum = 0;

			dsu.pb(tmp);

		}

	}

	

	int rt(int u)

	{

		if(dsu[u].p == u) return u;

		dsu[u].p = rt(dsu[u].p);

		return dsu[u].p;

	}

	

	void merge(int u, int v)

	{

		u = rt(u); v = rt(v);

		if(u == v) return ;

		if(rand()&1) swap(u, v);

		dsu[v].p = u;

		dsu[u].sum += dsu[v].sum;

	}

	

	bool sameset(int u, int v)

	{

		if(rt(u) == rt(v)) return true;

		return false;

	}

	

	ll getstat(int u)

	{

		return dsu[rt(u)].sum;

	}

};



bool used[100001];

bool req[100001];

int main()

{

	ios_base::sync_with_stdio(0); cin.tie(0);

	int n; cin>>n;

	vector<ll> v1,v2;

	for(int i=0;i<n;i++)

	{

		cin>>a[i];

		a[n]^=a[i];

		v1.pb(a[i]);

	}

	v1.pb(a[n]);

	for(int i=0;i<n;i++)

	{

		cin>>b[i];

		b[n]^=b[i];

		v2.pb(b[i]);

	}

	v2.pb(b[n]);

	sort(v1.begin(),v1.end());

	sort(v2.begin(),v2.end());

	for(int i=0;i<=n;i++)

	{

		if(v1[i]!=v2[i])

		{

			cout<<-1<<'\n';

			return 0;

		}

	}

	//swap a[n] with a[i] how many times to reach b?

	v1.erase(unique(v1.begin(),v1.end()),v1.end());

	int ans=0;

	for(int i = 0; i <= n; i++)

	{

		a[i]=lower_bound(v1.begin(),v1.end(),a[i])-v1.begin();

		b[i]=lower_bound(v1.begin(),v1.end(),b[i])-v1.begin();

		if(i<n&&a[i]!=b[i]) ans++;

		if(a[i]!=b[i])

		{

			req[a[i]]=req[b[i]]=1;

		}

	}

	DSU dsu(int(v1.size()));

	for(int i=0;i<=n;i++)

	{

		dsu.merge(a[i],b[i]);

	}

	for(int i=0;i<v1.size();i++)

	{

		if(dsu.rt(i)==i)

		{

			used[i]=1;

		}

	}

	int holy1 = dsu.rt(a[n]);

	int holy2 = dsu.rt(b[n]);

	for(int i=0;i<v1.size();i++)

	{

		if(i!=holy1&&i!=holy2&&req[i]&&used[i]) ans++;

	}

	cout<<ans<<'\n';

}


// Faster C++ solution for the same problem (measured speedup: 1.106239148400726x):
#include<cstdio>

#include<map>

#include<algorithm>

#define maxn 100005

using namespace std;

int n,a[maxn],b[maxn],fa[maxn],siz[maxn],x,ans,cnt;

map<int,int> mp,val;

int find(int x)

{

	if(fa[x]==x) return x;

	return fa[x]=find(fa[x]);

}

int main()

{

	scanf("%d",&n);

	for(int i=1;i<=n;i++) fa[i]=i,siz[i]=1;

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

	{

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

		x^=a[i]; mp[a[i]]++;

		if(!val[a[i]]) val[a[i]]=++cnt;

	}

	mp[x]++;

	for(int i=1;i<=n;i++) scanf("%d",&b[i]);

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

	{

		if(!mp[b[i]]) { printf("-1\n"); return 0; }

		mp[b[i]]--;

		if(!val[b[i]]) val[b[i]]=++cnt;

		if(b[i]!=a[i])

		{

			int r1=find(val[a[i]]),r2=find(val[b[i]]);

			if(r1!=r2) fa[r2]=r1,siz[r1]+=siz[r2];

			ans++;

		}

	}

	bool f=true;

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

		if(a[i]!=b[i]) { f=false; break; }

	if(f) { printf("0\n"); return 0; }

	if(!val[x]) ans++;

	for(int i=1;i<=cnt;i++)

		if(find(i)==i&&siz[i]>1) ans++;

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

}