#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
inline long long in() {
  long long x = 0, flag = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') flag = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * flag;
}
int n, m, ans;
long long xsum, ori[MAXN], tar[MAXN], tot[MAXN];
int a[MAXN], b[MAXN];
bool vis[MAXN];
map<long long, int> cnt;
map<long long, int> id;
vector<int> vec[MAXN];
int fa[MAXN];
int find(int x) {
  if (fa[x] != x) fa[x] = find(fa[x]);
  return fa[x];
}
void unite(int x, int y) { fa[find(y)] = find(x); }
bool cmp(int x, int y) { return ori[x] < ori[y]; }
bool judge() {
  for (int i = 1; i <= n; i++) cnt[ori[i]] = cnt[ori[i]] + 1;
  cnt[xsum] = cnt[xsum] + 1;
  for (int i = 1; i <= n; i++) {
    if (cnt[tar[i]] > 0)
      cnt[tar[i]]--;
    else
      return false;
  }
  return true;
}
void init() {
  sort(tot + 1, tot + n + 2);
  id[tot[1]] = 1;
  int kind = 1;
  for (int i = 2; i <= n + 1; i++) {
    if (tot[i] != tot[i - 1]) kind++;
    id[tot[i]] = kind;
    fa[kind] = kind;
  }
  for (int i = 1; i <= n; i++) tar[n + 1] ^= tar[i];
  if (tar[n + 1] != ori[n + 1]) ans -= 2;
  for (int i = 1; i <= n + 1; i++) {
    a[i] = id[ori[i]];
    b[i] = id[tar[i]];
    vec[b[i]].push_back(i);
  }
  for (int i = 1; i <= kind; i++) {
    for (int j = 0; j < vec[i].size(); j++) {
      if (j > 0) unite(vec[i][j - 1], vec[i][j]);
    }
  }
}
int main() {
  n = in();
  for (int i = 1; i <= n; i++) {
    tot[i] = ori[i] = in();
    xsum ^= ori[i];
  }
  ori[n + 1] = tot[n + 1] = xsum;
  for (int i = 1; i <= n; i++) tar[i] = in();
  if (judge() == false) {
    printf("-1");
    return 0;
  }
  init();
  for (int i = 1; i <= n + 1; i++)
    if (a[i] == b[i])
      vis[i] = true;
    else
      ans++;
  for (int i = 1; i <= n + 1; i++) {
    unite(i, vec[a[i]][0]);
  }
  for (int i = 1; i <= n + 1; i++) {
    if (vis[i]) continue;
    if (fa[i] == i) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
