#include <bits/stdc++.h>
using namespace std;
signed main() {
  int n;
  cin >> n;
  vector<int> a(n + 1), b(n + 1);
  int x = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[n] ^= a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    b[n] ^= b[i];
  }
  auto aa = a;
  auto bb = b;
  sort(aa.begin(), aa.end());
  sort(bb.begin(), bb.end());
  if (aa != bb) {
    cout << -1 << endl;
    return 0;
  }
  set<pair<int, int> > m;
  for (int i = 0; i < n; i++) {
    if (b[i] != a[i]) m.insert({b[i], i});
  }
  int ans = 0;
  while (!m.empty()) {
    pair<int, int> tar = {a[n], -1};
    auto it = m.lower_bound(tar);
    if (it == m.end() || it->first != a[n]) {
      auto it = m.begin();
      pair<int, int> v = *it;
      swap(a[n], a[v.second]);
    } else {
      if (a[it->second] == b[n]) {
        auto next = it;
        next++;
        if (next != m.end() && next->first == a[n]) {
          it = next;
        }
      }
      pair<int, int> v = *it;
      int ind = v.second;
      m.erase(it);
      swap(a[n], a[ind]);
    }
    ans++;
  }
  cout << ans << endl;
}
