#include <bits/stdc++.h>
int main() {
  int n;
  while (std::cin >> n) {
    std::vector<int> a(n + 1, 0);
    std::vector<int> b(n, 0);
    for (int i = 0; i < n; i++) std::cin >> a[i];
    for (int i = 0; i < n; i++) std::cin >> b[i];
    for (int i = 0; i < n; i++) a[n] ^= a[i];
    std::map<int, int> freq[2];
    for (int i = 0; i <= n; i++) freq[0][a[i]]++;
    bool valid = true;
    for (int i = 0; i < n; i++) {
      freq[1][b[i]]++;
      if (freq[1][b[i]] > freq[0][b[i]]) valid = false;
    }
    if (!valid) {
      std::cout << "-1\n";
      continue;
    }
    std::set<std::pair<int, int> > to;
    std::set<int> pos;
    for (int i = 0; i < n; i++) {
      if (b[i] != a[i]) {
        pos.insert(i);
        to.insert(std::pair<int, int>(b[i], i));
      }
    }
    int ans = 0;
    while (pos.size()) {
      ans++;
      auto it = to.lower_bound(std::pair<int, int>(a[n], 0));
      if (it == to.end() || it->first != a[n]) {
        auto it2 = pos.begin();
        std::swap(a[*it2], a[n]);
      } else {
        std::swap(a[it->second], a[n]);
        pos.erase(it->second);
        to.erase(it);
      }
    }
    std::cout << ans << '\n';
  }
}
