Language: C++

// 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;
}

Compiler output:
a.cc: In function 'void FastIO::read(T&)':
a.cc:21:46: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   21 |                         x = 0; register char ch = getchar();
      |                                              ^~
a.cc: In function 'int main()':
a.cc:51:1: error: 'namespace' definition is not allowed here
   51 | namespace FastIO {
      | ^~~~~~~~~
a.cc:60:9: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
   60 | int main() {
      |         ^~
a.cc:60:9: note: remove parentheses to default-initialize a variable
   60 | int main() {
      |         ^~
      |         --
a.cc:60:9: note: or replace parentheses with braces to value-initialize a variable
a.cc:60:12: error: a function-definition is not allowed here before '{' token
   60 | int main() {
      |            ^
a.cc: In instantiation of 'void FastIO::read(T&) [with T = int]':
a.cc:33:6:   required from here
   33 |         read(N);
      |         ~~~~^~~
a.cc:21:46: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   21 |                         x = 0; register char ch = getchar();
      |                                              ^~