#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int N, A[100008], B[100008], c[100008], cn, d[100008], db[100008], ci[100008],
    v[100008], vc[100008], pi[100008];
map<int, int> cc;
vector<int> path[100008];
signed main() {
  if (!false) {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
  int N;
  scanf("%d", &N);
  for (int _i = (N), i = (0); i < _i; i++) {
    scanf("%d", &A[i]);
    A[N] ^= A[i];
  }
  for (int _i = (N), i = (0); i < _i; i++) {
    scanf("%d", &B[i]);
    B[N] ^= B[i];
  }
  for (int _i = (N + 1), i = (0); i < _i; i++) c[i] = A[i];
  sort(c, c + N + 1);
  cn = unique(c, c + N + 1) - c;
  for (int _i = (cn), i = (0); i < _i; i++) cc[c[i]] = i;
  for (int _i = (N + 1), i = (0); i < _i; i++) {
    ++d[cc[A[i]]];
    ++db[cc[B[i]]];
    path[cc[A[i]]].push_back(cc[B[i]]);
  }
  if (false) {
    for (int _i = (cn), i = (0); i < _i; i++) {
      cout << i << '\t' << c[i] << '\t' << cc[c[i]] << '\t' << d[i] << '\t'
           << db[i] << "|";
      for (int j : path[i]) cout << '\t' << j;
      cout << '\n';
    }
    cout << string(100, '-') << '\n';
  }
  for (int _i = (cn), i = (0); i < _i; i++) {
    if (d[i] != db[i]) {
      cout << "-1\n";
      return 0;
    }
  }
  int x = cc[A[N]], ans = A[N] == B[N] ? 0 : -1;
  if (path[x][0] == x) {
    vc[x] = true;
  } else {
    do {
      ++ans;
      vc[x] = true;
      int y;
      do {
        y = path[x][pi[x]];
        ++pi[x];
      } while (y != cc[A[N]] && x == y);
      x = y;
      if (false) cout << y << ">\t";
    } while (x != cc[A[N]]);
  }
  if (false) cout << ans << '\n';
  for (int _i = (cn), i = (0); i < _i; i++)
    while (pi[i] < path[i].size()) {
      if (path[i][pi[i]] == i) {
        pi[i]++;
        continue;
      }
      bool independent = true;
      int x = i;
      do {
        ++ans;
        if (vc[x]) independent = false;
        int y;
        do {
          y = path[x][pi[x]];
          ++pi[x];
        } while (y != i && x == y);
        x = y;
        if (false) cout << y << ">\t";
      } while (x != i);
      ans += independent;
      if (false) cout << i << "\t" << independent << "\t" << ans << '\n';
    }
  cout << ans << '\n';
  return 0;
}
