#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int a[n + 1], b[n + 1];
  a[n] = 0, b[n] = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[n] ^= a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    b[n] ^= b[i];
  }
  if (multiset<int>(a, a + n + 1) != multiset<int>(b, b + n + 1)) {
    cout << -1 << endl;
    return 0;
  }
  map<int, vector<int>> unmatch;
  map<int, set<int>> un2;
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      un2[a[i]].insert(i);
      unmatch[b[i]].push_back(i);
    }
  }
  int ans = 0;
  while (unmatch.size()) {
    ans++;
    if (unmatch[a[n]].empty()) {
      auto it = un2.begin();
      if (it->first == a[n]) it++;
      swap(a[n], a[*(it->second.begin())]);
      it->second.erase(it->second.begin());
    } else {
      int t = a[n];
      swap(a[n], a[unmatch[a[n]].back()]);
      un2[a[n]].erase(unmatch[t].back());
      if (un2[a[n]].empty()) un2.erase(a[n]);
      unmatch[t].pop_back();
      if (unmatch[t].empty()) unmatch.erase(t);
    }
    if (unmatch[a[n]].empty()) unmatch.erase(a[n]);
  }
  cout << ans << endl;
}
