#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, M = 2e6 + 5, mo = 1e9 + 7;
struct rec {
  int x, y;
} tmp;
bool operator<(rec a, rec b) { return a.x < b.x || a.x == b.x && a.y < b.y; }
map<int, int> Ref;
int n, i, x, a[N], b[N], c[N], d[N], pp, ans, y, fa[N], tt, act[N];
int get(int x) {
  if (fa[x] == x) return x;
  return fa[x] = get(fa[x]);
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", a + i);
    x ^= a[i];
  }
  a[n + 1] = x;
  x = 0;
  for (i = 1; i <= n; i++) {
    scanf("%d", b + i);
    x ^= b[i];
  }
  b[++n] = x;
  for (i = 1; i <= n; i++) c[i] = a[i], d[i] = a[i];
  sort(c + 1, c + 1 + n);
  sort(d + 1, d + 1 + n);
  pp = 1;
  for (i = 1; i <= n; i++)
    if (c[i] != d[i]) pp = 0;
  if (!pp)
    printf("-1\n");
  else {
    c[0] = -1;
    for (i = 1; i <= n; i++) {
      if (c[i] != c[i - 1]) tt++;
      Ref[c[i]] = tt;
    }
    for (i = 1; i <= tt; i++) fa[i] = i;
    for (i = 1; i <= n; i++)
      if (a[i] != b[i]) {
        ans += (i < n);
        act[Ref[a[i]]] = act[Ref[b[i]]] = 1;
        fa[get(Ref[a[i]])] = get(Ref[b[i]]);
      }
    if (ans) {
      for (i = 1; i <= tt; i++)
        if (act[i] && fa[i] == i) ans++;
      ans--;
    }
    printf("%d\n", ans);
  }
}
