#include <bits/stdc++.h>
using namespace std;
const int INFINT = 1 << 30;
const long long INFLL = 1LL << 60;
const double EPS = 1e-10;
const int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const int MAX_N = 100000;
int N;
long long a[MAX_N + 1], b[MAX_N + 1];
bool used[MAX_N + 1];
static const int MAX_UF = MAX_N + 1;
template <class T>
class UnionFind {
 private:
  T par[MAX_UF];
  T rank[MAX_UF];

 public:
  T find(T x);
  void init(T n);
  void unite(T x, T y);
  bool same(T x, T y);
};
template <class T>
void UnionFind<T>::init(T n) {
  for (T i = 0; i < n; ++i) {
    par[i] = i;
    rank[i] = 0;
  }
}
template <class T>
T UnionFind<T>::find(T x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
template <class T>
void UnionFind<T>::unite(T x, T y) {
  x = find(x);
  y = find(y);
  if (x == y) {
    return;
  } else {
    if (rank[x] > rank[y]) {
      par[y] = x;
    } else {
      par[x] = y;
      if (rank[x] == rank[y]) ++rank[y];
    }
  }
}
template <class T>
bool UnionFind<T>::same(T x, T y) {
  return find(x) == find(y);
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cin >> N;
  for (int(i) = 0; (i) < (N); ++(i)) cin >> a[i];
  for (int(i) = 0; (i) < (N); ++(i)) cin >> b[i];
  for (int(i) = 0; (i) < (N); ++(i)) {
    a[N] ^= a[i];
    b[N] ^= b[i];
  }
  for (int(i) = 0; (i) < (N + 1); ++(i)) {
    bool judge = false;
    for (int(j) = 0; (j) < (N + 1); ++(j)) {
      if (!used[j] && a[i] == b[j]) {
        used[j] = true;
        judge = true;
        break;
      }
    }
    if (!judge) {
      cout << -1 << endl;
      return 0;
    }
  }
  vector<pair<int, int> > cmp;
  map<int, int> mp;
  set<int> st;
  for (int(i) = 0; (i) < (N); ++(i)) {
    if (a[i] != b[i]) {
      cmp.push_back(pair<int, int>(a[i], b[i]));
      st.insert(a[i]);
      st.insert(b[i]);
    }
  }
  cmp.push_back(pair<int, int>(a[N], b[N]));
  auto ite = st.begin();
  for (int i = 0; i < st.size(); ++i) {
    mp[*ite] = i;
    ++ite;
  }
  for (int i = 0; i < cmp.size(); ++i) {
    cmp[i].first = mp[cmp[i].first];
    cmp[i].second = mp[cmp[i].second];
  }
  UnionFind<int> uf;
  uf.init(st.size());
  for (int i = 0; i < cmp.size(); ++i) {
    if (cmp[i].first != cmp[i].second) uf.unite(cmp[i].first, cmp[i].second);
  }
  set<int> group;
  for (int i = 0; i < st.size(); ++i) {
    group.insert(uf.find(i));
  }
  cout << (int)((cmp).size()) + -1 + (int)((group).size()) - 1 << endl;
  return 0;
}
