#include <bits/stdc++.h>
using namespace std;
class merge_find {
  int nr;
  vector<int> v;

 public:
  merge_find(const int n) : nr(n), v(n, 0) { iota(begin(v), end(v), 0); }
  int find(int x) { return x == v[x] ? x : v[x] = find(v[x]); }
  void merge(int a, int b) {
    a = find(a), b = find(b);
    if (a == b) return;
    if (rand() % 2 == 0) swap(a, b);
    v[b] = a;
    --nr;
  }
  int get_nr() { return nr; }
};
int min_nr_moves_to_permut_with_last_element_ops(vector<int> v1,
                                                 vector<int> v2) {
  for (const auto x : v1) cerr << x << ' ';
  cerr << endl;
  for (const auto x : v2) cerr << x << ' ';
  cerr << endl;
  if (!is_permutation(begin(v1), end(v1), begin(v2))) return -1;
  map<int, int> norm;
  int cur = 0;
  for (auto x : v1)
    if (norm.find(x) == end(norm)) norm[x] = cur++;
  for (auto& x : v1) x = norm[x];
  for (auto& x : v2) x = norm[x];
  merge_find mf(cur);
  for (const auto x : v1) cerr << x << ' ';
  cerr << endl;
  for (const auto x : v2) cerr << x << ' ';
  cerr << endl;
  set<int> solitary;
  for (int i = 0; i < cur; ++i) solitary.insert(i);
  for (int i = 0; i < v1.size(); ++i) mf.merge(v1[i], v2[i]);
  for (int i = 0; i < v1.size(); ++i)
    if (v1[i] != v2[i]) solitary.erase(v1[i]);
  cerr << v1.size() << ' ' << mf.get_nr() << ' ' << solitary.size() << endl;
  return v1.size() - 1 + mf.get_nr() - 1 - solitary.size() - 1 +
         (v1.back() == v2.back());
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (auto& x : a) cin >> x;
  for (auto& x : b) cin >> x;
  a.push_back(accumulate(begin(a), end(a), 0, bit_xor<int>()));
  b.push_back(accumulate(begin(b), end(b), 0, bit_xor<int>()));
  cout << min_nr_moves_to_permut_with_last_element_ops(a, b) << endl;
  return 0;
}
