#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
int a[maxn], b[maxn], n, c[maxn], m, cnt[maxn] = {0}, d[maxn];
bool flag[maxn];
vector<int> id[maxn];
int ans = 0, idgg = 1, idw[maxn] = {0};
int q;
void dfs() {
  if (id[a[n + 1]].size() == 0) {
    while (idgg <= n && flag[idgg]) idgg++;
    if (idgg > n || flag[idgg]) return;
    ans++;
    swap(a[idgg], a[n + 1]);
    dfs();
    return;
  }
  while (idw[a[n + 1]] < id[a[n + 1]].size() &&
         flag[id[a[n + 1]][idw[a[n + 1]]]])
    idw[a[n + 1]]++;
  if (idw[a[n + 1]] >= id[a[n + 1]].size() || flag[id[a[n + 1]][idw[a[n + 1]]]])
    return;
  ans++;
  flag[id[a[n + 1]][idw[a[n + 1]]]] = true;
  swap(a[id[a[n + 1]][idw[a[n + 1]]]], a[n + 1]);
  dfs();
  return;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[n + 1] ^= a[i];
  }
  for (int i = 1; i <= n + 1; i++) c[i] = a[i];
  sort(c + 1, c + n + 1 + 1);
  m = n + 1;
  m = unique(c + 1, c + m + 1) - c - 1;
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    d[i] = b[i];
  }
  sort(d + 1, d + n + 1);
  int p = 1;
  for (int i = 1; i <= n; i++) {
    while (p <= m && c[p] < d[i]) p++;
    if (c[p] != d[i]) {
      cout << -1 << endl;
      return 0;
    }
    cnt[p]++;
  }
  for (int i = 1; i <= n + 1; i++) d[i] = a[i];
  sort(d + 1, d + n + 1 + 1);
  p = 1;
  for (int i = 1; i <= n + 1; i++) {
    while (p <= m && c[p] < d[i]) p++;
    cnt[p]--;
  }
  for (int i = 1; i <= m; i++) {
    if (cnt[i] > 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = 1; i <= n + 1; i++) {
    int x = lower_bound(c + 1, c + m + 1, a[i]) - c;
    a[i] = x;
  }
  for (int i = 1; i <= n; i++) {
    int x = lower_bound(c + 1, c + m + 1, b[i]) - c;
    b[i] = x;
  }
  for (int i = 1; i <= n + 1; i++) {
    if (a[i] == b[i]) {
      flag[i] = true;
      continue;
    }
    id[b[i]].push_back(i);
  }
  dfs();
  cout << ans << endl;
  return 0;
}
