#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  int x = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    x ^= a[i];
  }
  for (int i = 0; i < n; ++i) cin >> b[i];
  vector<int> oa = a, ob = b;
  oa.push_back(x);
  sort(oa.begin(), oa.end());
  sort(ob.begin(), ob.end());
  int ndiff = 0;
  for (int i = 0, j = 0; i < n; ++i, ++j) {
    if (oa[j] != ob[i]) {
      if (++ndiff == 1) {
        --i;
        continue;
      }
    }
  }
  if (1 < ndiff) {
    cout << "-1\n";
    return 0;
  }
  *(int*)0 = 0;
  vector<pair<int, int> > o;
  for (int i = 0; i < n; ++i) {
    if (a[i] != b[i]) {
      o.push_back(make_pair(b[i], i));
    }
  }
  sort(o.begin(), o.end());
  int p[100111];
  map<int, int*> mp;
  for (int i = 0; i < o.size(); ++i) {
    p[i] = o[i].second;
    mp[o[i].first] = p + i;
  }
  int ans = 0;
  int left = o.size();
  while (0 < left) {
    map<int, int*>::iterator it;
    bool ok = 1;
    if ((it = mp.find(x)) == mp.end()) {
      it = mp.begin();
      ok = 0;
    }
    int targetp = *(it->second);
    if (ok) {
      left--;
      it->second--;
      if (it->second < p || b[*it->second] != it->first) mp.erase(it);
    }
    ++ans;
    x = a[targetp];
  }
  cout << ans << "\n";
  return 0;
}
