#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
inline int read() {
  int x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}
int n, a[1000005], b[1000005], c[1000005], d[1000005], cnt, n1, n2, nw, fl,
    f[1000005], t1, t2, ans;
inline int F(int x) {
  if (f[x] == x) return x;
  return f[x] = F(f[x]);
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) a[i] = read(), n1 ^= a[i];
  for (int i = 1; i <= n; i++) b[i] = read(), n2 ^= b[i];
  a[n + 1] = n1;
  b[n + 1] = n2;
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) c[++cnt] = a[i], d[cnt] = b[i], ans++;
  c[++cnt] = n1;
  d[cnt] = n2;
  sort(c + 1, c + cnt + 1);
  sort(d + 1, d + cnt + 1);
  for (int i = 1; i <= cnt; i++)
    if (c[i] != d[i]) {
      fl = 1;
      break;
    }
  if (fl) return puts("-1"), 0;
  t1 = unique(c + 1, c + cnt + 1) - c - 1;
  for (int i = 1; i <= t1; i++) f[i] = i;
  for (int i = 1; i <= (n + 1); i++) {
    if (a[i] != b[i]) {
      int x = lower_bound(c + 1, c + cnt + 1, a[i]) - c;
      int y = lower_bound(c + 1, c + cnt + 1, b[i]) - c;
      int fx = F(x), fy = F(y);
      if (fx != fy) f[fx] = fy;
    }
  }
  for (int i = 1; i <= t1; i++)
    if (f[i] == i) ans++;
  cout << ans - 1 << endl;
  return 0;
}
