#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
class UnionFindTree {
 private:
  std::vector<int> m_parent;
  std::vector<int> m_rank;

 public:
  explicit UnionFindTree(int n = 0) : m_parent(n), m_rank(n) {
    for (int i = 0; i < n; ++i) {
      m_parent[i] = i;
    }
  }
  int find(int x) {
    if (m_parent[x] == x) {
      return x;
    }
    m_parent[x] = find(m_parent[x]);
    return m_parent[x];
  }
  int unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) {
      return x;
    }
    if (m_rank[x] < m_rank[y]) {
      m_parent[x] = y;
      return y;
    } else if (m_rank[x] > m_rank[y]) {
      m_parent[y] = x;
      return x;
    } else {
      m_parent[y] = x;
      ++m_rank[x];
      return x;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
  }
  int a_sum = 0, b_sum = 0;
  for (int i = 0; i < n; ++i) {
    a_sum ^= a[i];
    b_sum ^= b[i];
  }
  a.push_back(a_sum);
  b.push_back(b_sum);
  vector<int> c, d;
  unordered_multimap<int, int> d_map;
  for (int i = 0; i <= n; ++i) {
    if (a[i] == b[i]) {
      continue;
    }
    c.push_back(a[i]);
    d.push_back(b[i]);
    d_map.emplace(d.back(), d.size() - 1);
  }
  const int m = c.size();
  if (m == 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> g(m);
  for (int i = 0; i < m; ++i) {
    g[i] = i;
  }
  for (int i = 0; i < m; ++i) {
    if (c[i] == d[i]) {
      continue;
    }
    int last = c[i];
    g[i] = i;
    while (d_map.find(last) != d_map.end()) {
      const auto it = d_map.find(last);
      const int j = it->second;
      const int t = c[j];
      c[j] = last;
      g[j] = i;
      last = t;
      d_map.erase(it);
      if (j == i) {
        break;
      }
    }
  }
  if (!d_map.empty()) {
    cout << -1 << endl;
    return -1;
  }
  vector<vector<int>> h(m);
  for (int i = 0; i < m; ++i) {
    h[g[i]].push_back(c[i]);
  }
  int answer = m - 1;
  unordered_map<int, int> occ;
  UnionFindTree uft(m);
  for (int i = 0; i < m; ++i) {
    const auto& v = h[i];
    if (v.empty()) {
      continue;
    }
    ++answer;
    for (const auto x : v) {
      const auto it = occ.find(x);
      if (it == occ.end()) {
        continue;
      }
      if (!uft.same(i, it->second)) {
        uft.unite(i, it->second);
        --answer;
      }
    }
    for (const auto x : v) {
      occ.emplace(x, i);
    }
  }
  if (a_sum != b_sum) {
    --answer;
  }
  cout << answer << endl;
  return 0;
}
