#include <bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;
struct UnionFind {
  vector<int> par;
  UnionFind(int n) { par = vector<int>(n, -1); }
  int root(int x) { return par[x] < 0 ? x : (par[x] = root(par[x])); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (par[x] < par[y]) swap(x, y);
      par[y] += par[x];
      par[x] = y;
    }
  }
  bool is_par(int x) { return root(x) == x; }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -par[root(x)]; }
};
struct Problem {
  int n;
  vector<int> a, b;
  Problem(LL n) : n(n), a(n), b(n){};
  void solve() {
    int xa = 0, xb = 0;
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      xa ^= a[i];
    }
    for (int i = 0; i < n; ++i) {
      cin >> b[i];
      xb ^= b[i];
    }
    vector<int> a2(a), b2(b);
    a2.push_back(xa);
    b2.push_back(xb);
    sort(a2.begin(), a2.end());
    sort(b2.begin(), b2.end());
    if (a2 != b2) {
      cout << -1 << endl;
      return;
    }
    a2.erase(unique(a2.begin(), a2.end()), a2.end());
    b2.erase(unique(b2.begin(), b2.end()), b2.end());
    a.push_back(xa);
    b.push_back(xb);
    for (int i = 0; i < a.size(); ++i) {
      a[i] = lower_bound(a2.begin(), a2.end(), a[i]) - a2.begin();
      b[i] = lower_bound(b2.begin(), b2.end(), b[i]) - b2.begin();
    }
    UnionFind uf((int)a2.size() + a.size());
    ;
    int cnt = 0, ans = 0;
    vector<int> used((int)a2.size(), 0);
    ;
    for (int i = 0; i < a.size(); ++i) {
      if (a[i] == b[i])
        cnt++;
      else {
        used[a[i]] = 1;
        used[b[i]] = 1;
        uf.unite(a[i] + a.size(), i);
        uf.unite(b[i] + b.size(), i);
        ;
      }
    };
    for (int i = 0; i < (int)a2.size() + a.size(); ++i) {
      if (uf.is_par(i)) {
        ans += uf.size(i) - 1;
        ;
      }
    };
    cout << ans - accumulate(used.begin(), used.end(), 0) << "\n";
  }
};
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  long long n = 0;
  cin >> n;
  Problem p(n);
  p.solve();
  return 0;
}
