#include <bits/stdc++.h>
using namespace std;
vector<int> uf;
int find(int n) {
  if (uf[n] == n)
    return n;
  else
    return uf[n] = find(uf[n]);
}
void uni(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b) return;
  uf[a] = b;
  return;
}
int main() {
  int n;
  cin >> n;
  int x = 0;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    x ^= a[i];
  }
  int count = 0;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (a[i] != b[i]) count++;
  }
  vector<int> c(count), d(count);
  map<int, pair<int, int>> e, f;
  map<int, pair<int, int>>::iterator itr, itr2;
  uf = vector<int>(count);
  for (int i = 0; i < count; i++) {
    uf[i] = i;
  }
  count = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      c[count] = a[i];
      d[count] = b[i];
      itr = e.find(a[i]);
      if (itr == e.end())
        e[a[i]] = {1, count};
      else
        itr->second.first++;
      itr = f.find(b[i]);
      if (itr == f.end())
        f[b[i]] = {1, count};
      else {
        itr->second.first++;
        uni(count, itr->second.second);
      }
      count++;
    }
  }
  bool xflag = false;
  for (itr = f.begin(); itr != f.end(); itr++) {
    if (itr->first == x) xflag = true;
    itr2 = e.find(itr->first);
    if (itr2 == e.end()) {
      if (itr->first != x || itr->second.first > 1) {
        cout << -1 << endl;
        return 0;
      }
    } else {
      if (itr->second.first + 1 < itr2->second.first ||
          (itr->first != x && itr->second.first + 1 == itr2->second.first)) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  for (int i = 0; i < count; i++) {
    itr = f.find(c[i]);
    if (itr != f.end()) {
      uni(i, itr->second.second);
    }
  }
  vector<bool> isroot(n, false);
  for (int i = 0; i < count; i++) {
    isroot[find(i)] = true;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (isroot[i]) ans++;
  }
  if (xflag) {
    cout << count + ans - 1 << endl;
  } else {
    cout << count + ans << endl;
  }
  return 0;
}
