#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
#include <map>

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) { lhs = rhs; return true; }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) { lhs = rhs; return true; }
  return false;
}

struct range {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) { }
    constexpr void operator ++ () noexcept { ++i; }
    constexpr itr operator * () const noexcept { return i; }
    constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr range(itr l_, itr r_) noexcept : l(l_), r(std::max(l_, r_)) { }
  constexpr iterator begin() const noexcept { return l; }
  constexpr iterator end() const noexcept { return r; }
};

struct revrange {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) { }
    constexpr void operator ++ () noexcept { --i; }
    constexpr itr operator * () const noexcept { return i; }
    constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr revrange(itr l_, itr r_) noexcept : l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr iterator begin() const noexcept { return r; }
  constexpr iterator end() const noexcept { return l; }
};

class union_find {
private:
  size_t component;
  std::vector<int32_t> parent;

public:
  union_find() = default;
  union_find(size_t size_) { init(size_); }

  void init(size_t size_) {
    component = size_;
    parent.assign(size_, -1);
  }

  size_t count_components() const { 
    return component; 
  }
  size_t component_size(size_t i) { 
    return -parent[find_parent(i)]; 
  }
  bool same_component(size_t i, size_t j) { 
    return find_parent(i) == find_parent(j); 
  }

  size_t find_parent(size_t i) {
    if (parent[i] < 0) {
      return i;
    }
    else {
      return parent[i] = find_parent(parent[i]);
    }
  }

  bool unite(size_t i, size_t j) {
    i = find_parent(i);
    j = find_parent(j);
    if (i == j) {
      return false;
    }
    if (parent[i] > parent[j]) {
      std::swap(i, j);
    }
    parent[i] += parent[j];
    parent[j] = i;
    --component;
    return true;
  }

};

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

int main() {
  size_t N;
  std::cin >> N;
  u32 X = 0;
  std::vector<u32> A, B;
  {
    std::vector<u32> A_(N), B_(N);
    std::map<u32, u32> mA, mB;
    for (auto &x: A_) {
      std::cin >> x;
      X ^= x;
      mA[x]++;
    }
    for (auto &x: B_) {
      std::cin >> x;
      mB[x]++;
    }
    mA[X]++;
    bool done = false;
    for (auto p: mB) {
      auto itr = mA.find(p.first);
      if (itr == mA.end()) {
        std::cout << "-1\n";
        return 0;
      }
      if (itr -> second != p.second) {
        if (itr -> second  != p.second + 1) {
          std::cout << "-1\n";
          return 0;
        }
        if (done) {
          std::cout << "-1\n";
          return 0;
        }
        done = true;
      }
    }
    for (auto i: range(0, N)) {
      if (A_[i] != B_[i]) {
        A.push_back(A_[i]);
        B.push_back(B_[i]);
      }
    }
    N = A.size();
  }
  if (N == 0) {
    std::cout << "0\n";
    return 0;
  }
  u32 ans = N;
  std::map<u32, std::vector<u32>> idx;
  union_find uni(N);
  bool done = false;
  for (auto i: range(0, N)) {
    idx[B[i]].push_back(i);
  }
  for (const auto &p: idx) {
    for (auto i: range(1, p.second.size())) {
      uni.unite(p.second[i - 1], p.second[i]);
    }
  }
  for (auto i: range(0, N)) {
    if (B[i] == X && !done) {
      --ans;
      done = true;
    }
    if (!idx[A[i]].empty()) {
      uni.unite(i, idx[A[i]].back());
      idx[A[i]].pop_back();
    }
  }
  std::cout << ans + uni.count_components() << '\n';
  return 0;
}


Measured memory_bytes: 12388