#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int tot, ans;
int a[N], b[N], a1[N], b1[N];
int f[N], bel[N];
inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) w |= ch == '-', ch = getchar();
  while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
inline int get(int x) { return f[x] == x ? x : f[x] = get(f[x]); }
int main() {
  int n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = a1[i] = read();
    a[n + 1] ^= a[i];
  }
  a1[n + 1] = a[n + 1];
  for (int i = 1; i <= n; i++) {
    b[i] = b1[i] = read();
    b[n + 1] ^= b[i];
  }
  b1[n + 1] = b[n + 1], n++;
  sort(a1 + 1, a1 + 1 + n);
  sort(b1 + 1, b1 + 1 + n);
  for (int i = 1; i <= n; i++)
    if (a1[i] ^ b1[i]) return 0 & printf("-1");
  for (int i = 1; i <= n; i++)
    if (i == n || a[i] ^ b[i]) {
      if (i < n) ans++;
      if (!bel[a[i]]) bel[a[i]] = ++tot;
      if (!bel[b[i]]) bel[b[i]] = ++tot;
    }
  for (int i = 1; i <= tot; i++) f[i] = i;
  for (int i = 1; i <= n; i++)
    if (a[i] ^ b[i]) f[get(bel[a[i]])] = get(bel[b[i]]);
  for (int i = 1; i <= tot; i++)
    if (f[i] == i) ans++;
  printf("%d", ans - 1);
  return 0;
}
