#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (int)(n); ++i) cin >> a[i];
  vector<int> b(n);
  for (int i = 0; i < (int)(n); ++i) cin >> b[i];
  int sa = 0;
  int sb = 0;
  for (int i = 0; i < (int)(n); ++i) {
    sa ^= a[i];
    sb ^= b[i];
  }
  a.push_back(sa);
  b.push_back(sb);
  n++;
  map<int, int> mpa, mpb;
  for (int i = 0; i < (int)(n); ++i) {
    mpa[a[i]]++;
    mpb[b[i]]++;
  }
  for (auto x : mpa) {
    if (x.second != mpb[x.first]) {
      cout << -1 << endl;
      return 0;
    }
  }
  int c = 0;
  map<int, multiset<int> > mp;
  for (int i = 0; i < (int)(n - 1); ++i) {
    if (a[i] != b[i]) {
      c++;
      mp[b[i]].insert(a[i]);
    }
  }
  if (c == 0) {
    cout << 0 << endl;
  } else {
    int p = sa;
    int res = 0;
    while (!mp.empty()) {
      if (mp.find(p) != mp.end()) {
        auto& st = mp[p];
        auto x = st.begin();
        res++;
        int pre = p;
        p = *x;
        st.erase(x);
        if (st.size() == 0) {
          mp.erase(pre);
        }
      } else {
        auto st = mp.begin();
        auto x = (*st).second.begin();
        res++;
        mp[(*st).first].insert(p);
        p = *x;
        (*st).second.erase(x);
        if ((*st).second.size() == 0) {
          mp.erase(st);
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}
