#include <bits/stdc++.h>
using namespace std;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
template <class T, class Y>
using P = pair<T, Y>;
template <class T, class Y>
ostream& operator<<(ostream& o, P<T, Y>& p) {
  return o << "(" << p.first << "," << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& o, V<T>& v) {
  for (T& t : v) o << t << ",";
  return o;
}
template <class T>
void uniq(V<T>& v) {
  sort(((v).begin()), ((v).end()));
  v.erase(unique(((v).begin()), ((v).end())), v.end());
}
template <class A, size_t N, class T>
void Fill(A (&a)[N], const T& v) {
  fill((T*)a, (T*)(a + 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 modInv(long long a, long long m) {
  __int128_t x, y;
  gcd(a, m, x, y);
  return (x % m + m) % m;
}
long long modPow(__int128_t a, __int128_t n, long long m) {
  __int128_t p = 1;
  for (; n; n >>= 1, a = a * a % m)
    if (n & 1) p = p * a % m;
  return p;
}
const int IINF = 1e9 + 6;
const long long LINF = 1e18;
const int MOD = 1e9 + 7;
const int N = 1e5 + 1;
int n, a[N], b[N], c[N], d[N], ans;
V<int> va, vb;
struct UnionFind {
  vector<int> p;
  int g;
  void init(int n) {
    p.assign(n, -1);
    g = n;
  }
  int find(int a) { return p[a] < 0 ? a : p[a] = find(p[a]); }
  bool merge(int a, int b) {
    a = find(a);
    b = find(b);
    if (a == b) return 0;
    g--;
    if (p[a] > p[b]) swap(a, b);
    p[a] += p[b];
    p[b] = a;
    return 1;
  }
} uf;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = (0), iformax = (n); i < iformax; i++) {
    cin >> a[i];
    a[n] ^= a[i];
  }
  for (int i = (0), iformax = (n); i < iformax; i++) {
    cin >> b[i];
    b[n] ^= b[i];
  }
  for (int i = (0), iformax = (n + 1); i < iformax; i++) va.push_back(a[i]);
  for (int i = (0), iformax = (n + 1); i < iformax; i++) vb.push_back(b[i]);
  sort(((va).begin()), ((va).end()));
  sort(((vb).begin()), ((vb).end()));
  for (int i = (0), iformax = (n + 1); i < iformax; i++)
    if (va[i] != vb[i]) {
      cout << -1 << endl;
      return 0;
    }
  uniq(va);
  for (int i = (0), iformax = (n + 1); i < iformax; i++)
    a[i] = lower_bound(((va).begin()), ((va).end()), a[i]) - va.begin();
  for (int i = (0), iformax = (n + 1); i < iformax; i++)
    b[i] = lower_bound(((va).begin()), ((va).end()), b[i]) - va.begin();
  uf.init((int)(va).size());
  for (int i = (0), iformax = (n + 1); i < iformax; i++)
    if (a[i] != b[i]) {
      uf.merge(a[i], b[i]);
      c[a[i]]++;
    }
  for (int i = (0), iformax = (n + 1); i < iformax; i++) d[uf.find(i)] += c[i];
  int ans = 0;
  for (int i = (0), iformax = (n + 1); i < iformax; i++)
    if (d[i]) ans += d[i] + 1;
  if (c[a[n]]) ans--;
  if (a[n] != b[n]) ans--;
  cout << ans << endl;
}
