#include <bits/stdc++.h>
using namespace std;
int i, j, k, n, x, y, gx, gy, cnt, ans;
int a[100007], b[100007], c[100007], d[100007];
int fa[100007];
bool bz[100007];
int get(int x) { return fa[x] == x ? x : fa[x] = get(fa[x]); }
int read() {
  int fh = 1, res = 0;
  char ch;
  while ((ch < '0' || ch > '9') && (ch ^ '0')) ch = getchar();
  if (ch == '-') fh = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    res = (res << 3) + (res << 1) + (ch ^ '0'), ch = getchar();
  return fh * res;
}
int main() {
  n = read();
  for (i = 1; i <= n; i++) a[i] = read(), a[n + 1] ^= a[i];
  for (i = 1; i <= n; i++) b[i] = read(), b[n + 1] ^= b[i];
  memcpy(c, a, sizeof(c));
  memcpy(d, b, sizeof(d));
  n++;
  sort(c + 1, c + n + 1);
  sort(d + 1, d + n + 1);
  for (i = 1; i <= n; i++)
    if (c[i] != d[i]) {
      printf("-1");
      return 0;
    }
  cnt = unique(c + 1, c + n + 1) - c - 1;
  for (i = 1; i <= cnt; i++) fa[i] = i;
  for (i = 1; i <= n; i++)
    a[i] = lower_bound(c + 1, c + n + 1, a[i]) - c,
    b[i] = lower_bound(c + 1, c + n + 1, b[i]) - c;
  for (i = 1; i <= n; i++) {
    if (a[i] == b[i] && i < n) continue;
    ans += (i < n);
    x = a[i], y = b[i];
    gx = get(x);
    gy = get(y);
    if (gx != gy) fa[gy] = gx;
    bz[x] = bz[y] = 1;
  }
  for (i = 1; i <= cnt; i++) fa[i] = get(fa[i]);
  for (i = 1; i <= cnt; i++)
    if (bz[i] && fa[i] == i) ans++;
  if (ans) ans--;
  printf("%d", ans);
  return 0;
}
