#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.sync_with_stdio(false);
  int n;
  cin >> n;
  map<int, int> t;
  vector<int> a;
  int xo = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.push_back(x);
    xo ^= x;
    t[x]++;
  }
  t[xo]++;
  vector<int> b;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    b.push_back(x);
    t[x]--;
    if (t[x] < 0) {
      puts("-1");
      return 0;
    }
  }
  map<int, multiset<int>> ab, ba;
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      ab[a[i]].insert(b[i]);
      ba[b[i]].insert(a[i]);
    }
  }
  int ans = 0;
  while (!ab.empty()) {
    int bx;
    int ax;
    if (ba.find(xo) != end(ba)) {
      bx = xo;
      ax = *begin(ba[bx]);
    } else {
      for (auto it = begin(ab); it != end(ab); ++it) {
        if (it->first != xo) {
          ax = it->first;
          bx = *begin(ab[ax]);
          ab[xo].insert(bx);
          ba[bx].insert(xo);
          break;
        }
      }
    }
    ba[bx].erase(ba[bx].find(ax));
    if (ba[bx].empty()) {
      ba.erase(bx);
    }
    ab[ax].erase(ab[ax].find(bx));
    if (ab[ax].empty()) {
      ab.erase(ax);
    }
    xo = ax;
    ans++;
  }
  printf("%d\n", ans);
  return 0;
}
