#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const long long LINF = INF * (long long)INF;
const int MAX = 100 * 1000 + 47;
const int MOD = 1000 * 1000 * 1000 + 7;
int A[MAX];
int B[MAX];
multimap<int, int> M;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  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];
  }
  for (int i = (0); i < (n); i++) {
    if (B[i] == A[i]) continue;
    M.insert(make_pair(B[i], A[i]));
  }
  int cnt = 0;
  while (1) {
    auto it = M.find(x);
    if (cnt > 3 * n) {
      cout << -1 << endl;
      return 0;
    }
    if (it == M.end()) {
      if (M.empty()) break;
      int key = M.begin()->first;
      int val = M.begin()->second;
      M.erase(M.begin());
      M.insert(make_pair(key, x));
      x = val;
      cnt++;
      continue;
    }
    x = it->second;
    M.erase(it);
    cnt++;
  }
  cout << cnt << endl;
}
