Language: C++

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <cstring>
#include <cstdlib>
#include <cctype>

#define fo(i, x, y) for (int i = x; i <= y; ++i)
#define fd(i, x, y) for (int i = x; i >= y; --i)

using namespace std;

const int maxn = 1e5 + 5;

int n, cnt, ans;
int a[maxn], b[maxn], tmpa[maxn], tmpb[maxn], fa[maxn], rank[maxn];
map<int, int> mp;

int getint() {
	char ch;
	int res = 0, p;
	while (!isdigit(ch = getchar()) && ch != '-');
	p = ch == '-'? ch = getchar(), -1 : 1;
	while (isdigit(ch))
		res = res * 10 + ch - '0' , ch = getchar();
	return res * p;
}

int getfa(int x) {
	return fa[x]? fa[x] = getfa(fa[x]) : x;
}

void un(int x, int y) {
	int fx = getfa(x), fy = getfa(y);
	if (fx == fy) return;
	if (rank[fx] < rank[fy]) fa[fx] = fy;
		else fa[fy] = fx, rank[fy] += rank[fx] == rank[fy];
}

int main() {
	//freopen("duliu.in", "r", stdin);
	//freopen("duliu.out", "w", stdout);
	
	n = getint();
	fo (i, 1, n) tmpa[i] = a[i] = getint(), a[0] ^= a[i];
	fo (i, 1, n) tmpb[i] = b[i] = getint(), b[0] ^= b[i];
	tmpa[0] = a[0]; tmpb[0] = b[0];
	sort(tmpa, tmpa + 1 + n);
	sort(tmpb, tmpb + 1 + n);
	fo (i, 0, n)
		if (tmpa[i] != tmpb[i]) {
			printf("-1\n");
			return 0;
		}
	if (!mp[a[0]]) mp[a[0]] = ++cnt;
	if (!mp[b[0]]) mp[b[0]] = ++cnt;
	fo (i, 1, n)
		if (a[i] != b[i]) {
			ans++;
			if (!mp[a[i]]) mp[a[i]] = ++cnt;
			if (!mp[b[i]]) mp[b[i]] = ++cnt;
		}
	fo (i, 1, n)
		if (a[i] != b[i]) un(mp[a[i]], mp[b[i]]);
	fo (i, 1, cnt)
		if (!fa[i]) ans++;
	ans--;
	printf("%d\n", ans);
	return 0;
}

Compiler output:
a.cc: In function 'void un(int, int)':
a.cc:37:13: error: reference to 'rank' is ambiguous
   37 |         if (rank[fx] < rank[fy]) fa[fx] = fy;
      |             ^~~~
In file included from /usr/include/c++/14/bits/move.h:37,
                 from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:2:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:17:57: note:                 'int rank [100005]'
   17 | int a[maxn], b[maxn], tmpa[maxn], tmpb[maxn], fa[maxn], rank[maxn];
      |                                                         ^~~~
a.cc:37:24: error: reference to 'rank' is ambiguous
   37 |         if (rank[fx] < rank[fy]) fa[fx] = fy;
      |                        ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:17:57: note:                 'int rank [100005]'
   17 | int a[maxn], b[maxn], tmpa[maxn], tmpb[maxn], fa[maxn], rank[maxn];
      |                                                         ^~~~
a.cc:38:35: error: reference to 'rank' is ambiguous
   38 |                 else fa[fy] = fx, rank[fy] += rank[fx] == rank[fy];
      |                                   ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:17:57: note:                 'int rank [100005]'
   17 | int a[maxn], b[maxn], tmpa[maxn], tmpb[maxn], fa[maxn], rank[maxn];
      |                                                         ^~~~
a.cc:38:47: error: reference to 'rank' is ambiguous
   38 |                 else fa[fy] = fx, rank[fy] += rank[fx] == rank[fy];
      |                                               ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:17:57: note:                 'int rank [100005]'
   17 | int a[maxn], b[maxn], tmpa[maxn], tmpb[maxn], fa[maxn], rank[maxn];
      |                                                         ^~~~
a.cc:38:59: error: reference to 'rank' is ambiguous
   38 |                 else fa[fy] = fx, rank[fy] += rank[fx] == rank[fy];
      |                                                           ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:17:57: note:                 'int rank [100005]'
   17 | int a[maxn], b[maxn], tmpa[maxn], tmpb[maxn], fa[maxn], rank[maxn];
      |                                                         ^~~~