#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> as(n);
  int s = 0;
  for (auto& a : as) {
    cin >> a;
    s ^= a;
  }
  vector<int> bs(n);
  for (auto& b : bs) {
    cin >> b;
  }
  multiset<int> ss(as.begin(), as.end());
  ss.insert(s);
  for (auto& b : bs) {
    auto it = ss.find(b);
    if (it == ss.end()) {
      cout << -1 << endl;
      return 0;
    }
    ss.erase(it);
  }
  multimap<int, int> mm;
  for (int i = 0; i < n; i++) {
    if (as[i] != bs[i]) {
      mm.insert(make_pair(bs[i], i));
    }
  }
  int ans = 0;
  while (mm.size() > 0) {
    auto it = mm.find(s);
    int i = 0;
    if (it == mm.end()) {
      i = mm.begin()->second;
    } else {
      i = it->second;
      mm.erase(it);
    }
    int t = as[i];
    as[i] = s;
    s = t;
    ans++;
  }
  cout << ans << endl;
  return 0;
}
