#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> a, b;
map<int, vector<int> > pos;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  a.resize(n);
  b.resize(n);
  for (auto &i : a) cin >> i;
  for (auto &i : b) cin >> i;
  set<int> g(a.begin(), a.end());
  set<int> h(b.begin(), b.end());
  int x = 0;
  for (auto &i : b) x ^= i;
  h.insert(x);
  x = 0;
  for (auto &i : a) x ^= i;
  g.insert(x);
  if (g != h) return cout << -1 << endl, 0;
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) pos[b[i]].push_back(i);
  }
  int ans = 0;
  while (pos.size()) {
    int idx;
    if (pos.find(x) != pos.end()) {
      idx = pos[x].back();
      pos[x].pop_back();
      if (pos[x].size() == 0) pos.erase(x);
    } else {
      idx = (*pos.begin()).first;
    }
    x = a[idx];
    ans++;
  }
  cout << ans << endl;
}
