#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;
    }
  }
  int cnt = 0;
  map<int, set<int>> mpa, mpb;
  for (int i = 0; i < (n); ++i)
    if (a[i] != b[i]) {
      mpa[a[i]].insert(i);
      mpb[b[i]].insert(i);
      ++cnt;
    }
  int ans = 0;
  while (true) {
    if (not mpb[a[n]].empty()) {
      int x = *begin(mpb[a[n]]);
      mpa[a[x]].erase(x);
      mpb[b[x]].erase(x);
      swap(a[x], a[n]);
      --cnt;
    } else {
      if (not cnt) break;
      vector<int> vec;
      int x = -1;
      for (auto p : mpa) {
        if (p.second.empty()) {
          vec.push_back(p.first);
          continue;
        }
        if (p.first == a[n]) continue;
        x = *begin(p.second);
      }
      for (auto v : vec) mpa.erase(v);
      mpa[a[x]].erase(x);
      mpb[b[x]].erase(x);
      swap(a[x], a[n]);
      if (a[x] != b[x]) {
        mpa[a[x]].insert(x);
        mpb[b[x]].insert(x);
      }
    }
    ++ans;
  }
  cout << ans << "\n";
  return 0;
}
