#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003;
template <typename T>
inline bool chmin(T& x, const T& y) {
  if (x > y) {
    x = y;
    return 1;
  }
  return 0;
}
template <typename T>
inline bool chmax(T& x, const T& y) {
  if (x < y) {
    x = y;
    return 1;
  }
  return 0;
}
struct IOSET {
  IOSET() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(10);
  }
} ioset;
int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1, 0), b(n + 1, 0);
  for (int i = 0; i < (n); ++i) cin >> a[i];
  for (int i = 0; i < (n); ++i) cin >> b[i];
  for (int i = 0; i < (n); ++i) a[n] ^= a[i];
  for (int i = 0; i < (n); ++i) b[n] ^= b[i];
  map<int, int> a_cnt, b_cnt;
  for (int i = 0; i < (n + 1); ++i) ++a_cnt[a[i]];
  for (int i = 0; i < (n + 1); ++i) ++b_cnt[b[i]];
  for (auto p : a_cnt) {
    if (p.second != b_cnt[p.first]) {
      cout << -1 << "\n";
      return 0;
    }
  }
  map<int, set<int>> mp;
  for (int i = 0; i < (n); ++i)
    if (a[i] != b[i]) mp[b[i]].insert(i);
  int ans = 0, k = 0;
  while (true) {
    if (not mp[a[n]].empty()) {
      int x = *begin(mp[a[n]]);
      mp[a[n]].erase(x);
      swap(a[x], a[n]);
    } else {
      while (k <= n) {
        if (a[k] != b[k])
          break;
        else
          ++k;
      }
      if (k == n + 1) break;
      swap(a[k], a[n]);
      if (a[k] == b[k]) mp[b[k]].erase(k);
    }
    ++ans;
  }
  cout << ans << "\n";
  return 0;
}
