#include <bits/stdc++.h>
using namespace std;
int a[101000], b[101000], n, m = 0, f[101000], ans = 0, bz[101000], q[101000],
                             w[101000];
map<int, int> c;
int gf(int x) { return f[x] == 0 ? x : f[x] = gf(f[x]); }
void pedg(int x, int y) {
  int jy = c[x];
  if (jy == 0) c[x] = ++m;
  jy = c[y];
  if (jy == 0) c[y] = ++m;
  x = c[x], y = c[y];
  x = gf(x);
  y = gf(y);
  if (x != y) f[x] = y;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), a[n + 1] ^= a[i], q[i] = a[i];
  for (int i = 1; i <= n; i++)
    scanf("%d", &b[i]), b[n + 1] ^= b[i], w[i] = b[i];
  q[n + 1] = a[n + 1];
  w[n + 1] = b[n + 1];
  sort(q + 1, q + n + 2);
  sort(w + 1, w + n + 2);
  int jy = (a[n + 1] != b[n + 1]);
  for (int i = 1; i <= n; i++)
    if (a[i] == a[n + 1]) jy = 1;
  for (int i = 1; i <= n + 1; i++)
    if (q[i] != w[i]) return printf("-1\n"), 0;
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) pedg(a[i], b[i]), ans++;
  for (int i = 1; i <= m; i++) {
    int x = gf(i);
    if (bz[x] == 0) bz[x] = 1, ans++;
  }
  printf("%d\n", ans - jy);
}
