#include <bits/stdc++.h>
using namespace std;
void print(vector<long long> a) {
  for (long long i = 0; i < (long long)((long long)((a).size())); i++)
    cout << a[i] << " \n"[i == (long long)((a).size()) - 1];
}
long long numSteps(vector<long long> a, vector<long long> b) {
  vector<long long> asort = a, bsort = b;
  sort(asort.begin(), asort.end()), sort(bsort.begin(), bsort.end());
  if (asort != bsort) return -1;
  long long step = 0;
  map<long long, vector<long long>> should;
  for (long long i = 0; i < (long long)((long long)((a).size()) - 1); i++) {
    if (a[i] != b[i]) {
      should[b[i]].push_back(i);
    }
  }
  while ((long long)((should).size())) {
    step++;
    if (should.count(a.back())) {
      long long place = should[a.back()].back();
      should[a.back()].pop_back();
      if (!(long long)((should[a.back()]).size())) should.erase(a.back());
      swap(a[place], a.back());
    } else {
      long long place = should.begin()->second[0];
      swap(a[place], a.back());
    }
  }
  return step;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  vector<long long> as, bs;
  long long last = 0;
  for (long long i = 0; i < (long long)(n); i++) {
    long long a;
    cin >> a;
    as.push_back(a);
    last ^= a;
  }
  as.push_back(last);
  last = 0;
  for (long long i = 0; i < (long long)(n); i++) {
    long long b;
    cin >> b;
    bs.push_back(b);
    last ^= b;
  }
  bs.push_back(last);
  cout << numSteps(as, bs) << endl;
}
