#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmin(T &a, const T &b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool chmax(T &a, const T &b) {
  return a < b ? (a = b, true) : false;
}
int nextInt() {
  int x;
  scanf("%d", &x);
  return x;
}
int exor_all(const vector<int> &v) {
  int res = 0;
  for (int i = 0, _n = (int)(v.size()); i < _n; ++i) res ^= v[i];
  return res;
}
int main2() {
  int N = nextInt();
  vector<int> a(N), b(N);
  for (int i = 0, _n = (int)(N); i < _n; ++i) a[i] = nextInt();
  for (int i = 0, _n = (int)(N); i < _n; ++i) b[i] = nextInt();
  int xa = exor_all(a);
  {
    vector<int> ta = a;
    vector<int> tb = b;
    ta.push_back(xa);
    tb.push_back(exor_all(b));
    sort(ta.begin(), ta.end());
    sort(tb.begin(), tb.end());
    if (ta != tb) {
      cout << -1 << endl;
      return 0;
    }
  }
  map<int, deque<int>> mp;
  for (int i = 0, _n = (int)(N); i < _n; ++i)
    if (b[i] != a[i]) {
      mp[b[i]].push_back(i);
    }
  int ans = 0;
  int hold = xa;
  for (;;) {
    int bi = hold;
    while (!(mp.count(bi) > 0 && mp[bi].size() > 0)) {
      if (mp.count(bi) > 0) mp.erase(bi);
      if (mp.size() == 0) goto END;
      bi = mp.begin()->first;
    }
    auto &q = mp[bi];
    int i = q.back();
    q.pop_back();
    int next_hold = a[i];
    if (hold == next_hold) continue;
    ans++;
    a[i] = hold;
    if (b[i] != a[i]) {
      mp[b[i]].push_back(i);
    }
    hold = next_hold;
  }
END:
  cout << ans << endl;
  return 0;
}
int main() {
  for (; !cin.eof(); cin >> ws) main2();
  return 0;
}
