#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, cn, i, ans, u, v, fa[N], s[N], A[N], B[N], C[N * 2], a[N], b[N], sz[N];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void link(int u, int v) {
  int fx = find(u), fy = find(v);
  if (fx == fy) return;
  fa[fy] = fx;
  sz[fx] += sz[fy];
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &A[i]), C[++cn] = A[i], A[n + 1] ^= A[i];
  for (i = 1; i <= n; i++) scanf("%d", &B[i]), C[++cn] = B[i], B[n + 1] ^= B[i];
  C[++cn] = A[n + 1];
  C[++cn] = B[n + 1];
  sort(C + 1, C + 1 + cn);
  cn = unique(C + 1, C + 1 + cn) - C - 1;
  for (i = 1; i <= cn; i++) fa[i] = i, sz[i] = 1;
  n++;
  for (i = 1; i <= n; i++)
    if (A[i] != B[i]) {
      u = lower_bound(C + 1, C + 1 + cn, A[i]) - C;
      v = lower_bound(C + 1, C + 1 + cn, B[i]) - C;
      link(u, v);
      if (i != n) ans++;
    }
  sort(A + 1, A + 1 + n);
  sort(B + 1, B + 1 + n);
  for (i = 1; i <= n; i++)
    if (A[i] != B[i]) {
      printf("-1\n");
      return 0;
    }
  for (i = 1; i <= cn; i++)
    if (fa[i] == i && sz[i] > 1) ans++;
  u = lower_bound(C + 1, C + 1 + cn, a[n]) - C;
  if (fa[u] == u && sz[u] == 1) ans++;
  printf("%d\n", ans - 1);
  return 0;
}
