#include <bits/stdc++.h>
using namespace std;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const long long INF = 1LL << 29;
const long long mod = 1e9 + 7;
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  int n = v.size();
  for (int(i) = 0; (i) < (long long)(n); ++(i))
    os << v[i] << (i == n - 1 ? "" : " ");
  return os;
}
struct UF {
  vector<int> par, rank, sz;
  void init(int n) {
    par = rank = vector<int>(n, 0);
    sz = vector<int>(n, 1);
    for (int i = 0; i < n; i++) {
      par[i] = i;
    }
  }
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }
  int size(int x) { return sz[find(x)]; }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y]) {
      par[x] = y;
      sz[y] += sz[x];
    } else {
      par[y] = par[x];
      if (rank[x] == rank[y]) rank[y]++;
      sz[x] += sz[y];
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};
map<long long, long long> mk_comp(
    const vector<pair<long long, long long> > &x) {
  map<long long, long long> dc;
  vector<long long> y;
  y.reserve(x.size() * 2);
  for (auto p : x) {
    y.push_back(p.first);
    y.push_back(p.second);
  }
  sort((y).begin(), (y).end());
  (y).erase(unique((y).begin(), (y).end()), (y).end());
  for (int(i) = 0; (i) < (long long)(y.size()); ++(i)) dc[y[i]] = i;
  return dc;
}
int main() {
  int n;
  cin >> n;
  vector<long long> a[2];
  for (int(i) = 0; (i) < (long long)(2); ++(i))
    a[i] = vector<long long>(n + 1, 0);
  for (int(i) = 0; (i) < (long long)(2); ++(i))
    for (int(j) = 0; (j) < (long long)(n); ++(j)) cin >> a[i][j];
  for (int(i) = 0; (i) < (long long)(2); ++(i))
    for (int(j) = 0; (j) < (long long)(n); ++(j)) a[i][n] ^= a[i][j];
  vector<pair<long long, long long> > v;
  v.reserve(n + 1);
  for (int(i) = 0; (i) < (long long)(n + 1); ++(i))
    if (i == n || a[0][i] != a[1][i])
      v.push_back(pair<long long, long long>(a[1][i], a[0][i]));
  auto dc = mk_comp(v);
  for (auto &p : v) p = pair<long long, long long>(dc[p.first], dc[p.second]);
  long long res = v.size();
  if (a[0][n] == a[1][n])
    res--;
  else {
    cout << res - 1 << endl;
    return 0;
  }
  for (int(i) = 0; (i) < (long long)(2); ++(i))
    sort((a[i]).begin(), (a[i]).end());
  if (a[0] != a[1]) {
    cout << -1 << endl;
    return 0;
  }
  UF uf;
  uf.init(dc.size());
  for (auto x : v) uf.unite(x.first, x.second);
  for (int(i) = 0; (i) < (long long)(dc.size()); ++(i)) {
    if (uf.find(i) == i) res++;
  }
  if (!v.empty()) res--;
  (res = max(res, 0LL));
  cout << res << endl;
  return 0;
}
