#include <bits/stdc++.h>
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second

using namespace std;
typedef pair <int, int> pii;

int arr[301010], brr[301010];
vector <int> ta, tb;

map <int, vector <int> > g;

set <int> chk;
int dfs(int k) {
	if(chk.find(k) != chk.end()) return 0;
	chk.insert(k);
	int ret = 0;
	for(auto i : g[k]) {
		if(i == k) continue;
		ret++;
		ret += dfs(i);
	}
	return ret;
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> arr[i];
		arr[0] ^= arr[i];
		ta.eb(arr[i]);
	}
	ta.eb(arr[0]);
	for(int i = 1; i <= n; i++) {
		cin >> brr[i];
		brr[0] ^= brr[i];
		tb.eb(brr[i]);
	}
	tb.eb(brr[0]);
	sort(all(ta));
	sort(all(tb));

	for(int i = 0; i <= n; i++) {
		if(ta[i] != tb[i]) {
			cout << -1;
			return 0;
		}
	}
	for(int i = 0; i <= n; i++) {
		g[arr[i]].eb(brr[i]);
	}
	int ans = max(1, dfs(arr[0])) - 1;
	if(arr[0] == brr[0] && ans != 0) ans++;
	for(int i = 1; i <= n; i++) {
		if(arr[i] != brr[i] && chk.find(arr[i]) == chk.end()) ans += dfs(arr[i]) + 1;
	}
	cout << ans;
}