#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:512000000")
using namespace std;
void solve(bool);
void precalc();
clock_t start;
int main() {
  start = clock();
  int t = 1;
  cout.sync_with_stdio(0);
  cin.tie(0);
  precalc();
  cout.precision(10);
  cout << fixed;
  int testNum = 1;
  while (t--) {
    solve(true);
  }
  cout.flush();
  return 0;
}
template <typename T>
T binpow(T q, T w, T mod) {
  if (!w) return 1 % mod;
  if (w & 1) return q * 1LL * binpow(q, w - 1, mod) % mod;
  return binpow(q * 1LL * q % mod, w / 2, mod);
}
template <typename T>
T gcd(T q, T w) {
  while (w) {
    q %= w;
    swap(q, w);
  }
  return q;
}
template <typename T>
T lcm(T q, T w) {
  return q / gcd(q, w) * w;
}
void precalc() {}
template <typename T>
void relax_min(T& cur, T val) {
  cur = min(cur, val);
}
template <typename T>
void relax_max(T& cur, T val) {
  cur = max(cur, val);
}
void solve(bool read) {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  int all_xor = 0;
  map<int, int> nums_a;
  vector<int> vals;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    all_xor ^= a[i];
    ++nums_a[a[i]];
    vals.push_back(a[i]);
  }
  map<int, int> nums_b;
  int ham = 0;
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    if (b[i] != a[i]) {
      ++ham;
    }
    ++nums_b[b[i]];
    vals.push_back(b[i]);
  }
  if (ham == 0) {
    cout << 0 << endl;
    return;
  }
  sort(vals.begin(), vals.end());
  vals.erase(unique(vals.begin(), vals.end()), vals.end());
  vector<int> a_vals, b_vals;
  vector<int> diffs;
  for (int x : vals) {
    a_vals.push_back(nums_a[x]);
    b_vals.push_back(nums_b[x]);
    if (a_vals.back() != b_vals.back()) {
      diffs.push_back(x);
    }
  }
  if (diffs.size() > 2) {
    cout << "-1\n";
    return;
  }
  assert(diffs.size() != 1);
  int cur_ans = 0;
  if (diffs.size() == 2) {
    if (diffs[0] != all_xor) {
      swap(diffs[0], diffs[1]);
    }
    if (diffs[0] != all_xor) {
      cout << "-1\n";
      return;
    }
    if (nums_a[all_xor] + 1 != nums_b[all_xor]) {
      cout << "-1\n";
      return;
    }
    for (int i = 0; i < n; ++i) {
      if (a[i] == diffs[1]) {
        a[i] = all_xor;
        break;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    if (a[i] != b[i]) {
      ++cur_ans;
    }
  }
  set<int> not_used;
  map<int, vector<int>> g;
  for (int i = 0; i < n; ++i) {
    g[a[i]].push_back(b[i]);
    g[b[i]].push_back(a[i]);
    not_used.insert(a[i]);
  }
  for (auto& item : g) {
    sort(item.second.begin(), item.second.end());
    item.second.erase(unique(item.second.begin(), item.second.end()),
                      item.second.end());
  }
  while (!not_used.empty()) {
    int v = *not_used.begin();
    not_used.erase(not_used.begin());
    vector<int> q;
    q.push_back(v);
    for (int i = 0; i < q.size(); ++i) {
      int cur_v = q[i];
      for (int to : g[cur_v]) {
        if (not_used.count(to)) {
          not_used.erase(to);
          q.push_back(to);
        }
      }
    }
    if (q.size() > 1) {
      bool f = false;
      for (int cur_v : q) {
        if (cur_v == all_xor) {
          f = true;
          break;
        }
      }
      if (!f) {
        ++cur_ans;
      }
    }
  }
  cout << cur_ans << endl;
}
