#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], to[N];
bool loop[N], vis[N];
int loopnum, ans;
void Dfs(int u) {
  vis[u] = 1;
  for (int i = 0, sz = to[u].size(); i < sz; i++) {
    int v = to[u][i];
    if (vis[v]) continue;
    Dfs(v);
  }
}
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;
  memset(vis, 0, sizeof(vis));
  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++) {
        to[pos[tr[b[i]]][j]].push_back(i);
        if (pos[tr[b[i]]][j] == i && !loop[i] && i != n)
          loop[i] = 1, loopnum++, vis[i] = 1;
      }
      sum ^= b[i];
    }
  }
  ans = n - loopnum - 2;
  if (loop[n]) ans++;
  for (int i = 1; i <= n; i++)
    if (!vis[i]) Dfs(i), ans++;
  printf("%d\n", ans);
  return 0;
}
