#include <bits/stdc++.h>
using namespace std;
ostream& operator<<(ostream& os, vector<long long>& x) {
  for (long long i = 0; i < x.size(); i++) os << x[i] << ' ';
  return os;
}
ostream& operator<<(ostream& os, pair<long long, long long> x) {
  cout << x.first << ' ' << x.second << ' ';
  return os;
}
ostream& operator<<(ostream& os, vector<pair<long long, long long> >& x) {
  for (long long i = 0; i < x.size(); i++) os << x[i] << '\n';
  return os;
}
map<long long, stack<long long> > cnt;
long long n;
long long a[100005], b[100005];
long long outnum = 0;
long long total_diff = 0;
long long ans = 0;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> a[i], outnum ^= a[i];
  for (long long i = 1; i <= n; i++) cin >> b[i];
  for (long long i = 1; i <= n; i++) {
    if (a[i] != b[i]) {
      total_diff++;
      cnt[b[i]].push(i);
    }
  }
  bool pen = false;
  while (1) {
    if (total_diff == 0) break;
    ans++;
    if (cnt.find(outnum) == cnt.end()) {
      if (pen) {
        cout << "-1" << '\n';
        return 0;
      }
      pen = true;
      swap(outnum, a[cnt.begin()->second.top()]);
      continue;
    }
    long long ori = outnum;
    swap(outnum, a[cnt[outnum].top()]);
    total_diff--;
    cnt[ori].pop();
    if (cnt[ori].size() == 0) cnt.erase(cnt.find(ori));
    pen = false;
  }
  for (long long i = 1; i <= n; i++) {
    if (a[i] != b[i]) {
      assert(1 == 0);
      return 0;
    }
  }
  cout << ans << '\n';
}
