#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const int inf = 1000000009;
int i, j, k, m, n, l;
int ans;
int a[100005], b[100005];
map<int, vector<int> > M;
int main() {
  cin >> n;
  int x = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    x ^= a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (a[i] != b[i]) M[b[i]].push_back(a[i]);
  }
  for (int ans = 0; ans < 200001; ans++) {
    if (M.empty()) {
      cout << ans << endl;
      exit(0);
    }
    if (M.count(x)) {
      int y = M[x].back();
      M[x].pop_back();
      if (M[x].empty()) M.erase(M.find(x));
      x = y;
    } else {
      x = M.begin()->second.back();
    }
  }
  cout << -1 << endl;
  return 0;
}
