#include <bits/stdc++.h>
int get() {
  char c;
  while (!isdigit(c = getchar()))
    ;
  int k = c - '0';
  for (; isdigit(c = getchar()); k = k * 10 + c - '0')
    ;
  return k;
}
using namespace std;
const int N = 2e5 + 10;
int n, a[N], b[N], ans, idx;
map<int, int> cnt, id;
struct edge {
  int v;
  edge *nxt;
} pool[N], *tp = pool, *fst[N];
int dfn[N], low[N], tot, top, stk[N];
bool inst[N];
void addedge(int u, int v) { *tp = (edge){v, fst[u]}, fst[u] = tp++; }
void tarjan(int x) {
  dfn[x] = low[x] = ++idx, stk[++top] = x, inst[x] = 1;
  for (edge *i = fst[x]; i; i = i->nxt)
    if (!dfn[i->v])
      tarjan(i->v), low[x] = min(low[x], low[i->v]);
    else if (inst[i->v])
      low[x] = min(low[x], dfn[i->v]);
  if (low[x] == dfn[x])
    for (++tot; inst[stk[top]] = 0, stk[top--] != x;)
      ;
}
void judge() {
  bool f = 0;
  for (auto i : cnt)
    if (!i.second)
      continue;
    else if (f)
      puts("-1"), exit(0);
    else
      f = 1;
}
void init() {
  n = get();
  for (int i = 1; i <= n; ++i) a[i] = get(), ++cnt[a[i]];
  for (int i = 1; i <= n; ++i) b[i] = get(), ans += a[i] != b[i], --cnt[b[i]];
  int x = 0;
  for (int i = 1; i <= n; ++i) x ^= a[i];
  ++cnt[x], judge(), --cnt[x];
  bool eq = 1;
  for (auto i : cnt)
    if (i.second) {
      eq = 0;
      break;
    }
  if (eq) {
    for (int i = 1; i <= n; ++i)
      if (a[i] != b[i] && a[i] == x) {
        --ans;
        break;
      }
    return;
  }
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i] && cnt[a[i]] == 1) {
      a[i] = x, --ans;
      return;
    }
  assert(0);
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i] && cnt[a[i]] == 1 && b[i] == x) {
      a[i] = x;
      return;
    }
  assert(0);
}
int main() {
  init();
  idx = n + 1;
  for (int i = 1; i <= n; ++i)
    if (!id.count(a[i])) id[a[i]] = ++idx;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == b[i]) continue;
    addedge(i, id[b[i]]), addedge(id[a[i]], i);
  }
  idx = 0;
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i] && !dfn[i]) tarjan(i);
  printf("%d", ans + tot);
  return 0;
}
