#include <bits/stdc++.h>
using namespace std;
long long n, a[100010], b[100010], aa[100010], bb[100010];
int f[100010], ans;
int find(int x) {
  if (f[x] == x) return x;
  return f[x] = find(f[x]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), aa[i] = a[i], a[n + 1] ^= a[i];
  for (int i = 1; i <= n; i++)
    scanf("%d", &b[i]), bb[i] = b[i], b[n + 1] ^= b[i];
  bb[n + 1] = b[n + 1];
  aa[n + 1] = a[n + 1];
  sort(a + 1, a + n + 2);
  int la = unique(a + 1, a + n + 2) - (a + 1);
  sort(b + 1, b + n + 2);
  int lb = unique(b + 1, b + n + 2) - (b + 1);
  if (la ^ lb) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= la; i++)
    if (a[i] ^ b[i]) {
      puts("-1");
      return 0;
    }
  for (int i = 1; i <= n + 1; i++) {
    aa[i] = lower_bound(a + 1, a + la + 1, aa[i]) - a;
    bb[i] = lower_bound(b + 1, b + la + 1, bb[i]) - b;
  }
  for (int i = 1; i <= n; i++)
    if (aa[i] ^ bb[i]) f[aa[i]] = aa[i], f[bb[i]] = bb[i];
  f[aa[n + 1]] = aa[n + 1];
  f[bb[n + 1]] = bb[n + 1];
  for (int i = 1; i <= n + 1; i++)
    if (aa[i] ^ bb[i]) {
      if (i <= n) ans++;
      int fa = find(aa[i]);
      int fb = find(bb[i]);
      f[fa] = fb;
    }
  for (int i = 1; i <= la; i++)
    if (find(i) == i) ans++;
  ans > 1 ? ans -= 1 : ans = 0;
  printf("%d\n", ans);
  return 0;
}
