#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) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
inline bool chmin(T1 &a, const T2 &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "{";
  for (long long i = 0; i < (long long)vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
  }
  os << "}";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &st) {
  os << "{";
  for (auto itr = st.begin(); itr != st.end(); itr++) {
    os << *itr;
    itr++;
    if (itr != st.end()) {
      os << ", ";
    }
    itr--;
  }
  os << "}";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &st) {
  os << "{";
  for (auto itr = st.begin(); itr != st.end(); itr++) {
    os << *itr;
    itr++;
    if (itr != st.end()) {
      os << ", ";
    }
    itr--;
  }
  os << "}";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &mp) {
  os << "{";
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    os << *itr;
    itr++;
    if (itr != mp.end()) {
      os << ", ";
    }
    itr--;
  }
  os << "}";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const multimap<T, U> &mp) {
  os << "{";
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    os << *itr;
    itr++;
    if (itr != mp.end()) {
      os << ", ";
    }
    itr--;
  }
  os << "}";
  return os;
}
struct SetUp {
  SetUp() {
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(20);
  };
} setUp;
struct UnionFindTree {
  const long long n;
  long long cc = 0;
  std::vector<long long> par, size;
  UnionFindTree(long long n) : n(n), par(n), size(n) {
    for (long long i = 0; i < n; i++) {
      par[i] = i;
      size[i] = 1;
    }
  }
  long long getRoot(long long i) {
    if (par[i] == i) {
      return i;
    } else {
      return par[i] = getRoot(par[i]);
    }
  }
  void unite(long long i, long long j) {
    if ((i = getRoot(i)) == (j = getRoot(j))) {
      return;
    }
    if (size[i] < size[j]) {
      std::swap(i, j);
    }
    par[j] = i;
    if (size[i] == 1 && size[j] == 1) {
      cc++;
    }
    size[i] += size[j];
  }
  bool isSame(long long i, long long j) { return getRoot(i) == getRoot(j); }
  long long getSize(long long i) { return size[getRoot(i)]; }
};
long long N;
signed main() {
  cin >> N;
  vint a(N + 1), b(N + 1);
  for (long long i = 0; i < (long long)N; i++) {
    cin >> a[i];
    a[N] ^= a[i];
  }
  for (long long i = 0; i < (long long)N; i++) {
    cin >> b[i];
    b[N] ^= b[i];
  }
  vint ac(a), bc(b);
  sort((ac).begin(), (ac).end()), sort((bc).begin(), (bc).end());
  for (long long i = 0; i < (long long)N + 1; i++) {
    if (ac[i] != bc[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  ac.erase(unique(ac.begin(), ac.end()), ac.end());
  ;
  map<long long, long long> zat;
  for (long long i = 0; i < (long long)ac.size(); i++) {
    zat[ac[i]] = i;
  }
  UnionFindTree uft(ac.size());
  long long ans = 0, op = 0;
  for (long long i = 0; i < (long long)N; i++) {
    if (a[i] == b[i]) {
      continue;
    }
    ans++;
    if (b[i] == a[N]) {
      op = 1;
    }
    uft.unite(zat[a[i]], zat[b[i]]);
  }
  uft.unite(zat[a[N]], zat[b[N]]);
  ans += uft.cc - op;
  cout << ans << endl;
}
