#include <bits/stdc++.h>
using namespace std;
int n;
int bit = 0;
int a[100001];
int b[100000];
map<int, int> ma, mb;
int calc(int key) {
  set<pair<int, int> > st;
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      st.insert(pair<int, int>(b[i], i));
      assert(a[i] != key);
      assert(b[i] != key);
    }
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) continue;
    res++;
    while (1) {
      set<pair<int, int> >::iterator it =
          st.lower_bound(pair<int, int>(key, 0));
      if (it == st.end()) break;
      if (it->first != key) break;
      if (a[it->second] == key) {
        st.erase(it);
        continue;
      }
      swap(key, a[it->second]);
      res++;
      st.erase(it);
    }
  }
  return res;
}
int solve() {
  for (auto p : mb) {
    int key = p.first;
    int value = p.second;
    if (ma[key] < value) {
      return -1;
    }
  }
  set<pair<int, int> > st;
  for (int i = 0; i < n; i++) st.insert(pair<int, int>(b[i], i));
  int key = bit;
  int count = 0;
  while (1) {
    set<pair<int, int> >::iterator it = st.lower_bound(pair<int, int>(key, 0));
    if (it == st.end()) break;
    if (it->first != key) break;
    if (a[it->second] == key) {
      st.erase(it);
      continue;
    }
    count++;
    swap(a[it->second], key);
    st.erase(it);
  }
  return calc(key) + count;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ma[a[i]]++;
    bit ^= a[i];
  }
  ma[bit]++;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    mb[b[i]]++;
  }
  int ans = solve();
  cout << ans << endl;
  return 0;
}
