#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int sc = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') sc = sc * 10 + ch - '0', ch = getchar();
  return sc * f;
}
const int MAXN = 100005;
int n, a[MAXN], b[MAXN], ans, ls[MAXN << 1], m, fa[MAXN];
map<int, int> cnt;
inline int findfa(int x) {
  while (x ^ fa[x]) x = fa[x] = fa[fa[x]];
  return x;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) a[i] = read(), a[n + 1] ^= a[i], cnt[a[i]]++;
  cnt[a[n + 1]]++;
  for (int i = 1; i <= n; i++)
    if (!cnt[b[i] = read()]--) return puts("-1"), 0;
  for (int i = 1; i <= n; i++)
    if (a[i] ^ b[i]) ls[++m] = a[i], ls[++m] = b[i];
  sort(ls + 1, ls + m + 1);
  m = unique(ls + 1, ls + m + 1) - ls - 1;
  for (int i = 1; i <= m; i++) fa[i] = i;
  for (int i = 1; i <= n; i++)
    if (a[i] ^ b[i])
      a[i] = lower_bound(ls + 1, ls + m + 1, a[i]) - ls,
      b[i] = lower_bound(ls + 1, ls + m + 1, b[i]) - ls,
      fa[findfa(a[i])] = findfa(b[i]), ans++;
  for (int i = 1; i <= m; i++) ans += findfa(i) == i;
  if (*lower_bound(ls + 1, ls + m + 1, a[n + 1]) == a[n + 1]) ans--;
  return printf("%d\n", ans), 0;
}
