#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

int a_arr[10001];
int b_arr[10000];
bool b_chk[100000];

bool a_sort(const pair<unsigned int, unsigned int>&x,  const pair<unsigned int, unsigned int>&y)
{
	if (x.first < y.first) return true;
	if (x.first > y.first) return false;
	return x.second < y.second;
}


int main(void) {
	int N;
	cin >> N;
	
	vector <pair<unsigned int, unsigned int>> a, b;
	unsigned int n;
	unsigned int xor = 0;
	for (int i = 0; i < N; i++) {
		cin >> n;
		xor ^= n;
		a.push_back(make_pair(n, i));
		a_arr[i] = n;
	}
	for (int i = 0; i < N; i++) {
		cin >> n;
		b.push_back(make_pair(n, i));
		b_arr[i] = n;
	}

	int diff_cnt = 0;
	for (int i = a.size()-1; i >= 0; i--) {
		if (a[i].first != b[i].first) diff_cnt++;
		else {
			a.erase(a.begin()+i);
			b.erase(b.begin()+i);
		}
	}


	a.push_back(make_pair(xor, N));
	a_arr[N] = xor;
	std::sort(a.begin(), a.end(), a_sort);
	std::sort(b.begin(), b.end());

	bool use_xor = false;
	int a_xor_vec_pos = -1;
	int a_replace_vec_pos = -1;
	unsigned int b_vec_pos = 0;
	for (unsigned int a_vec_pos = 0; (a_vec_pos < a.size()) && (b_vec_pos < b.size()); a_vec_pos++) {
		if (a[a_vec_pos].second == N) a_xor_vec_pos = a_vec_pos;
		if (b[b_vec_pos].first==a[a_vec_pos].first) {
			if (b[b_vec_pos].first == xor) use_xor = true;
			b_arr[b[b_vec_pos].second] = b_vec_pos;
			b[b_vec_pos].second = a_vec_pos;
			b_vec_pos++;
		} else {
			a[a_vec_pos].second = -1;
			a_replace_vec_pos = a_vec_pos;
		}
	}

	if (use_xor) {
		a[a_replace_vec_pos].second = a_xor_vec_pos;
	}
	if (b_vec_pos == b.size()) {
		for (unsigned int b_pos = 0; b_pos < b.size(); b_pos++) {
			b_chk[b_pos] = false;
		}
		int b_chk_cnt = 0;
		int add_cnt = 0;
		for (unsigned int i = 0; i < b.size() && b_chk_cnt < b.size(); i++) {
			int b_pos = i;
			if(b_chk[b_pos]) continue;

			add_cnt++;
			while (!b_chk[b_pos]) {
				b_chk[b_pos] = true;
				b_chk_cnt++;
				int a_vec_pos = b[b_pos].second;
				int ary_pos = a[a_vec_pos].second;
				b_pos = b_arr[ary_pos];
			}
		}
		if(use_xor) add_cnt--;
		cout << add_cnt + b_chk_cnt << endl;
	} else {
		cout << -1 << endl;
	}

	return 0;
}
