#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using ld = long double;
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "[";
  for (ll i = ll(0); i < ll((ll)v.size()); i++) {
    if (i) os << ",";
    os << v[i];
  }
  os << "]";
  return os;
}
ll read() {
  ll i;
  scanf("%lld", &i);
  return i;
}
void printSpace() { printf(" "); }
void printEoln() { printf("\n"); }
void print(ll x, ll suc = 1) {
  printf("%lld", x);
  if (suc == 1) printEoln();
  if (suc == 2) printSpace();
}
string readString() {
  static char buf[3341919];
  scanf("%s", buf);
  return string(buf);
}
char* readCharArray() {
  static char buf[3341919];
  static ll bufUsed = 0;
  char* ret = buf + bufUsed;
  scanf("%s", ret);
  bufUsed += strlen(ret) + 1;
  return ret;
}
template <class T, class U>
void chmax(T& a, U b) {
  if (a < b) a = b;
}
template <class T, class U>
void chmin(T& a, U b) {
  if (a > b) a = b;
}
template <class T>
T Sq(const T& t) {
  return t * t;
}
const ll inf = LLONG_MAX / 3;
void Check(vi a, vi b) {
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  if (a != b) {
    cout << -1 << endl;
    exit(0);
  }
}
struct UnionFind {
  vi rank, par;
  void Init(ll n) {
    rank.assign(n, 0);
    par.assign(n, -1);
  }
  ll Find(ll i) { return par[i] == -1 ? i : (par[i] = Find(par[i])); }
  bool Same(ll a, ll b) { return Find(a) == Find(b); }
  bool Unite(ll a, ll b) {
    a = Find(a);
    b = Find(b);
    if (a == b) return false;
    if (rank[a] < rank[b]) swap(a, b);
    par[b] = a;
    if (rank[a] == rank[b]) rank[a]++;
    return true;
  }
} uf;
void Solve(ll n, vi a, vi b) {
  vi v = a;
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  uf.Init(v.size());
  auto idx = [&](ll x) {
    return lower_bound(v.begin(), v.end(), x) - v.begin();
  };
  ll dif = 0;
  for (ll i = ll(0); i < ll(n); i++)
    if (a[i] != b[i]) {
      uf.Unite(idx(a[i]), idx(b[i]));
      dif++;
    }
  ll con = 0;
  for (ll i = ll(0); i < ll(n); i++)
    if (a[i] != b[i])
      if (!uf.Same(idx(a[n - 1]), idx(a[i]))) {
        uf.Unite(idx(a[n - 1]), idx(a[i]));
        con += 2;
      }
  if (dif) con--;
  cout << dif + con << endl;
}
signed main() {
  ll n = read();
  vi a(n + 1, 0);
  for (ll i = ll(0); i < ll(n); i++) a[n] ^= a[i] = read();
  vi b(n + 1, 0);
  for (ll i = ll(0); i < ll(n); i++) b[n] ^= b[i] = read();
  Check(a, b);
  Solve(n + 1, a, b);
}
