#include <bits/stdc++.h>
using namespace std;
const int rlen = 1 << 18 | 1;
char buf[rlen], *ib = buf, *ob = buf;
inline int read() {
  int ans = 0;
  char ch =
      (((ib == ob) && (ob = (ib = buf) + fread(buf, 1, rlen, stdin)), ib == ob)
           ? -1
           : *ib++);
  while (!isdigit(ch))
    ch = (((ib == ob) && (ob = (ib = buf) + fread(buf, 1, rlen, stdin)),
           ib == ob)
              ? -1
              : *ib++);
  while (isdigit(ch))
    ans = ((ans << 2) + ans << 1) + (ch ^ 48),
    ch = (((ib == ob) && (ob = (ib = buf) + fread(buf, 1, rlen, stdin)),
           ib == ob)
              ? -1
              : *ib++);
  return ans;
}
const int N = 2e5 + 5;
int n, a[N], b[N], _a[N], _b[N];
inline bool check() {
  for (register int i = 1; i <= n + 1; ++i) _a[i] = a[i], _b[i] = b[i];
  sort(_a + 1, _a + n + 2);
  sort(_b + 1, _b + n + 2);
  for (register int i = 1; i <= n + 1; ++i)
    if (_a[i] ^ _b[i]) return 0;
  return 1;
}
int vl[N << 1], sig = 0;
inline int Find(int x) { return lower_bound(vl + 1, vl + sig + 1, x) - vl; }
int ft[N << 1];
inline int find(int x) { return x ^ ft[x] ? ft[x] = find(ft[x]) : x; }
int main() {
  n = read();
  for (register int i = 1; i <= n; ++i) a[n + 1] ^= (a[i] = read());
  for (register int i = 1; i <= n; ++i) b[n + 1] ^= (b[i] = read());
  if (!check()) {
    cout << -1;
    return 0;
  }
  int res = 0;
  for (register int i = 1; i <= n + 1; ++i) {
    vl[++sig] = a[i];
    vl[++sig] = b[i];
    if (i <= n && a[i] != b[i]) ++res;
  }
  if (!res) {
    cout << 0;
    return 0;
  }
  sort(vl + 1, vl + sig + 1), sig = unique(vl + 1, vl + sig + 1) - vl - 1;
  for (register int i = 1; i <= sig; ++i) ft[i] = i;
  for (register int i = 1; i <= n + 1; ++i) {
    a[i] = find(Find(a[i]));
    b[i] = find(Find(b[i]));
    if (a[i] ^ b[i]) ft[b[i]] = a[i];
  }
  for (register int i = 1; i <= n + 1; ++i)
    if (ft[i] == i) ++res;
  cout << res - 1;
  return 0;
}
