#include <bits/stdc++.h>
using namespace std;
vector<int> a, b;
int getXor(vector<int> c) {
  int ret = 0;
  for (int x : c) ret ^= x;
  return ret;
}
vector<int> A, B;
unordered_map<int, vector<int> > inds;
int main() {
  ios::sync_with_stdio(0);
  int N;
  cin >> N;
  a.resize(N);
  b.resize(N);
  for (int i = (0); i < int(N); ++i) cin >> a[i];
  for (int i = (0); i < int(N); ++i) cin >> b[i];
  a.push_back(getXor(a));
  b.push_back(getXor(b));
  A = a;
  B = b;
  sort((A).begin(), (A).end());
  sort((B).begin(), (B).end());
  if (A != B) {
    cout << "-1" << endl;
    return 0;
  }
  for (int i = (0); i < int((int)(b).size() - 1); ++i) {
    if (a[i] != b[i]) {
      inds[b[i]].push_back(i);
    }
  }
  int ans = 0;
  while (!inds.empty()) {
    ++ans;
    if (!inds.count(a.back())) {
      int ind = inds.begin()->second.back();
      swap(a[(int)(a).size() - 1], a[ind]);
    } else {
      int ind = inds[a.back()].back();
      swap(a[(int)(a).size() - 1], a[ind]);
      inds[a[ind]].pop_back();
      if (inds[a[ind]].empty()) inds.erase(a[ind]);
    }
  }
  cout << ans << endl;
}
