#include <bits/stdc++.h>
using namespace std;
template <class T>
void debug(T itr1, T itr2) {
  auto now = itr1;
  while (now < itr2) {
    cout << *now << " ";
    now++;
  }
  cout << endl;
}
const long long MOD = pow(10, 9) + 7;
const long long LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;
vector<long long> fac;
void c_fac(int x = pow(10, 6) + 10) {
  fac.resize(x, true);
  for (int i = (int)0; i < (int)x; i++) fac[i] = i ? (fac[i - 1] * i) % MOD : 1;
}
long long inv(long long a, long long m = MOD) {
  long long b = m, x = 1, y = 0;
  while (b != 0) {
    int d = a / b;
    a -= b * d;
    swap(a, b);
    x -= y * d;
    swap(x, y);
  }
  return (x + m) % m;
}
long long nck(long long n, long long k) {
  return fac[n] * inv(fac[k] * fac[n - k] % MOD) % MOD;
}
long long gcd(long long a, long long b) {
  if (a < b) swap(a, b);
  return b == 0 ? a : gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
struct UnionFind {
  vector<int> par, s, e;
  UnionFind(int N) : par(N), s(N), e(N) {
    for (int i = (int)0; i < (int)N; i++) {
      par[i] = i;
      s[i] = 1;
      e[i] = 0;
    }
  }
  int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); }
  int size(int x) { return par[x] == x ? s[x] : s[x] = size(root(x)); }
  int edge(int x) { return par[x] == x ? e[x] : e[x] = edge(root(x)); }
  void unite(int x, int y) {
    int rx = root(x), ry = root(y);
    if (rx != ry) {
      s[rx] += s[ry];
      e[rx] += e[ry] + 1;
      par[ry] = rx;
    } else
      e[rx]++;
  }
  bool same(int x, int y) {
    int rx = root(x), ry = root(y);
    return rx == ry;
  }
};
vector<long long> zaatsu(vector<long long> &list) {
  map<long long, long long> num;
  for (auto x : list) num[x]++;
  long long p = 0;
  for (auto x : num) {
    num[x.first] = p;
    p++;
  }
  vector<long long> result;
  for (auto x : list) result.push_back(num[x]);
  return result;
}
int main() {
  int N;
  cin >> N;
  vector<long long> a(N + 1, 0);
  for (int i = (int)0; i < (int)N; i++) {
    cin >> a[i];
    a[N] ^= a[i];
  }
  vector<long long> b(N + 1, 0);
  for (int i = (int)0; i < (int)N; i++) {
    cin >> b[i];
    b[N] ^= b[i];
  }
  map<long long, long long> ca, cb;
  for (int i = (int)0; i < (int)N + 1; i++) {
    ca[a[i]]++;
    cb[b[i]]++;
  }
  bool ok = true;
  for (auto x : ca) ok &= cb[x.first] == x.second;
  if (!ok) {
    cout << -1 << endl;
    return 0;
  }
  vector<long long> za = zaatsu(a), zb = zaatsu(b);
  UnionFind uf(N + 1);
  for (int i = (int)0; i < (int)N + 1; i++)
    if (za[i] != zb[i]) uf.unite(za[i], zb[i]);
  long long result = 0;
  for (int i = (int)0; i < (int)N; i++) {
    if (i == uf.root(i)) {
      if (i == uf.root(a[N])) {
        if (za[N] == zb[N])
          result += uf.edge(i);
        else
          result += uf.edge(i) - 1;
      } else if (uf.edge(i) != 0)
        result += uf.edge(i) + 1;
    }
  }
  cout << result << endl;
  return 0;
}
