#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pi = pair<ll, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct dsu {
  vi p, r, sm;
  void init(int n) {
    p.resize(n + 1);
    r.assign(n + 1, 1);
    sm.assign(n + 1, 0);
    for (int i = 1; i <= n; i++) p[i] = i;
  }
  int par(int i) {
    if (i == p[i]) return i;
    return p[i] = par(p[i]);
  }
  void unite(int i, int j) {
    i = par(i), j = par(j);
    if (i == j) return;
    if (r[i] < r[j]) swap(i, j);
    p[j] = i;
    r[i] += r[j];
  }
  void inc(int x) {
    x = par(x);
    sm[x]++;
  }
};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, x = 0, y = 0;
  vi a, b;
  map<int, int> cnt;
  cin >> n;
  a.resize(n);
  for (auto &i : a) cin >> i, cnt[i]++, x ^= i;
  a.push_back(x), cnt[x]++;
  b.resize(n);
  for (auto &i : b) cin >> i, y ^= i, cnt[i]--;
  b.push_back(y), cnt[y]--;
  for (auto i : cnt)
    if (i.second) return cout << -1, 0;
  map<int, int> id;
  for (auto &i : a) {
    if (id.count(i))
      i = id[i];
    else
      i = (id[i] = id.size());
  }
  for (auto &i : b) {
    i = id[i];
  }
  int ans = 0;
  dsu d;
  d.init(n);
  for (int i = 0; i < n + 1; i++) {
    if (a[i] != b[i]) {
      d.unite(a[i], b[i]);
      d.inc(a[i]);
    }
  }
  for (int i = 0; i < n + 1; i++) {
    if (i != d.par(i)) continue;
    if (d.r[i] == 1) continue;
    ans += d.sm[i] + 1;
    ans -= i == d.par(a[n]);
    ans -= a[n] != b[n];
  }
  cout << ans;
}
