#include <bits/stdc++.h>
using namespace std;
int N, A[100005], B[100005], s = 0;
map<int, int> M;
vector<int> V[100005];
set<int> S;
int ind;
int A2[100005], B2[100005];
bool check() {
  sort(A2 + 1, A2 + N + 1);
  sort(B2 + 1, B2 + N + 1);
  for (int i = 1; i <= N; i++)
    if (A2[i] != B2[i]) return 0;
  return 1;
}
void Read() {
  cin >> N;
  for (int i = 2; i <= N + 1; i++) cin >> A[i], A[1] = (A[1] ^ A[i]);
  for (int i = 2; i <= N + 1; i++) cin >> B[i], s = (s ^ B[i]);
  B[1] = s;
  ++N;
  for (int i = 1; i <= N; i++) {
    A2[i] = A[i];
    B2[i] = B[i];
    if (M.find(B[i]) == M.end()) {
      M[B[i]] = ++ind;
    }
    int pos = M[B[i]];
    if (A[i] != B[i] && i > 1) V[pos].push_back(i);
  }
  for (int i = 1; i <= ind; i++)
    if (V[i].size() > 0) S.insert(i);
  for (int i = 1; i <= N; i++) {
    if (M.find(A[i]) != M.end()) A[i] = M[A[i]];
    B[i] = M[B[i]];
  }
}
void Solve() {
  int ans = 0;
  while (!S.empty()) {
    if (V[A[1]].size() > 0) {
      int pos = V[A[1]].back();
      ans++;
      V[A[1]].pop_back();
      if (V[A[1]].size() == 0) S.erase(A[1]);
      swap(A[1], A[pos]);
    } else {
      int last = *S.begin();
      int pos = V[last].back();
      ans++;
      swap(A[1], A[pos]);
    }
  }
  cout << ans << "\n";
}
int main() {
  Read();
  if (check() == 0) {
    cout << "-1\n";
    return 0;
  }
  Solve();
  return 0;
}
