#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 100000;
const int MAXX = 2 * MAXN + 2;
int n;
int a[MAXN];
int asum;
int b[MAXN];
int bsum;
int aa[MAXN + 1];
int bb[MAXN + 1];
int x[MAXX], nx;
int par[MAXX], sz[MAXX];
int find(int z) {
  if (par[z] == z) return z;
  return par[z] = find(par[z]);
}
void unite(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b) return;
  if (sz[a] < sz[b]) swap(a, b);
  par[b] = a;
  sz[a] += sz[b];
}
int solve() {
  asum = 0;
  for (int i = (0); i < (n); ++i) asum ^= a[i];
  for (int i = (0); i < (n); ++i) aa[i] = a[i];
  aa[n] = asum;
  sort(aa, aa + n + 1);
  bsum = 0;
  for (int i = (0); i < (n); ++i) bsum ^= b[i];
  for (int i = (0); i < (n); ++i) bb[i] = b[i];
  bb[n] = bsum;
  sort(bb, bb + n + 1);
  for (int i = (0); i <= (n); ++i)
    if (aa[i] != bb[i]) return -1;
  nx = 0;
  for (int i = (0); i <= (n); ++i) x[nx++] = aa[i];
  for (int i = (0); i <= (n); ++i) x[nx++] = bb[i];
  sort(x, x + nx);
  nx = unique(x, x + nx) - x;
  for (int i = (0); i < (nx); ++i) par[i] = -1, sz[i] = -1;
  int ndiff = 0;
  for (int i = (0); i <= (n); ++i) {
    int ax = lower_bound(x, x + nx, i == n ? asum : a[i]) - x;
    int bx = lower_bound(x, x + nx, i == n ? bsum : b[i]) - x;
    if (ax == bx) continue;
    if (par[ax] == -1 || sz[ax] == -1) par[ax] = ax, sz[ax] = 1;
    if (par[bx] == -1 || sz[bx] == -1) par[bx] = bx, sz[bx] = 1;
    ++ndiff;
    unite(ax, bx);
  }
  int ncomp = 0;
  for (int i = (0); i < (nx); ++i)
    if (par[i] == i) ++ncomp;
  if (ncomp == 0) return 0;
  return ndiff + ncomp - 1;
}
void run() {
  scanf("%d", &n);
  for (int i = (0); i < (n); ++i) scanf("%d", &a[i]);
  for (int i = (0); i < (n); ++i) scanf("%d", &b[i]);
  printf("%d\n", solve());
}
int main() {
  run();
  return 0;
}
