#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int main() {
  ll N;
  array<ll, 100000> A, B;
  cin >> N;
  for (ll i = 0; i < N; ++i) {
    cin >> A[i];
  }
  for (ll i = 0; i < N; ++i) {
    cin >> B[i];
  }
  ll x = 0;
  for (ll i = 0; i < N; ++i) {
    x ^= A[i];
  }
  A[N] = x;
  ll diff = 0;
  for (ll i = 0; i < N; ++i) {
    if (A[i] != B[i]) {
      diff += 1;
    }
  }
  ll xi = -1;
  for (ll i = 0; i < N; ++i) {
    if (x == B[i]) {
      xi = i;
    }
  }
  if (diff == 0) {
    cout << 0 << endl;
    return 0;
  }
  ll pairA[100000];
  ll pairB[100000];
  array<array<ll, 2>, 100000> ia, ib;
  for (ll i = 0; i < N; ++i) {
    ia[i][0] = i;
    ia[i][1] = A[i];
    ib[i][0] = i;
    ib[i][1] = B[i];
  }
  stable_sort(ia.begin(), ia.begin() + N,
              [](const array<ll, 2>& a, const array<ll, 2>& b) -> bool {
                return a[1] < b[1];
              });
  stable_sort(ib.begin(), ib.begin() + N,
              [](const array<ll, 2>& a, const array<ll, 2>& b) -> bool {
                return a[1] < b[1];
              });
  bool hold = true;
  bool usex = false;
  ll i0 = 0;
  for (ll i = 0; i < N; ++i) {
    if (ia[i0][1] != ib[i][1]) {
      if (x == ib[i][1] && !usex) {
        usex = true;
        pairB[ib[i][0]] = N;
      } else {
        hold = false;
        break;
      }
    } else {
      pairA[ia[i0][0]] = ib[i][0];
      pairB[ib[i][0]] = ia[i0][0];
    }
    ++i0;
  }
  if (!hold) {
    cout << -1 << endl;
    return 0;
  }
  if (xi == -1) {
    cout << diff + 1 << endl;
    return 0;
  }
  ll start = xi;
  ll path = 0;
  ll cur = start;
  while (1) {
    cur = pairA[cur];
    path += 1;
    if (cur == start || path >= N) break;
  }
  if (path < N) {
    diff += 1;
  }
  cout << diff << endl;
  return 0;
}
