#include <bits/stdc++.h>
auto& in = std::cin;
auto& out = std::cout;
const double PI =
    3.141592653589793238462643383279502884197169399375105820974944;
int32_t N;
int32_t A[100001];
int32_t B[100001];
int32_t A_tmp[100001];
int32_t B_tmp[100001];
bool used[100001];
std::map<int32_t, std::vector<int32_t>> graph;
int main() {
  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);
  in.tie(nullptr);
  out.tie(nullptr);
  in >> N;
  for (int32_t i = 0; i < N; i++) {
    in >> A[i];
    A_tmp[i] = A[i];
    A_tmp[N] = A[N] ^= A[i];
  }
  for (int32_t i = 0; i < N; i++) {
    in >> B[i];
    B_tmp[i] = B[i];
    B_tmp[N] = B[N] ^= B[i];
  }
  std::sort(A_tmp, A_tmp + N + 1);
  std::sort(B_tmp, B_tmp + N + 1);
  if (!std::equal(A_tmp, A_tmp + N + 1, B_tmp)) {
    out << -1 << endl;
    return 0;
  }
  for (int32_t i = 0; i <= N; i++) {
    graph[B[i]].push_back(i);
  }
  for (int32_t i = 0; i <= N; i++) {
    if (A[i] == B[i]) {
      used[i] = true;
    }
  }
  std::queue<int> que;
  int32_t res = 0;
  used[N] = true;
  que.push(N);
  while (!que.empty()) {
    auto v = que.front();
    que.pop();
    for (auto& n : graph[A[v]]) {
      if (!used[n]) {
        used[n] = true;
        ++res;
        que.push(n);
      }
    }
  }
  for (int32_t i = 0; i <= N; i++) {
    if (!used[i]) {
      res += 2;
      used[i] = true;
      que.push(i);
      while (!que.empty()) {
        auto v = que.front();
        que.pop();
        for (auto& n : graph[v]) {
          if (!used[n]) {
            used[n] = true;
            ++res;
            que.push(n);
          }
        }
      }
    }
  }
  out << res << endl;
  return 0;
}
