#include <bits/stdc++.h>
using namespace std;
int solve(int n, vector<int> const& a, vector<int> const& b) {
  int sum_a = accumulate(begin(a), end(a), 0, bit_xor<int>());
  map<int, map<int, int> > g;
  auto link = [&](int x, int y) {
    if (x != y) {
      g[x][y] += 1;
    }
  };
  auto unlink = [&](int x, int y) {
    g[x][y] -= 1;
    if (not g[x][y]) {
      g[x].erase(y);
      if (g[x].empty()) {
        g.erase(x);
      }
    }
  };
  for (int i = 0; (i) < int(n); ++(i)) {
    link(b[i], a[i]);
  }
  int result = 0;
  for (int i = sum_a; not g.empty(); ++result) {
    if (result > 2 * n) return -1;
    if (not g.count(i)) {
      int j = g.begin()->first;
      int k = g.begin()->second.begin()->first;
      unlink(j, k);
      link(j, i);
      i = k;
    } else {
      int j = g[i].begin()->first;
      unlink(i, j);
      i = j;
    }
  }
  return result;
}
int main() {
  int n;
  scanf("%d", &n);
  vector<int> a(n);
  for (int i = 0; (i) < int(n); ++(i)) scanf("%d", &a[i]);
  vector<int> b(n);
  for (int i = 0; (i) < int(n); ++(i)) scanf("%d", &b[i]);
  int result = solve(n, a, b);
  printf("%d\n", result);
  return 0;
}
