#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, A[100001], B[100001];
  cin >> N;
  for (int i = 0; i < N; i++) cin >> A[i];
  for (int i = 0; i < N; i++) cin >> B[i];
  A[N] = B[N] = 0;
  for (int i = 0; i < N; i++) {
    A[N] ^= A[i];
    B[N] ^= B[i];
  }
  bitset<100001> ok;
  map<int, vector<int>> mpA, mpB;
  for (int i = 0; i <= N; i++) {
    if (i < N && A[i] == B[i]) {
      ok[i] = 1;
    } else {
      mpA[A[i]].push_back(i);
      mpB[B[i]].push_back(i);
    }
  }
  int ans = 0;
  for (int st = N; st >= 0; st--) {
    if (ok[st]) continue;
    if (st < N) ans += 2;
    mpB[B[st]].pop_back();
    int p = st;
    while (true) {
      if (mpB[A[p]].size()) {
        ans++;
        ok[p] = 1;
        int p2 = mpB[A[p]].back();
        mpB[A[p]].pop_back();
        p = p2;
      } else {
        if (A[p] == B[st]) {
          ok[p] = 1;
          break;
        } else {
          cout << -1 << endl;
          return 0;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
