#include <bits/stdc++.h>
struct {
  inline operator int() {
    int x;
    return scanf("%d", &x), x;
  }
  inline operator long long() {
    long long x;
    return scanf("%lld", &x), x;
  }
  template <class T>
  inline void operator()(T &x) {
    x = *this;
  }
  template <class T, class... A>
  inline void operator()(T &x, A &...a) {
    x = *this;
    this->operator()(a...);
  }
} read;
const int maxn = 100005;
int a[maxn], b[maxn];
int tmp[maxn];
int tot[maxn];
int fa[maxn], size[maxn];
int find(int x) {
  if (fa[x] == x) return x;
  return fa[x] = find(fa[x]);
}
int main() {
  int n = read;
  for (int i = 1; i <= n; i++) {
    a[i] = read;
    a[n + 1] ^= a[i];
  }
  for (int i = 1; i <= n + 1; i++) tmp[i] = a[i];
  std::sort(tmp + 1, tmp + n + 2);
  for (int i = 1; i <= n + 1; i++) {
    a[i] = int(std::lower_bound(tmp + 1, tmp + n + 2, a[i]) - tmp);
    ++tot[a[i]];
  }
  for (int i = 1; i <= n; i++) {
    b[i] = read;
    b[n + 1] ^= b[i];
  }
  for (int i = 1; i <= n + 1; i++) {
    int x = int(std::lower_bound(tmp + 1, tmp + n + 2, b[i]) - tmp);
    if (tmp[x] != b[i] or !tot[x]) return puts("-1"), 0;
    b[i] = x;
    --tot[x];
  }
  for (int i = 1; i <= n + 1; i++) {
    fa[i] = i;
    size[i] = 1;
  }
  int ans = -1;
  for (int i = 1; i <= n + 1; i++) {
    int x = find(a[i]), y = find(b[i]);
    if (a[i] != b[i]) {
      fa[x] = y;
      size[y] += size[x];
      ++ans;
    }
  }
  for (int i = 1; i <= n + 1; i++)
    if (fa[i] == i and size[i] > 1) ++ans;
  printf("%d\n", ans == -1 ? 0 : ans);
}
