#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> A(N + 1), B(N + 1);
  unordered_map<int, int> H;
  int all = 0;
  for (int i = 0; i < N; i++) {
    int j;
    cin >> j;
    A[i] = j;
    all = (all ^ j);
    H[j]++;
  }
  A[N] = all;
  H[all]++;
  bool ok = 1;
  int t = 0;
  for (int i = 0; i < N; i++) {
    int j;
    cin >> j;
    B[i] = j;
    t ^= j;
    H[j]--;
    if (H[j] < 0) ok = 0;
  }
  B[N] = t;
  H[t]--;
  if (H[t] < 0) ok = 0;
  if (ok == 0) {
    cout << -1 << endl;
    return 0;
  }
  int op = 0;
  unordered_map<int, vector<int> > AX, BX;
  for (int i = 0; i <= N; i++) {
    if (A[i] != B[i]) {
      AX[A[i]].push_back(i);
      BX[B[i]].push_back(i);
    }
  }
  int idx_a[100010], idx_b[100010];
  int solve[100010];
  memset(idx_a, 0, sizeof(idx_a));
  memset(idx_b, 0, sizeof(idx_b));
  memset(solve, 0, sizeof(solve));
  for (int i = 0; i <= N; i++) {
    if (A[i] != B[i]) {
      int sz = BX[A[i]].size();
      idx_a[i] = BX[A[i]][sz - 1];
      BX[A[i]].pop_back();
      sz = AX[B[i]].size();
      idx_b[i] = AX[B[i]][sz - 1];
      AX[B[i]].pop_back();
    }
  }
  for (int i = 0; i <= N; i++) {
    if (A[i] == B[i]) solve[i] = 1;
  }
  for (int i = 0; i <= N; i++) {
    if (solve[i] == 1) continue;
    bool ki = 0;
    int now = i;
    op++;
    while (solve[now] == 0) {
      op++;
      if (B[now] == all) ki = 1;
      solve[now] = 1;
      now = idx_a[now];
    }
    if (ki) op--;
  }
  if (A[N] != B[N]) op--;
  cout << op << endl;
  return 0;
}
