#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
struct DSU {
  vector<int> p, sz;
  DSU(int N) {
    p.resize(N);
    sz.resize(N, 1);
    for (int i = 0; i < N; i++) p[i] = i;
  }
  int par(int x) {
    if (x == p[x]) return x;
    return p[x] = par(p[x]);
  }
  bool mrg(int x, int y) {
    x = par(x), y = par(y);
    if (x == y) return false;
    if (sz[x] < sz[y]) {
      swap(x, y);
    }
    p[y] = p[x];
    sz[x] += sz[y];
    return true;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<int> a(N), b(N), cpy;
  int Exor = 0;
  vector<int> all;
  for (int i = 0; i < N; i++) cin >> a[i], Exor ^= a[i], all.push_back(a[i]);
  all.push_back(Exor);
  for (int i = 0; i < N; i++) cin >> b[i];
  {
    cpy = b;
    sort(all.begin(), all.end());
    sort(cpy.begin(), cpy.end());
    bool can = true;
    for (int i = 0, j = 0; i < N && can; i++) {
      while (j < N + 1 && all[j] < cpy[i]) j++;
      if (j >= N + 1) can = false;
      if (j < N + 1 && all[j] > cpy[i]) can = false;
      j++;
    }
    if (!can) {
      cout << "-1\n";
      return 0;
    }
  }
  all.clear();
  all.push_back(Exor);
  for (int i = 0; i < N; i++) {
    if (a[i] != b[i]) all.push_back(a[i]), all.push_back(b[i]);
  }
  sort(all.begin(), all.end());
  auto it = unique(all.begin(), all.end());
  all.erase(it, all.end());
  DSU dsu(all.size());
  int com = all.size();
  cerr << com << '\n';
  int diff = 0;
  for (int i = 0; i < N; i++) {
    if (a[i] == b[i]) continue;
    diff++;
    auto x = lower_bound(all.begin(), all.end(), a[i]) - all.begin();
    auto y = lower_bound(all.begin(), all.end(), b[i]) - all.begin();
    if (x > (int)all.size() || y > (int)all.size()) continue;
    com -= dsu.mrg(x, y);
  }
  cerr << com << '\n';
  if (com == 1) {
    cout << diff << '\n';
  } else {
    cout << diff + 1 << '\n';
  }
  return 0;
}
