#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &s) {
  char c = getchar();
  int f = 1;
  s = 0;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
  }
  s *= f;
}
const int N = 100005;
int n, cnt, ans;
int a[N], b[N], c[N], d[N], fa[N], siz[N];
bool bo[N];
map<int, int> id;
int getfa(int x) { return (x == fa[x]) ? x : fa[x] = getfa(fa[x]); }
inline void merge(int a, int b) {
  int x = getfa(a), y = getfa(b);
  bo[x] = bo[y] = 1;
  if (x != y) {
    if (siz[x] > siz[y])
      fa[y] = x, siz[x] += siz[y];
    else
      fa[x] = y, siz[y] += siz[x];
  }
}
int main() {
  read(n);
  for (int i = (1); i <= (n); i = -(~i)) {
    read(a[i]), a[n + 1] ^= a[i];
    if (!id[a[i]]) id[a[i]] = ++cnt;
  }
  if (!id[a[n + 1]]) id[a[n + 1]] = ++cnt;
  for (int i = (1); i <= (n); i = -(~i)) {
    read(b[i]), b[n + 1] ^= b[i];
    if (!id[b[i]]) id[b[i]] = ++cnt;
  }
  if (!id[b[n + 1]]) id[b[n + 1]] = ++cnt;
  for (int i = (1); i <= (n + 1); i = -(~i)) c[i] = a[i], d[i] = b[i];
  sort(c + 1, c + n + 2), sort(d + 1, d + n + 2);
  for (int i = (1); i <= (cnt); i = -(~i)) fa[i] = i, siz[i] = 1;
  for (int i = (1); i <= (n + 1); i = -(~i))
    if (c[i] != d[i]) return (puts("-1"), 0);
  for (int i = (1); i <= (n + 1); i = -(~i)) {
    a[i] = id[a[i]], b[i] = id[b[i]];
    if (a[i] != b[i]) {
      if (i <= n) ++ans;
      merge(a[i], b[i]);
    }
  }
  if (ans == 0) return puts("0\n");
  bo[a[n + 1]] = bo[b[n + 1]] = 1;
  for (int i = (1); i <= (cnt); i = -(~i))
    if (fa[i] == i && bo[i] == 1) ++ans;
  printf("%d\n", ans - 1);
  return 0;
}
