#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000000 + 7;
map<int, deque<int> > e;
map<int, int> cnt;
std::vector<int> a, b;
void solve() {
  int n, x, y;
  y = 0;
  cin >> n;
  cnt.clear();
  for (int i = 0; i < int(n); ++i) {
    cin >> x;
    a.push_back(x);
    y ^= x;
    cnt[x]++;
  }
  cnt[y]++;
  bool no = 0;
  for (int i = 0; i < int(n); ++i) {
    cin >> x;
    b.push_back(x);
    cnt[x]--;
    if (cnt[x] < 0) no = 1;
  }
  if (no) {
    cout << -1;
    return;
  }
  int ans = 0;
  cnt.clear();
  e.clear();
  for (int i = 0; i < int(n); ++i) {
    if (a[i] != b[i]) e[b[i]].push_back(a[i]);
    cnt[a[i]]++;
    cnt[b[i]]--;
  }
  cnt[y]++;
  int more = -1;
  for (auto p : cnt) {
    if (p.second > 0) {
      more = p.first;
    }
  }
  for (auto p : e) {
    int k = p.first;
    for (int i = 0, j = 0; i < e[k].size(); ++i) {
      if (e[k][i] == more) {
        swap(e[k][i], e[k][j]);
        j++;
      }
    }
  }
  {
    while (!e.empty()) {
      int z = y;
      if (e.count(z) > 0) {
        y = e[z].back();
        e[z].pop_back();
      } else {
        std::vector<int> zs;
        zs.clear();
        for (auto p : e) {
          zs.push_back(p.first);
        }
        for (int _ : zs) {
          z = _;
          bool done = 0;
          for (int i = e[z].size() - 1; i >= 0; --i) {
            if (e[z][i] != y) {
              int tmp = e[z][i];
              swap(e[z][i], e[z][e[z].size() - 1]);
              e[z].pop_back();
              e[z].push_front(y);
              y = tmp;
              done = 1;
              break;
            }
          }
          if (done) break;
        }
      }
      ans++;
      if (e[z].empty()) {
        e.erase(z);
      }
    }
  }
  cout << ans;
}
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(0);
  solve();
}
