// duliu
#include <cstdio>
#include <map>
#include <set>

#define fir first
#define sec second
#define ONLINE_JUDGE

typedef std::set<int> MySt;
typedef std::map<int, MySt> MyMp;

const int MAXN = 1e5 + 10;

int N, val[MAXN], vis[MAXN];
MyMp org_pos;

namespace FastIO {
	template <typename T>
		void read(T & x) {
			x = 0; register char ch = getchar();
			for (; ch < '0' || ch > '9'; ch = getchar());
			for (; ch >= '0' && ch <= '9'; x = (x << 3) + (x << 1) + (ch ^ '0'), ch = getchar());
		}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("duliu.in", "r", stdin);
	freopen("duliu.out", "w", stdout);
#endif
	using FastIO::read;
	read(N);
	int _xor = 0;// duliu
#include <cstdio>
#include <map>
#include <set>

#define fir first
#define sec second
// #define ONLINE_JUDGE

typedef std::set<int> MySt;
typedef std::map<int, MySt> MyMp;

const int MAXN = 1e5 + 10;

int N, val[MAXN], vis[MAXN];
MyMp org_pos;

namespace FastIO {
	template <typename T>
		void read(T & x) {
			x = 0; register char ch = getchar();
			for (; ch < '0' || ch > '9'; ch = getchar());
			for (; ch >= '0' && ch <= '9'; x = (x << 3) + (x << 1) + (ch ^ '0'), ch = getchar());
		}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("duliu.in", "r", stdin);
	freopen("duliu.out", "w", stdout);
#endif
	using FastIO::read;
	read(N);
	int _xor = 0;
	for (int i = 1, vali; i <= N; i++) {
		read(vali);
		org_pos[vali].insert(i);
		_xor ^= vali;
	}
	org_pos[_xor].insert(0);
	for (int i = 1; i <= N; i++) {
		read(val[i]);
		MyMp::iterator it = org_pos.find(val[i]);
		if (it == org_pos.end() || it->sec.empty()) {
			puts("-1");
#ifndef ONLINE_JUDGE
	fclose(stdin); fclose(stdout);
#endif
			return 0;
		}
		MySt::iterator itt = it->sec.find(i);
		if (itt != it->sec.end()) vis[i] = 1, it->sec.erase(itt);
	}
	int ans = 0;
	vis[0] = 1;
	for (int i = 1, tag = 2; i <= N; ++tag, i++) {
		if (vis[i]) continue;
		int step = 0, j;
		for (j = i; !vis[j]; ++step) {
			if (org_pos[val[j]].empty()) {
				puts("-1");
#ifndef ONLINE_JUDGE
				fclose(stdin); fclose(stdout);
#endif
				return 0;
			}
			vis[j] = tag;
			int tmp = *org_pos[val[j]].begin();
			org_pos[val[j]].erase(org_pos[val[j]].begin());
			j = tmp;
		}
		if (vis[j] == tag) ++step;
		ans += step;
	}
	printf("%d\n", ans);
#ifndef ONLINE_JUDGE
	fclose(stdin); fclose(stdout);
#endif
	return 0;
}
	for (int i = 1, vali; i <= N; i++) {
		read(vali);
		org_pos[vali].insert(i);
		_xor ^= vali;
	}
	org_pos[_xor].insert(0);
	for (int i = 1; i <= N; i++) {
		read(val[i]);
		MyMp::iterator it = org_pos.find(val[i]);
		if (it == org_pos.end() || it->sec.empty()) {
			puts("-1");
#ifndef ONLINE_JUDGE
	fclose(stdin); fclose(stdout);
#endif
			return 0;
		}
		MySt::iterator itt = it->sec.find(i);
		if (itt != it->sec.end()) vis[i] = 1, it->sec.erase(itt);
	}
	int ans = 0;
	vis[0] = 1;
	for (int i = 1, tag = 2; i <= N; ++tag, i++) {
		if (vis[i]) continue;
		int step = 0, j;
		for (j = i; !vis[j]; ++step) {
			if (org_pos[val[j]].empty()) {
				puts("-1");
#ifndef ONLINE_JUDGE
				fclose(stdin); fclose(stdout);
#endif
				return 0;
			}
			vis[j] = tag;
			int tmp = *org_pos[val[j]].begin();
			org_pos[val[j]].erase(org_pos[val[j]].begin());
			j = tmp;
		}
		if (vis[j] == tag) ++step;
		ans += step;
	}
	printf("%d\n", ans);
#ifndef ONLINE_JUDGE
	fclose(stdin); fclose(stdout);
#endif
	return 0;
}