#include <bits/stdc++.h>
auto& in = std::cin;
auto& out = std::cout;
int32_t N;
int32_t a_xor = 0;
int32_t nouse_v;
int32_t a[100000];
int32_t b[100000];
std::pair<int32_t, int32_t> a_copy[100010];
std::pair<int32_t, int32_t> b_copy[100010];
bool is_able() {
  a_copy[N].first = a_xor;
  a_copy[N].second = N;
  std::sort(a_copy, a_copy + N + 1);
  std::sort(b_copy, b_copy + N);
  int adiff = 0;
  for (int32_t i = 0; i < N; ++i) {
    if (b_copy[i].first == a_copy[i + adiff].first) {
    } else {
      if (adiff == 0) {
        nouse_v = a_copy[i + adiff].first;
        b_copy[N].first = a_copy[i + adiff].first;
        b_copy[N].second = N;
        --i;
        ++adiff;
      } else {
        return false;
      }
    }
  }
  std::sort(b_copy, b_copy + N);
  return true;
}
int32_t diff_count = 0;
static bool used[100010] = {};
void func(int ddd) {
  if (ddd == diff_count) {
    out << diff_count << std::endl;
    exit(0);
  }
  auto iter = std::lower_bound(b_copy, b_copy + diff_count + 1,
                               std::pair<int32_t, int32_t>{a_xor, -1});
  auto iter_end =
      std::upper_bound(b_copy, b_copy + diff_count + 1,
                       std::pair<int32_t, int32_t>{a_xor, 10000000});
  for (; iter != iter_end; ++iter) {
    if (iter->second == N || used[iter->second]) {
      continue;
    }
    auto old_xor = a_xor;
    used[iter->second] = true;
    a_xor = a[iter->second];
    func(ddd + 1);
    used[iter->second] = false;
    a_xor = old_xor;
  }
}
int main() {
  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);
  in >> N;
  for (int32_t i = 0; i < N; ++i) {
    in >> a[i];
    a_copy[i].first = a[i];
    a_copy[i].second = i;
    a_xor ^= a[i];
  }
  for (int32_t i = 0; i < N; ++i) {
    in >> b[i];
    b_copy[i].first = b[i];
    b_copy[i].second = i;
  }
  if (is_able() == false) {
    out << -1 << endl;
    return 0;
  }
  for (int32_t i = 0; i < N; ++i) {
    if (a[i] != b[i]) {
      a_copy[diff_count].first = a[i];
      a_copy[diff_count].second = i;
      b_copy[diff_count].first = b[i];
      b_copy[diff_count].second = i;
      ++diff_count;
    }
  }
  b_copy[diff_count].first = nouse_v;
  b_copy[diff_count].second = N;
  std::sort(a_copy, a_copy + diff_count);
  std::sort(b_copy, b_copy + diff_count + 1);
  func(0);
  out << diff_count + 1 << endl;
  return 0;
}
