#include <bits/stdc++.h>
using namespace std;
int a[100100], b[100100];
pair<int, int> A[100100], B[100100];
int p[100100];
int col[100100];
vector<int> com[100100];
int main() {
  int n;
  cin >> n;
  int x = 0;
  for (int i = 0; i < (int)(n); ++i) {
    cin >> a[i];
    x ^= a[i];
    A[i] = make_pair(a[i], i);
  }
  a[n] = x;
  A[n] = make_pair(x, n);
  x = 0;
  for (int i = 0; i < (int)(n); ++i) {
    cin >> b[i];
    x ^= b[i];
    B[i] = make_pair(b[i], i);
  }
  b[n] = x;
  B[n] = make_pair(x, n);
  ++n;
  sort(A, A + n);
  sort(B, B + n);
  bool ok = true;
  for (int i = 0; i < (int)(n); ++i) {
    if (A[i].first != B[i].first) {
      ok = false;
    }
    p[A[i].second] = B[i].second;
  }
  if (!ok) {
    cout << -1 << endl;
    return 0;
  }
  int cur = 1;
  for (int i = 0; i < (int)(n); ++i) {
    if (p[i] == i || col[i]) {
      continue;
    }
    col[i] = cur++;
    com[col[i]].push_back(i);
    int v = p[i];
    while (v != i) {
      col[v] = col[i];
      com[col[v]].push_back(v);
      v = p[v];
    }
  }
  for (int i = 0; i < (int)(n - 1); ++i) {
    if (A[i].first != A[i + 1].first) {
      continue;
    }
    int x = A[i].second, y = A[i + 1].second;
    if (col[x] == col[y]) {
      continue;
    }
    if (col[x] == 0 && x < n - 1) {
      continue;
    }
    if (col[y] == 0 && y < n - 1) {
      continue;
    }
    if (com[col[x]].size() < com[col[y]].size()) {
      swap(x, y);
    }
    int ccol = col[y];
    for (int j = 0; j < (int)(com[ccol].size()); ++j) {
      int tmp = com[ccol][j];
      col[tmp] = col[x];
      com[col[x]].push_back(tmp);
    }
  }
  long long ans = 0;
  for (int i = 0; i < (int)(n); ++i) {
    if (col[i] == 0) {
      continue;
    }
    if (col[i] == col[n - 1]) {
      ans += com[col[i]].size() - 1;
    } else {
      ans += com[col[i]].size() + 1;
    }
    int ccol = col[i];
    for (int j = 0; j < (int)(com[ccol].size()); ++j) {
      int tmp = com[ccol][j];
      col[tmp] = 0;
    }
  }
  cout << ans << endl;
  return 0;
}
