#include <bits/stdc++.h>
using namespace std;
int a[100005], b[100005], n, m, d[100005], c[100005], n1, f[100005];
map<int, int> h;
int getf(int k) {
  if (f[k] == 0 || f[k] == k) return k;
  return (f[k] = getf(f[k]));
}
int main() {
  cin >> n;
  int v = 0;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), v ^= a[i], c[i] = a[i];
  a[n + 1] = c[n + 1] = v;
  bool pd = 0;
  v = 0;
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]), d[i] = b[i], v ^= b[i];
  b[++n] = d[n] = v;
  sort(c + 1, c + n + 1), sort(d + 1, d + n + 1);
  n1 = 0;
  for (int i = 1; i <= n; i++) {
    if (c[i] != d[i]) {
      printf("-1\n");
      return 0;
    }
    if (h.count(c[i]) == 0) h[c[i]] = ++n1;
    if (h.count(d[i]) == 0) h[d[i]] = ++n1;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = h[a[i]], b[i] = h[b[i]];
    if (a[i] != b[i]) {
      if (i < n) ans++;
      int fx = getf(a[i]), fy = getf(b[i]);
      f[fx] = fy;
    }
  }
  if (ans == 0)
    printf("0\n");
  else {
    for (int i = 1; i <= n1; i++)
      if (getf(i) == i) ans++;
    printf("%d\n", ans - 1);
  }
}
