#include <bits/stdc++.h>
using namespace std;
vector<int> par, sz;
int find(int cur) {
  if (par[cur] == -1) return cur;
  return par[cur] = find(par[cur]);
}
bool merge(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b) return false;
  if (sz[b] > sz[a]) swap(a, b);
  par[b] = a;
  sz[a] += sz[b];
  return true;
}
void init(int n) {
  par = vector<int>(n, -1);
  sz = vector<int>(n, 1);
}
signed main() {
  int n;
  cin >> n;
  vector<int> a(n + 1), b(n + 1);
  int x = 0;
  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];
  }
  init(n);
  auto aa = a;
  auto bb = b;
  sort(aa.begin(), aa.end());
  sort(bb.begin(), bb.end());
  if (aa != bb) {
    cout << -1 << endl;
    return 0;
  }
  map<int, vector<int> > m;
  for (int i = 0; i <= n; i++) {
    if (a[i] == b[i]) continue;
    m[a[i]].push_back(i);
    m[b[i]].push_back(i);
  }
  if (false) cerr << "REACHED" << endl;
  for (auto& v : m) {
    vector<int>& c = v.second;
    for (int i = 0; i < c.size() - 1; i++) {
      merge(c[i], c[i + 1]);
    }
  }
  if (false) cerr << "REACHED" << endl;
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    if (find(i) != i) continue;
    if (sz[i] == 1) continue;
    ans += sz[i] + 1;
    if (find(n) == find(i)) ans--;
  }
  cout << ans << endl;
}
