#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000000 + 7;
map<int, map<int, 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]][a[i]]++;
    cnt[a[i]]++;
    cnt[b[i]]--;
  }
  cnt[y]++;
  int more = -1;
  for (auto p : cnt) {
    if (p.second > 0) {
      more = p.first;
    }
  }
  {
    while (!e.empty()) {
      int z = y;
      int w = -1;
      if (e.find(z) != e.end()) {
        map<int, int>::iterator iter = e[z].begin();
        if (iter->first == more && e[z].size() > 1) iter++;
        w = iter->first;
        e[z][w]--;
        if (e[z][w] == 0) e[z].erase(w);
        y = w;
      } else {
        w = -1;
        for (map<int, map<int, int> >::iterator it = e.begin(); it != e.end();
             ++it) {
          z = it->first;
          map<int, int>::iterator iter = e[z].begin();
          while (iter != e[z].end()) {
            if (iter->first != more && iter->first != y) {
              w = iter->first;
              break;
            }
          }
          if (w != -1) break;
        }
        if (w == -1) {
          for (map<int, map<int, int> >::iterator it = e.begin(); it != e.end();
               ++it) {
            z = it->first;
            map<int, int>::iterator iter = e[z].begin();
            while (iter != e[z].end()) {
              if (iter->first != y) {
                w = iter->first;
                break;
              }
            }
            if (w != -1) break;
          }
        }
        e[z][w]--;
        if (e[z][w] == 0) e[z].erase(w);
        e[z][y]++;
        y = w;
      }
      ans++;
      if (e[z].empty()) {
        e.erase(z);
      }
    }
  }
  cout << ans;
}
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(0);
  solve();
}
