#include <bits/stdc++.h>
std::map<int, int> mp;
std::vector<int> g[100003];
int ans, k;
bool viz[100003];
int t[100003];
int a[100003], b[100003];
int v[100003];
int ind[100003], per[100003];
int sef(int x) {
  if (x == t[x])
    return x;
  else
    return t[x] = sef(t[x]);
}
inline void baga(int i) {
  if (a[i] == b[i]) return;
  ans++;
  int &u = mp[b[i]];
  if (u == 0) u = ++k;
  g[u].push_back(i);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    a[n + 1] ^= a[i];
  }
  bool ok = 0;
  int k = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    b[n + 1] ^= b[i];
    ok |= (b[i] == a[n + 1]);
    baga(i);
  }
  baga(n + 1);
  if (a[n + 1] != b[n + 1]) ans--;
  int poz = 1;
  bool sol = 1;
  while (poz <= n + 1 && sol) {
    if (a[poz] != b[poz]) {
      int &u = mp[a[poz]];
      if (ind[u] < (int)g[u].size()) {
        v[poz] = g[u][ind[u]];
        per[g[u][ind[u]]] = poz;
        ind[u]++;
      } else
        sol = 0;
    }
    poz++;
  }
  if (sol == 0) {
    printf("-1\n");
    return 0;
  }
  for (int i = 1; i <= n + 1; i++) {
    if (viz[i] == 0 && v[i]) {
      int x = i;
      t[x] = i;
      ans++;
      viz[x] = 1;
      x = v[x];
      while (viz[x] == 0) {
        viz[x] = 1;
        t[x] = i;
        x = v[x];
      }
    }
  }
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j < (int)g[i].size(); j++) {
      int x = sef(per[g[i][j - 1]]), y = sef(per[g[i][j]]);
      if (x != y) {
        ans--;
        t[x] = y;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
