#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, a[N], b[N], sum;
map<int, int> tr;
int trnum, exi[N];
vector<int> pos[N];
bool loop[N];
int loopnum, ans, fa[N], tot;
int Find(int u) {
  if (u == fa[u]) return u;
  return fa[u] = Find(fa[u]);
}
void Merge(int x, int y) {
  if (Find(x) == Find(y)) return;
  fa[Find(x)] = Find(y);
}
int main() {
  scanf("%d", &n);
  n++;
  trnum = 0;
  tr.clear();
  sum = 0;
  for (int i = 1; i <= n; i++) {
    if (i < n)
      scanf("%d", &a[i]);
    else
      a[i] = sum;
    if (tr.find(a[i]) == tr.end()) {
      tr[a[i]] = ++trnum;
      exi[tr[a[i]]] = 0;
    }
    pos[tr[a[i]]].push_back(i);
    exi[tr[a[i]]]++;
    sum ^= a[i];
  }
  sum = 0;
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= n; i++) {
    if (i < n)
      scanf("%d", &b[i]);
    else
      b[i] = sum;
    if (tr.find(b[i]) == tr.end()) {
      printf("-1");
      return 0;
    } else if (exi[tr[b[i]]] == 0) {
      printf("-1");
      return 0;
    } else {
      exi[tr[b[i]]]--;
      for (int j = 0, sz = pos[tr[b[i]]].size(); j < sz; j++) {
        if (pos[tr[b[i]]][j] == i && !loop[i] && i != n) loop[i] = 1, loopnum++;
        Merge(pos[tr[b[i]]][j], i);
      }
      sum ^= b[i];
    }
  }
  tot = 0;
  for (int i = 1; i <= n; i++)
    if (fa[i] == i && !loop[i]) tot++;
  ans = n - loopnum + tot - 2;
  printf("%d\n", ans);
  return 0;
}
