/************************************************
 * Au: Hany01
 * Prob: duliu
 * Email: hany01dxx@gmail.com & hany01@foxmail.com
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define Rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define X first
#define Y second
#define PB(a) push_back(a)
#define MP(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define ALL(a) a.begin(), a.end()
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

template <typename T> inline T read() {
	static T _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}
//EOT


const int maxn = 2e5 + 5;

int n, all, ls[maxn], lss, ans, fa[maxn], a[maxn], b[maxn];

struct Item {
	int x, y;
}p[maxn];

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

int main()
{


	n = read<int>();
	For(i, 1, n) all ^= (a[i] = ls[++ lss] = p[i].x = read<int>());
	For(i, 1, n) ls[++ lss] = b[i] = p[i].y = read<int>();

	ls[++ lss] = all;
	sort(ls + 1, ls + 1 + lss);
	lss = unique(ls + 1, ls + 1 + lss) - ls - 1;

	static int cnt1[maxn], cnt2[maxn];
	For(i, 1, n) {
		a[i] = lower_bound(ls + 1, ls + 1 + lss, a[i]) - ls;
		b[i] = lower_bound(ls + 1, ls + 1 + lss, b[i]) - ls;
		++ cnt1[a[i]], ++ cnt2[b[i]];
	}
	int sb = 0;
	For(i, 1, lss) if (cnt1[i] != cnt2[i]) {
		sb = i; break;
	}
	if (sb) {
		int as[5], ass = 0;
		For(i, 1, lss) if (cnt1[i] != cnt2[i]) { as[++ ass] = i; if (ass > 2) break; }
		if (ass > 2 || (ls[as[1]] != all && ls[as[2]] != all)) { puts("-1"); return 0; }
	}

	For(i, 1, n) {
		p[i].x = lower_bound(ls + 1, ls + 1 + lss, p[i].x) - ls;
		p[i].y = lower_bound(ls + 1, ls + 1 + lss, p[i].y) - ls;
	}
	all = lower_bound(ls + 1, ls + 1 + lss, all) - ls;
	fa[all] = all;
	For(i, 1, n) if (p[i].x != p[i].y) {
		if (!fa[p[i].x]) fa[p[i].x] = p[i].x;
		if (!fa[p[i].y]) fa[p[i].y] = p[i].y;
		++ ans;
		fa[find(p[i].x)] = find(p[i].y);
	}
	For(i, 1, lss) if (fa[i] == i) ++ ans;
	printf("%d\n", ans - 1);

	return 0;
}