#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5 + 7;
template <class T>
inline T rd() {
  bool f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = 0;
  T x = 0;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - 48;
  return f ? x : -x;
}
inline void NIE() {
  puts("-1");
  exit(0);
}
int n, m;
int a[maxN], b[maxN];
int occa[maxN], occb[maxN];
bool vis[maxN];
int cc;
int res = 0;
struct vec {
  static const int maxE = maxN;
  int g[maxN], te;
  struct edge {
    int y, nxt;
  } e[maxE << 1];
  inline void push(int x, int y) {
    e[++te] = (edge){y, g[x]};
    g[x] = te;
  }
  inline void link(int x, int y) { push(x, y), push(y, x); }
  inline int& operator()(int x) { return g[x]; }
  inline edge& operator[](int x) { return e[x]; }
} e;
void gao() {
  static int val[maxN];
  for (int i = (1), _ = (n); i <= _; ++i) val[i] = a[i], val[i + n] = b[i];
  val[m = 2 * n + 1] = a[n + 1];
  sort(val + 1, val + m + 1);
  m = unique(val + 1, val + m + 1) - (val + 1);
  for (int i = (1), _ = (n + 1); i <= _; ++i)
    a[i] = lower_bound(val + 1, val + m + 1, a[i]) - val;
  for (int i = (1), _ = (n); i <= _; ++i)
    b[i] = lower_bound(val + 1, val + m + 1, b[i]) - val;
}
void dfs(int x) {
  vis[x] = 1;
  ++cc;
  for (int p = e(x), y = e[p].y; p; y = e[p = e[p].nxt].y)
    if (!vis[y]) dfs(y);
}
int main() {
  n = rd<int>();
  for (int i = (1), _ = (n); i <= _; ++i) a[n + 1] ^= (a[i] = rd<int>());
  for (int i = (1), _ = (n); i <= _; ++i) b[i] = rd<int>();
  gao();
  for (int i = (1), _ = (n); i <= _; ++i) ++occa[a[i]], ++occb[b[i]];
  int dif = 0;
  for (int i = (1), _ = (m); i <= _; ++i) {
    if (occa[i] > occb[i]) {
      if (dif != 0) NIE();
      dif = i;
    }
  }
  if (dif) {
    if (occa[dif] == occb[dif] + 1 && occa[a[n + 1]] == occb[a[n + 1]] - 1) {
      res = -1;
      for (int i = (1), _ = (n); i <= _; ++i)
        if (a[i] == dif) {
          swap(a[n + 1], a[i]);
          break;
        }
    } else
      NIE();
  }
  for (int i = (1), _ = (n); i <= _; ++i) e.link(a[i], b[i]);
  for (int i = (1), _ = (n); i <= _; ++i)
    if (a[i] == a[n + 1]) --res;
  for (int i = (1), _ = (m); i <= _; ++i)
    if (!vis[i]) {
      cc = 0;
      dfs(i);
      if (cc != 1) res += cc + 1;
    }
  printf("%d\n", res);
  return 0;
}
