#include <bits/stdc++.h>
using namespace std;
int n, a[100010], b[100010], tot, cnt[100010], fa[100010], ans;
map<int, int> f;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
inline void merge(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx != fy) fa[fx] = fy;
}
int main() {
  freopen("duliu.in", "r", stdin);
  freopen("duliu.out", "w", stdout);
  n = read();
  for (register int i = 1; i <= n; i++) a[i] = read(), a[n + 1] ^= a[i];
  for (register int i = 1; i <= n; i++) b[i] = read(), b[n + 1] ^= b[i];
  n++;
  for (register int i = 1; i <= n; i++)
    if (a[i] != b[i] || i == n) {
      if (!f[a[i]]) f[a[i]] = ++tot;
      cnt[f[a[i]]]++;
    }
  for (register int i = 1; i <= n; i++)
    if (a[i] != b[i] || i == n) cnt[f[b[i]]]--;
  for (register int i = 0; i <= tot; i++)
    if (cnt[i]) {
      printf("-1");
      return 0;
    }
  for (register int i = 1; i <= tot; i++) fa[i] = i;
  for (register int i = 1; i <= n; i++) a[i] = f[a[i]], b[i] = f[b[i]];
  for (register int i = 1; i <= n; i++)
    if (a[i] != b[i]) {
      if (i < n) ans++;
      merge(a[i], b[i]);
    }
  for (register int i = 1; i <= tot; i++)
    if (find(i) == i) ans++;
  if (ans) printf("%d", ans - 1);
  return 0;
}
