/* Generated by powerful Codeforces Tool
 * You can download the binary file in here https://github.com/xalanq/cf-tool (Windows, macOS, Linux)
 * Author: alireza_kaviani
 * Time: 2020-09-02 12:56:06
**/
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

#define all(x)                      (x).begin(),(x).end()
#define Sort(x)                     sort(all((x)))
#define X                           first
#define Y                           second
#define sep                         ' '
#define endl                        '\n'
#define SZ(x)                       ll(x.size())

ll poww(ll a, ll b, ll md) {
    return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));
}

const ll MAXN = 1e6 + 10;
const ll LOG = 22;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9;

int n , mark[MAXN] , A[MAXN] , B[MAXN] , nxt[MAXN];
vector<int> v1 , v2 , vec[MAXN];

int main() {
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

	cin >> n;
	for(int i = 0 ; i < n ; i++){
		cin >> A[i];
		A[n] ^= A[i];
		v1.push_back(A[i]);
	}
	v1.push_back(A[n]);
	for(int i = 0 ; i < n ; i++){
		cin >> B[i];
		B[n] ^= B[i];
		v2.push_back(B[i]);
	}
	v2.push_back(B[n]);
	sort(all(v1));
	sort(all(v2));
	if(v1 != v2)	return cout << -1 << endl , 0;
	for(int i = 0 ; i <= n ; i++){
		A[i] = lower_bound(all(v1) , A[i]) - v1.begin();
		B[i] = lower_bound(all(v1) , B[i]) - v1.begin();
		if(A[i] != B[i])	vec[B[i]].push_back(i);
	}
	int ans = 0;
	for(int i = 0 ; i <= n ; i++)	reverse(all(vec[i]));
	for(int i = 0 ; i <= n ; i++){
		if(A[i] != B[i]){
			nxt[i] = vec[A[i]].back();
			vec[A[i]].pop_back();
			if(i != n)	ans++;
		}
	}
	for(int i = 0 ; i <= n ; i++){
		if(A[i] == B[i] || mark[i])	continue;
		int x = i , flag = 0 , ok = 0;
		ans++;
		while(flag == 0 || x != i){
			flag = 1;
	//		cout << x << sep;
			mark[x] = 1;
			if(A[x] == A[n])	ok = 1;
			x = nxt[x];
		}
		ans -= ok;
	//	cout << ok << endl;
	//	cout << "*********" << endl;
	}
	cout << ans << endl;

    return 0;
}
/*

*/