#include <bits/stdc++.h>
using namespace std;
long long mod(long long a) {
  long long c = a % (long long)(1E9 + 7);
  if (c >= 0) return c;
  return c + (long long)(1E9 + 7);
}
long long inf = (long long)1E17;
long long max(long long a, long long b) {
  if (a < b)
    return b;
  else
    return a;
}
long long min(long long a, long long b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(long long &pos, long long val) { pos = max(pos, val); }
void Min(long long &pos, long long val) { pos = min(pos, val); }
void Add(long long &pos, long long val) { pos = mod(pos + val); }
long double EPS = 1E-9;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long a[n + 1], b[n];
  a[n] = 0;
  for (long long i = 0; i <= n - 1; i++) {
    cin >> a[i];
    a[n] ^= a[i];
  }
  multiset<long long> c;
  for (long long i = 0; i <= n - 1; i++) {
    cin >> b[i];
    c.insert(b[i]);
  }
  for (long long i = 0; i <= n; i++) {
    auto itr = c.find(a[i]);
    if (itr != c.end()) {
      c.erase(itr);
    }
  }
  if (c.size() >= 1) {
    cout << -1 << endl;
    return 0;
  }
  multiset<pair<long long, long long> > s;
  for (long long i = 0; i <= n - 1; i++) {
    if (a[i] == b[i]) continue;
    s.insert(pair<long long, long long>(b[i], a[i]));
  }
  long long ans = 0;
  long long res = a[n];
  while (!s.empty()) {
    ans++;
    auto itr = s.lower_bound(pair<long long, long long>(res, -inf));
    if (itr == s.end()) itr--;
    if (res == (*itr).first) {
      res = (*itr).second;
      s.erase(itr);
    } else {
      pair<long long, long long> k = *itr;
      s.erase(itr);
      swap(k.second, res);
      s.insert(k);
    }
  }
  cout << ans << endl;
  return 0;
}
