#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1e5 + 123;
int n, a[N], b[N], last, ind[N], rev[N];
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  cin >> n;
  multiset<int> A, B;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    last ^= a[i];
    A.insert(a[i]);
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    B.insert(b[i]);
  }
  vector<int> del;
  for (int i : A) {
    if (B.find(i) != B.end()) {
      B.erase(B.find(i));
      del.push_back(i);
    }
  }
  for (int i : del) {
    A.erase(A.find(i));
  }
  if (A.size() > 1) {
    cout << -1;
    return 0;
  }
  if (B.size() > 0 && *B.begin() != last) {
    cout << -1;
    return 0;
  }
  multiset<pair<int, int> > st;
  for (int i = 1; i <= n; i++) {
    ind[i] = i;
  }
  shuffle(ind + 1, ind + n + 1, rng);
  for (int i = 1; i <= n; i++) {
    rev[ind[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    st.insert({b[i], ind[i]});
  }
  int ans = 0;
  while (!st.empty()) {
    auto it = st.lower_bound({last, 0});
    if (it == st.end()) {
      break;
    }
    int pos = rev[(*it).second];
    if (a[pos] == b[pos]) {
      st.erase(it);
      continue;
    }
    ans++;
    swap(last, a[pos]);
    if (a[pos] == b[pos]) {
      st.erase(it);
    }
  }
  cout << ans;
}
