#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<ll> p;
ll fd(ll i) { return p[i] == i ? i : fd(p[i]); }
void mg(ll i, ll j) { p[fd(i)] = fd(j); }
int main() {
  ios::sync_with_stdio(false);
  cin.exceptions(ios::failbit | ios::badbit);
  ll N;
  cin >> N;
  ;
  vector<ll> a(N), b(N), g;
  unordered_map<ll, vector<ll>> ma, mb;
  ll sa = 0, sb = 0;
  for (ll i = 0; i < N; i++) {
    cin >> a[i];
    sa ^= a[i];
  }
  for (ll i = 0; i < N; i++) {
    cin >> b[i];
    sb ^= b[i];
  }
  a.push_back(sa);
  b.push_back(sb);
  auto ca = a;
  auto cb = b;
  a.clear();
  b.clear();
  for (ll i = 0; i < ca.size(); i++) {
    if (ca[i] == cb[i]) {
      continue;
    }
    a.push_back(ca[i]);
    b.push_back(cb[i]);
  }
  for (ll i = 0; i < a.size(); i++) {
    ma[a[i]].push_back(i);
  }
  for (ll i = 0; i < a.size(); i++) {
    mb[b[i]].push_back(i);
  }
  for (ll i = 0; i < a.size(); i++) {
    if (ma[a[i]].size() != mb[a[i]].size()) {
      ;
      ;
      ;
      cout << -1 << endl;
      return 0;
    }
  }
  for (ll i = 0; i < a.size(); i++) {
    if (ma[b[i]].size() != mb[b[i]].size()) {
      cout << -1 << endl;
      return 0;
    }
  }
  g.resize(a.size());
  p.resize(a.size());
  for (ll i = 0; i < a.size(); i++) {
    g[i] = mb[a[i]].back();
    mb[a[i]].pop_back();
  }
  for (ll i = 0; i < g.size(); i++) {
    ;
    ;
  }
  for (ll i = 0; i < g.size(); i++) {
    ll x = g[i];
    do {
      mg(x, g[x]);
      x = g[x];
    } while (x != g[i]);
  }
  for (ll i = 0; i < a.size(); i++) {
    for (ll j = 0; j < ma[a[i]].size(); j++) {
      mg(i, ma[a[i]][j]);
    }
    ma[a[i]].clear();
  }
  unordered_set<ll> s;
  for (ll i = 0; i < a.size(); i++) {
    s.insert(fd(i));
  };
  ;
  ll ans;
  if (sa == sb) {
    ans = s.size() + p.size();
  } else {
    ans = s.size() + p.size() - 2;
  }
  cout << ans << endl;
}
