#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long n;
  cin >> n;
  long long a[n], b[n];
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < n; i++) cin >> b[i];
  long long x = 0;
  for (long long i = 0; i < n; i++) x ^= a[i];
  map<long long, deque<long long> > m;
  map<long long, long long> c;
  for (long long i = 0; i < n; i++) {
    c[a[i]]++;
    c[b[i]]--;
    if (a[i] == b[i]) continue;
    m[b[i]].push_front(i);
  }
  long long g;
  c[x]++;
  for (auto p : c) {
    if (p.second < 0) {
      cout << -1 << endl;
      return 0;
    }
    if (p.second > 0) g = p.first;
  }
  long long ans = 0;
  while (!m.empty()) {
    if (0) {
      cout << x << ":" << endl;
      for (long long i = 0; i < n; i++) cout << a[i] << " ";
      cout << endl;
      for (long long i = 0; i < n; i++) cout << b[i] << " ";
      cout << endl;
      long long w = 0;
      for (long long i = 0; i < n; i++) w ^= a[i];
      assert(w == x);
    }
    ans++;
    if (m.count(x)) {
      long long y = m[x].front();
      m[x].pop_front();
      if (m[x].empty()) m.erase(m.find(x));
      swap(x, a[y]);
    } else {
      long long z = m.begin()->first;
      long long y = m[z].front();
      if (y == g && m.size() > 1) {
        z = (++m.begin())->first;
        y = m[z].front();
      }
      m[z].pop_front();
      if (m[z].empty()) m.erase(m.find(z));
      swap(x, a[y]);
      m[b[y]].push_back(y);
    }
  }
  for (long long i = 0; i < n; i++) assert(a[i] == b[i]);
  cout << ans << endl;
  return 0;
}
