#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
long long n, a[maxn], b[maxn], pa[maxn], pb[maxn], TOT;
char cc;
long long ff;
template <typename T>
void read(T& aa) {
  aa = 0;
  cc = getchar();
  ff = 1;
  while ((cc < '0' || cc > '9') && cc != '-') cc = getchar();
  if (cc == '-') ff = -1, cc = getchar();
  while (cc >= '0' && cc <= '9') aa = aa * 10 + cc - '0', cc = getchar();
  aa *= ff;
}
int f[maxn], size[maxn], ind[maxn], oud[maxn];
int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); }
void add(int x, int y) {
  ++ind[y];
  ++oud[x];
  int a = find(x), b = find(y);
  if (a != b) f[b] = a, size[a] += size[b];
}
long long solve() {
  for (int i = (0); i <= (n); ++i) pa[i] = a[i], pb[i] = b[i];
  sort(pa, pa + n + 1);
  sort(pb, pb + n + 1);
  for (int i = (0); i <= (n); ++i)
    if (pa[i] != pb[i]) return -1;
  TOT = unique(pa, pa + n + 1) - pa;
  for (int i = (0); i <= (n); ++i) {
    a[i] = lower_bound(pa, pa + TOT, a[i]) - pa + 1;
    b[i] = lower_bound(pa, pa + TOT, b[i]) - pa + 1;
  }
  long long ans = 0;
  for (int i = (1); i <= (TOT); ++i) f[i] = i, size[i] = 1;
  for (int i = (1); i <= (n); ++i)
    if (a[i] != b[i]) add(a[i], b[i]);
  for (int i = (1); i <= (TOT); ++i)
    if (oud[i] > ind[i]) {
      add(a[0], i);
      --ans;
    }
  for (int i = (1); i <= (TOT); ++i)
    if (find(i) == i) {
      if (size[i] == 1) continue;
      ans += size[i];
      if (find(a[0]) != i) ans++;
    }
  return ans;
}
int main() {
  read(n);
  for (int i = (1); i <= (n); ++i) read(a[i]), a[0] ^= a[i];
  for (int i = (1); i <= (n); ++i) read(b[i]), b[0] ^= b[i];
  printf("%lld\n", solve());
  return 0;
}
