#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &num) {
  char c = getchar();
  T f = 1;
  num = 0;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    num = (num << 3) + (num << 1) + (c ^ 48);
    c = getchar();
  }
  num *= f;
}
template <typename T>
void qwq(T x) {
  if (x > 9) qwq(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
void write(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  qwq(x);
  putchar('\n');
}
int a[100010];
int b[100010];
int at[100010];
int bt[100010];
int f[100010];
bool in[100010];
inline int root_seeking(int x) {
  if (f[x] != x) f[x] = root_seeking(f[x]);
  return f[x];
}
int main() {
  int n;
  read(n);
  for (int i = 1; i <= n; i++) {
    read(a[i]);
    a[n + 1] ^= a[i];
  }
  for (int i = 1; i <= n; i++) {
    read(b[i]);
    b[n + 1] ^= b[i];
  }
  for (int i = 1; i <= n + 1; i++) {
    at[i] = a[i];
    bt[i] = b[i];
  }
  sort(at + 1, at + n + 2);
  sort(bt + 1, bt + n + 2);
  bool flag = true;
  for (int i = 1; i <= n + 1; i++) {
    if (at[i] != bt[i]) {
      flag = false;
      break;
    }
  }
  if (flag == false) {
    write(-1);
    return 0;
  }
  int len = unique(at + 1, at + n + 2) - at - 1;
  int ans = 0;
  for (int i = 1; i <= n + 1; i++)
    a[i] = lower_bound(at + 1, at + len + 1, a[i]) - at;
  for (int i = 1; i <= n + 1; i++)
    b[i] = lower_bound(at + 1, at + len + 1, b[i]) - at;
  for (int i = 1; i <= len; i++) f[i] = i;
  for (int i = 1; i <= n; i++) {
    if (a[i] == b[i]) {
      in[a[i]] = 1;
      continue;
    }
    ans++;
    int n1 = root_seeking(a[i]);
    int n2 = root_seeking(b[i]);
    if (n1 != n2) f[n1] = n2;
  }
  int now = 0;
  for (int i = 1; i <= len; i++) {
    if (in[i]) continue;
    ans += (root_seeking(i) == i);
    now++;
  }
  if (now) ans--;
  write(ans);
  return 0;
}
