#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9 + 123;
const long long LINF = (long long)1e18 + 123;
const long double EPS = (long double)1e-12;
const long long MOD = (long long)1e9 + 7;
long long power(long long x, long long y, long long mod = MOD) {
  if (y == 0) {
    return 1;
  }
  if (y & 1) {
    return power(x, y - 1, mod) * x % mod;
  } else {
    long long tmp = power(x, y / 2, mod);
    return tmp * tmp % mod;
  }
}
void add(long long &x, long long y, long long mod = MOD) {
  x += y;
  if (x >= mod) x -= mod;
  if (x < 0) x += mod;
}
long long sum(long long x, long long y, long long mod = MOD) {
  add(x, y, mod);
  return x;
}
long long mult(long long x, long long y, long long mod = MOD) {
  return (x * y) % mod;
}
long long div(long long x, long long y, long long mod) {
  return x * power(y, mod - 2, mod);
}
template <typename A, typename B>
bool mini(A &x, const B &y) {
  if (y < x) {
    x = y;
    return true;
  }
  return false;
}
template <typename A, typename B>
bool maxi(A &x, const B &y) {
  if (y > x) {
    x = y;
    return true;
  }
  return false;
}
template <typename T>
void read(T first, T last) {
  while (first != last) {
    cin >> *(first++);
  }
}
template <typename T>
void print(T first, T last) {
  while (first != last) {
    cout << *(first++) << " ";
  }
  cout << "\n";
}
void run();
int main() {
  if (strlen("")) {
    freopen(
        ""
        ".in",
        "r", stdin);
    freopen(
        ""
        ".out",
        "w", stdout);
  }
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(12);
  run();
  return 0;
}
const int N = (int)1e5 + 123;
int n;
int a[N], b[N], p[N];
pair<int, int> as[N], bs[N];
int get(int v) {
  if (p[v] == v) return v;
  return (p[v] = get(p[v]));
}
bool unite(int u, int v) {
  u = get(u);
  v = get(v);
  if (u == v) return 0;
  p[u] = v;
  return 1;
}
void run() {
  cin >> n;
  read(a, a + n);
  read(b, b + n);
  for (auto i = 0; i < n; i++) {
    a[n] ^= a[i];
    b[n] ^= b[i];
  }
  bool ok = 1;
  for (auto i = 0; i < n; i++) ok &= a[i] == b[i];
  if (ok) {
    cout << "0\n";
    return;
  }
  n++;
  for (auto i = 0; i < n; i++) p[i] = i;
  for (auto i = 0; i < n; i++) {
    as[i] = {a[i], i};
    bs[i] = {b[i], i};
  }
  sort(as, as + n);
  sort(bs, bs + n);
  for (auto i = 0; i < n; i++) {
    if (as[i].first != bs[i].first) {
      cout << "-1\n";
      return;
    }
  }
  int cnt = 0;
  int comps = 0;
  set<int> q;
  for (auto i = 0; i < n - 1; i++) {
    if (a[i] != b[i]) {
      cnt++;
      q.insert(a[i]);
      q.insert(b[i]);
      comps -= unite(a[i], b[i]);
    }
  }
  comps += (int)(q).size();
  cout << cnt + comps - 1 << "\n";
}
