#include <bits/stdc++.h>
using namespace std;
template <typename T>
string join(vector<T> &v) {
  stringstream s;
  for (int i = (0); i < ((int)(v).size()); i++) s << ' ' << v[i];
  return s.str().substr(1);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &v) {
  fill((T *)array, (T *)(array + N), v);
}
__int128_t gcd(__int128_t a, __int128_t b, __int128_t &x, __int128_t &y) {
  if (!b) {
    x = 1;
    y = 0;
    return a;
  }
  __int128_t d = gcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
long long gcd(long long a, long long b) {
  __int128_t x = 0, y = 0;
  return gcd(a, b, x, y);
}
long long modpow(__int128_t a, __int128_t n, long long m) {
  if (!a) return a;
  __int128_t p = 1;
  for (; n > 0; n >>= 1, a = a * a % m)
    if (n & 1) p = p * a % m;
  return (long long)p;
}
bool isprime(long long n) {
  if (n < 2) return 0;
  if (!(n % 2)) return !(n - 2);
  __int128_t d = n - 1, t, y;
  while (!(d % 2)) d >>= 1;
  vector<long long> al = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
  for (long long a : al) {
    if (n <= a) break;
    t = d, y = modpow(a, t, n);
    while (t != n - 1 && y != 1 && y != n - 1) y = y * y % n, t <<= 1;
    if (y != n - 1 && !(t % 2)) return 0;
  }
  return 1;
}
void dout(double d) { printf("%.12f\n", d); }
void YN(bool z) { cout << (z ? "YES\n" : "NO\n"); }
void Yn(bool z) { cout << (z ? "Yes\n" : "No\n"); }
void yn(bool z) { cout << (z ? "yes\n" : "no\n"); }
const int iinf = 1e9 + 6;
const long long linf = 1e18;
const int mod = 1e9 + 7;
const double pi = acos(-1);
const double eps = 1e-10;
const int N = 1e5;
int n, a[N], b[N], d, ans;
map<int, set<int> > m;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  int x = 0;
  for (int i = (0); i < (n); i++) {
    cin >> a[i];
    x ^= a[i];
  }
  for (int i = (0); i < (n); i++) {
    cin >> b[i];
    if (a[i] != b[i]) {
      m[b[i]].insert(i);
      d++;
    }
  }
  while (d) {
    ans++;
    if (ans > n * 2) {
      cout << -1 << endl;
      return 0;
    }
    if (m.find(x) == m.end()) {
      map<int, set<int> >::iterator itr = m.begin();
      swap(a[*(itr->second.begin())], x);
    } else {
      map<int, set<int> >::iterator itr = m.find(x);
      swap(a[*(itr->second.begin())], x);
      itr->second.erase(itr->second.begin());
      if (itr->second.empty()) m.erase(itr);
      d--;
    }
  }
  cout << ans << endl;
  return 0;
}
