#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100500;
int a[MAXN], b[MAXN];
int n;
vector<int> va, vb;
vector<int> last[MAXN], todel[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];
  for (int i = 0; i < n; i++) {
    a[n] ^= a[i];
    b[n] ^= b[i];
  }
  for (int i = 0; i <= n; i++) {
    va.push_back(a[i]);
    vb.push_back(b[i]);
  }
  sort(va.begin(), va.end());
  sort(vb.begin(), vb.end());
  if (va != vb) {
    cout << -1 << '\n';
    return 0;
  }
  va.resize(unique(va.begin(), va.end()) - va.begin());
  for (int i = 0; i <= n; i++) {
    a[i] = lower_bound(va.begin(), va.end(), a[i]) - va.begin();
    b[i] = lower_bound(va.begin(), va.end(), b[i]) - va.begin();
  }
  int need = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      if (a[i] == b[n])
        last[b[i]].push_back(i);
      else
        todel[b[i]].push_back(i);
      need++;
    }
  }
  int sz_todel = n;
  while (sz_todel && todel[sz_todel - 1].empty()) sz_todel--;
  int ans = 0;
  while (need != 0) {
    if (a[n] == b[n]) {
      assert(last[a[n]].empty());
      if (!todel[a[n]].empty()) {
        int ind = todel[a[n]].back();
        todel[a[n]].pop_back();
        swap(a[ind], a[n]);
        need--;
        ans++;
      } else {
        while (sz_todel && todel[sz_todel - 1].empty()) sz_todel--;
        assert(sz_todel != 0);
        assert(!todel[sz_todel - 1].empty());
        int ind = todel[sz_todel - 1].back();
        todel[sz_todel - 1].pop_back();
        swap(a[ind], a[n]);
        ans++;
        last[b[ind]].push_back(ind);
      }
    } else {
      if (!todel[a[n]].empty()) {
        int ind = todel[a[n]].back();
        todel[a[n]].pop_back();
        swap(a[ind], a[n]);
        ans++;
        need--;
        continue;
      }
      if (!last[a[n]].empty()) {
        int ind = last[a[n]].back();
        last[a[n]].pop_back();
        swap(a[ind], a[n]);
        ans++;
        need--;
        continue;
      }
      assert(false);
    }
  }
  cout << ans << '\n';
  return 0;
}
