#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() {
  set<pair<int, int> > st;
  map<int, bool> visited;
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i])
      st.insert(pair<int, int>(a[i], i));
    else
      visited[i] = true;
  }
  int res = st.size();
  for (int i = 0; i < n; i++) {
    if (visited[i]) continue;
    res++;
    int p = i;
    while (visited[p] == false) {
      visited[p] = true;
      set<pair<int, int> >::iterator it =
          st.lower_bound(pair<int, int>(b[p], 0));
      if (it == st.end()) break;
      if (it->first != b[p]) break;
      int next = it->second;
      st.erase(it);
      a[p] = b[p];
      p = next;
    }
  }
  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() + 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;
}
