#include <bits/stdc++.h>
using namespace std;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
                                       qu[55];
int f, qr;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char x) {
  *oS++ = x;
  if (oS == oT) flush();
}
template <class I>
inline void gi(I &x) {
  for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-') f = -1;
  for (x = 0; c <= '9' && c >= '0';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    x = (x << 1) + (x << 3) + (c & 15);
  x *= f;
}
template <class I>
inline void get(I &x) {
  for (c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++);
       c < 'A' || c > 'Z';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    ;
  x = c;
}
inline void read(char *s) {
  for (c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++);
       c < 'A' || c > 'Z';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    ;
  for (; c >= 'A' && c <= 'Z';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    *++s = c;
  *++s = '\0';
}
template <class I>
inline void print(I x) {
  if (!x) putc('0');
  if (x < 0) putc('-'), x = -x;
  while (x) qu[++qr] = x % 10 + '0', x /= 10;
  while (qr) putc(qu[qr--]);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher_;
}  // namespace io
using io ::get;
using io ::gi;
using io ::print;
using io ::putc;
using io ::read;
const int N = 1e5 + 5;
int a[N], b[N], c[N], d[N], fa[N], sz[N], vis[N];
inline int find(register int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
set<pair<int, int> > s;
int main() {
  register int n, m, i, sa, sb, edge, cnt, x, y;
  gi(n);
  sa = 0;
  for (i = 1; i <= n; ++i) gi(a[i]), sa ^= a[i], c[i] = a[i];
  c[n + 1] = sa;
  sb = 0;
  for (i = 1; i <= n; ++i) gi(b[i]), sb ^= b[i], d[i] = b[i];
  d[n + 1] = sb;
  sort(c + 1, c + 1 + n + 1);
  sort(d + 1, d + 1 + n + 1);
  for (i = 1; i <= n + 1; ++i)
    if (c[i] != d[i]) return puts("-1"), 0;
  a[n + 1] = sa;
  b[n + 1] = sb;
  m = unique(c + 1, c + 1 + n + 1) - c - 1;
  for (i = 1; i <= n + 1; ++i)
    a[i] = lower_bound(c + 1, c + 1 + m, a[i]) - c,
    b[i] = lower_bound(c + 1, c + 1 + m, b[i]) - c;
  for (i = 1; i <= m; ++i) fa[i] = i, sz[i] = 1, d[i] = 0;
  edge = 0;
  for (i = 1; i <= n; ++i)
    if (a[i] != b[i]) {
      x = a[i];
      y = b[i];
      if (s.find(make_pair(x, y)) != s.end()) continue;
      s.insert(make_pair(x, y));
      ++edge;
      x = find(x);
      y = find(y);
      if (x == y) continue;
      if (d[x] < d[y]) swap(x, y);
      if (d[x] == d[y]) ++d[x];
      fa[y] = x;
      sz[x] += sz[y];
    }
  cnt = 0;
  for (i = 1; i <= n + 1; ++i) {
    x = find(a[i]);
    if (!vis[x]) {
      vis[x] = 1;
      if (sz[x] > 1) ++cnt;
    }
  }
  cnt += sz[find(a[n + 1])] == 1;
  printf("%d\n", edge + cnt - 1);
  return 0;
}
