#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long s = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = 0;
    c = getchar();
  }
  while (c >= '0' && c <= '9') s = (s << 1) + (s << 3) + c - 48, c = getchar();
  return f ? s : -s;
}
map<long long, long long> Map;
long long a[100010], b[100010], n, suma, sumb;
long long c[100010], d[100010], ans, tot;
long long fa[100010];
long long get_fa(long long x) { return fa[x] == x ? x : fa[x] = get_fa(fa[x]); }
signed main() {
  n = read();
  for (register long long i = 1; i <= n; ++i) a[i] = read(), suma ^= a[i];
  for (register long long i = 1; i <= n; ++i) b[i] = read(), sumb ^= b[i];
  a[n + 1] = suma;
  b[n + 1] = sumb;
  ++n;
  for (register long long i = 1; i <= n; ++i) c[i] = a[i], d[i] = b[i];
  sort(c + 1, c + 1 + n);
  sort(d + 1, d + 1 + n);
  for (register long long i = 1; i <= n; ++i)
    if (c[i] != d[i]) {
      printf("-1\n");
      return 0;
    }
  for (register long long i = 1; i <= n; ++i)
    if (a[i] != b[i]) {
      if (i != n) ++ans;
      if (!Map[a[i]]) Map[a[i]] = ++tot;
      if (!Map[b[i]]) Map[b[i]] = ++tot;
    }
  if (!ans) {
    printf("0\n");
    return 0;
  }
  for (register long long i = 1; i <= tot; ++i) fa[i] = i;
  for (register long long i = 1; i <= n; ++i)
    if (Map[a[i]] != Map[b[i]]) fa[get_fa(Map[a[i]])] = get_fa(Map[b[i]]);
  for (register long long i = 1; i <= tot; ++i)
    if (fa[i] == i) ans++;
  printf("%lld\n", ans - 1);
  return 0;
}
