#include <bits/stdc++.h>
using namespace std;
using vint = vector<long long>;
using vvint = vector<vint>;
using pint = pair<long long, long long>;
constexpr long double pi = 3.1415926535897932384626433832795028;
constexpr long long dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr long long dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
template <typename T>
using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
template <typename T1, typename T2>
inline bool chmax(T1 &a, const T2 &b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1 &a, const T2 &b) {
  return b < a && (a = b, true);
}
struct SetUp {
  SetUp() { cout << fixed << setprecision(20); };
} setUp;
long long N;
long long a[110000], b[110000];
multimap<long long, long long> mp;
signed main() {
  cin >> N;
  long long cur = 0;
  for (long long i = 0; i < (long long)N; i++) {
    cin >> a[i];
    cur ^= a[i];
  }
  for (long long i = 0; i < (long long)N; i++) {
    cin >> b[i];
    if (a[i] != b[i]) {
      mp.insert({b[i], a[i]});
    }
  }
  long long ans = 0;
  for (long long i = 0; i < (long long)N * 2; i++) {
    if (mp.empty()) {
      cout << ans << endl;
      return 0;
    }
    ans++;
    auto it = mp.find(cur);
    if (it == mp.end()) {
      it = mp.begin();
    }
    long long nb = it->first, na = it->second;
    mp.erase(it);
    if (nb != cur) {
      mp.insert({nb, cur});
    }
    cur = na;
  }
  cout << -1 << endl;
}
