#include <bits/stdc++.h>
// #include "stdafx.h"
// #pragma warning(disable : 4996) //_CRT_SECURE_NO_WARNINGS
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x)	scanf("%d",&x)
#define sl(x)	scanf("%lld",&x)
#define ss(s)	scanf("%s",s)
#define pi(x)	printf("%d\n",x)
#define pl(x)	printf("%lld\n",x)
#define ps(s)	printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pl;
typedef vector<int>		vi;
typedef vector<ll>		vl;
typedef vector<pii>		vpii;
typedef vector<pl>		vpl;
typedef vector<vi>		vvi;
typedef vector<vl>		vvl;
int mpow(int base, int exp); 
void ipgraph(int m);
int dfs(int u);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================

vi g[N];
int a[N];
int b[N];
map<int,int> vis;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int i,n,k,j;
	cin >> n;
	int x = 0;
	multiset<int> A;
	fo(i, n) cin >> a[i+1], x ^= a[i+1], A.insert(a[i+1]);
	a[0] = x;
	A.insert(x);
	fo(i, n) {
		cin >> b[i+1];
		if(A.find(b[i+1]) == A.end()){
			cout << -1 << endl;
			return 0;
		}
		A.erase(A.find(b[i+1]));
	}
	b[0] = *A.begin();
	fo(i, n+1){
		g[b[i]].pb(a[i]);
	}
	int ans = max(1,dfs(a[0])) - 1;
	if(a[0] == b[0] and ans) ans++;
	Fo(i, 1, n+1) {
		if(!vis[a[i]]){
			x = dfs(a[i]);
			if(x) ans += x+1;
		}
	}
	cout << ans << endl;
	
	return 0;
} 

int dfs(int u){
	vis[u] = 1;
	int ans = 0;
	for(int v:g[u]){
		if(v == u) continue;
		ans++;
		if (vis[v]) continue;
		ans += dfs(v);
	}
	return ans;
}