#include <bits/stdc++.h>
const int N = 200000 + 10;
int n, a[N], b[N];
int sorted[N], tot;
int anc[N];
int find(int x) { return anc[x] == x ? x : (anc[x] = find(anc[x])); }
bool init() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), a[0] ^= a[i];
  for (int i = 1; i <= n; ++i) scanf("%d", &b[i]), b[0] ^= b[i];
  tot = 2 * (n + 1);
  std::copy(a, a + n + 1, sorted + 1);
  std::copy(b, b + n + 1, sorted + n + 2);
  std::sort(sorted + 1, sorted + tot + 1);
  tot = std::unique(sorted + 1, sorted + tot + 1) - sorted - 1;
  static int cnt[N];
  for (int i = 0; i <= n; ++i)
    ++cnt[a[i] = std::lower_bound(sorted + 1, sorted + tot + 1, a[i]) - sorted];
  for (int i = 0; i <= n; ++i)
    --cnt[b[i] = std::lower_bound(sorted + 1, sorted + tot + 1, b[i]) - sorted];
  for (int i = 1; i <= tot; ++i)
    if (cnt[i]) return false;
  return true;
}
int main() {
  if (!init()) {
    puts("-1");
    return 0;
  }
  std::iota(anc, anc + tot + 1, 0);
  int ans = tot - 1;
  for (int i = 0; i <= n; ++i)
    if (a[i] != b[i] && find(a[i]) != find(b[i]))
      anc[find(a[i])] = find(b[i]), --ans;
  for (int i = 1; i <= n; ++i) ans += (a[i] != b[i]);
  for (int i = 1; i <= n; ++i) {
    if (a[i] != b[i] && find(a[i]) == find(a[0])) {
      --ans;
      break;
    }
  }
  printf("%d\n", ans);
  return 0;
}
