#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> par;
int root(int x) {
  if (x == par[x]) return x;
  return par[x] = root(par[x]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  vector<int> a(n + 1), b(n + 1);
  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];
  vector<int> pa = a;
  sort(pa.begin(), pa.end());
  vector<int> pb = b;
  sort(pb.begin(), pb.end());
  if (pa != pb)
    cout << -1 << endl;
  else {
    vector<int> p = pa;
    p.erase(unique(p.begin(), p.end()), p.end());
    int cnt = 0;
    for (int i = 0; i <= n; i++) {
      a[i] = lower_bound(p.begin(), p.end(), a[i]) - p.begin();
      b[i] = lower_bound(p.begin(), p.end(), b[i]) - p.begin();
      if (i != n) cnt += (a[i] != b[i] ? 1 : 0);
    }
    par = vector<int>(p.size());
    for (int i = 0; i < p.size(); i++) par[i] = i;
    for (int i = 0; i <= n; i++) par[root(a[i])] = root(b[i]);
    set<int> s;
    for (int i = 0; i <= n; i++) {
      if (a[i] != b[i]) s.insert(root(i));
    }
    cout << cnt + (s.size() ? s.size() - 1 : 0) << endl;
  }
  return 0;
}
