#include <bits/stdc++.h>
using namespace std;
const long long N = 5e5 + 7;
long long n, m, fa[N], cnt = 0;
long long a[N], b[N], c[N], d[N];
inline long long read() {
  long long s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
inline long long find(long long x) {
  return fa[x] == x ? x : fa[x] = find(fa[x]);
}
inline void unionn(long long x, long long y) { fa[find(x)] = find(y); }
map<long long, long long> p;
signed main() {
  n = read();
  for (long long i = 1; i <= n; i++) {
    a[i] = c[i] = read();
    a[n + 1] ^= a[i];
  }
  c[n + 1] = a[n + 1];
  for (long long i = 1; i <= n; i++) {
    b[i] = d[i] = read();
    b[n + 1] ^= b[i];
  }
  d[n + 1] = b[n + 1];
  sort(c + 1, c + n + 2);
  sort(d + 1, d + n + 2);
  bool flag = false;
  for (long long i = 1; i <= n + 1; i++) {
    if (c[i] != d[i]) {
      flag = true;
      break;
    }
  }
  if (flag) {
    printf("-1\n");
    return 0;
  }
  long long ans = 0;
  for (long long i = 1; i <= n + 1; i++) {
    if (a[i] != b[i] || i == n + 1) {
      if (i <= n) ans++;
      if (!p[a[i]]) p[a[i]] = ++cnt;
      if (!p[b[i]]) p[b[i]] = ++cnt;
    }
  }
  if (ans == 0) {
    printf("0\n");
    return 0;
  }
  for (long long i = 1; i <= cnt; i++) fa[i] = i;
  for (long long i = 1; i <= n; i++) {
    if (a[i] != b[i]) {
      unionn(p[a[i]], p[b[i]]);
    }
  }
  for (long long i = 1; i <= cnt; i++) {
    if (fa[i] == i && a[i] != b[i]) ans++;
  }
  printf("%d\n", ans - 1);
  return 0;
}
