#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[100002], b[100002], as[100002], bs[100002], s = 0;
  set<pair<int, int> > st;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    as[i] = a[i];
    s ^= a[i];
    st.insert(pair<int, int>(a[i], i));
  }
  as[n] = a[n] = s;
  sort(as, as + n + 1);
  s = 0;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    bs[i] = b[i];
    s ^= b[i];
  }
  bs[n] = b[n] = s;
  sort(bs, bs + n + 1);
  for (int i = 0; i <= n; i++) {
    if (as[i] != bs[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  int ct = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      st.erase(pair<int, int>(a[i], i));
      continue;
    }
    if (a[n] == b[i]) {
      st.erase(pair<int, int>(a[i], i));
      swap(a[i], a[n]);
      ct++;
      continue;
    }
    pair<int, int> p = *st.lower_bound(pair<int, int>(b[i], -1));
    int j = p.second;
    st.erase(pair<int, int>(a[j], j));
    st.insert(pair<int, int>(a[n], j));
    a[j] = a[n];
    a[n] = a[i];
    st.erase(pair<int, int>(a[i], i));
    ct += 2;
  }
  cout << ct << endl;
  return 0;
}
