#include <bits/stdc++.h>
using namespace std;
const int maxn = 210000;
int n, m;
int fa[maxn], v[maxn];
int findfa(const int x) { return fa[x] == x ? x : fa[x] = findfa(fa[x]); }
struct node {
  int x, p, i;
} a[maxn], b[maxn];
inline bool cmpx(const node x, const node y) { return x.x < y.x; }
inline bool cmpi(const node x, const node y) { return x.i < y.i; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i].x);
    a[i].i = i;
    a[0].x ^= a[i].x;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i].x);
    b[i].i = i;
    b[0].x ^= b[i].x;
  }
  sort(a, a + n + 1, cmpx);
  sort(b, b + n + 1, cmpx);
  a[0].p = b[0].p = 0;
  if (a[0].x != b[0].x) return puts("-1"), 0;
  for (int i = 1; i <= n; i++) {
    if (a[i].x != b[i].x) return puts("-1"), 0;
    b[i].p = a[i].p = a[i - 1].p + (a[i].x != a[i - 1].x ? 1 : 0);
  }
  m = a[n].p;
  sort(a, a + n + 1, cmpi);
  sort(b, b + n + 1, cmpi);
  for (int i = 0; i <= n; i++) fa[i] = i;
  int re = 0;
  for (int i = 0; i <= n; i++)
    if (a[i].p != b[i].p) {
      if (i) re++;
      int x = findfa(a[i].p), y = findfa(b[i].p);
      if (x != y) fa[y] = x;
      v[x] = 1;
    }
  int tk = 0;
  for (int i = 0; i <= m; i++)
    if (i == findfa(i) && findfa(i) != findfa(0) && v[i]) re++, tk = 1;
  if (a[0].p == b[0].p && !tk) re++;
  printf("%d\n", re);
  return 0;
}
