#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
long long int N, M, K, H, W, L, R;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  vector<int> v(N);
  vector<int> w(N);
  int box = 0;
  for (auto &i : v) {
    cin >> i;
    box ^= i;
  }
  v.push_back(box);
  box = 0;
  for (auto &i : w) {
    cin >> i;
    box ^= i;
  }
  w.push_back(box);
  auto vv = v;
  auto ww = w;
  sort(vv.begin(), vv.end());
  sort(ww.begin(), ww.end());
  if (vv != ww) {
    cout << -1 << endl;
    return 0;
  }
  vv.erase(unique(vv.begin(), vv.end()), vv.end());
  map<int, set<int>> ms;
  for (int i = 0; i < N; i++) {
    if (v[i] != w[i]) {
      ms[w[i]].insert(i);
    }
  }
  int ans = 0;
  while (!ms.empty()) {
    if (ms.find(v.back()) != ms.end()) {
      int nindex = *ms[v.back()].begin();
      ms[v.back()].erase(ms[v.back()].begin());
      if (ms[v.back()].empty()) ms.erase(v.back());
      swap(v[N], v[nindex]);
      ans++;
    } else {
      int nindex = *((*ms.begin()).second).begin();
      swap(v[N], v[nindex]);
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
