#include <bits/stdc++.h>
using namespace std;
vector<int> v[200005];
int n, sm, rin, lk, ans;
int a[200005], b[200005], c[200005], d[200005], fa[200005];
inline int read() {
  int n = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    n = n * 10 + c - '0';
    c = getchar();
  }
  return n * f;
}
int getf(int x) { return fa[x] ? fa[x] = getf(fa[x]) : x; }
inline bool gather(int x, int y) {
  if (x != y)
    return fa[x] = y;
  else
    return false;
}
int main() {
  register int i, j;
  n = read();
  for (i = 1; i <= n; ++i) sm ^= (a[i] = c[i] = read());
  for (i = 1; i <= n; ++i) b[i] = d[i] = read();
  c[n + 1] = sm;
  sort(c + 1, c + n + 2);
  sort(d + 1, d + n + 1);
  for (i = 1; i <= n; ++i)
    if (c[i] != d[i]) break;
  for (j = i; j <= n; ++j)
    if (c[j + 1] != d[j]) break;
  if (j <= n) return 0 * printf("-1");
  rin = unique(c + 1, c + n + 2) - c - 1;
  for (i = 1; i <= n; ++i)
    a[i] = lower_bound(c + 1, c + rin + 1, a[i]) - c,
    b[i] = lower_bound(c + 1, c + rin + 1, b[i]) - c;
  sm = lower_bound(c + 1, c + rin + 1, sm) - c;
  for (i = 1; i <= n; ++i)
    if (a[i] != b[i]) ++lk, ++ans, v[a[i]].push_back(i), v[b[i]].push_back(i);
  for (i = 1; i <= n; ++i)
    if (a[i] != b[i] && sm == b[i]) --ans;
  for (i = 1; i <= rin; ++i)
    for (j = 0; j + 1 < v[i].size(); ++j)
      if (gather(getf(v[i][j]), getf(v[i][j + 1]))) --lk;
  ans += lk;
  printf("%d", ans);
}
