#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)(2e5) + 5;
int a[maxn], b[maxn], c[maxn], fa[maxn], vi[maxn], T[maxn];
int n, tot;
inline int hs(int x) { return lower_bound(T + 1, T + tot + 1, x) - T; }
int Find(int x) { return fa[x] == x ? x : fa[x] = Find(fa[x]); }
int main() {
  scanf("%d", &n);
  ++n;
  for (int i = 1; i < n; ++i) scanf("%d", &a[i]), a[n] ^= a[i];
  for (int i = 1; i < n; ++i) scanf("%d", &b[i]), b[n] ^= b[i];
  for (int i = 1; i <= n; ++i) T[++tot] = a[i], T[++tot] = b[i];
  sort(T + 1, T + tot + 1);
  tot = unique(T + 1, T + tot + 1) - T - 1;
  for (int i = 1; i <= n; ++i) ++c[a[i] = hs(a[i])], --c[b[i] = hs(b[i])];
  for (int i = 1; i <= tot; ++i)
    if (c[i]) return printf("-1\n"), 0;
  for (int i = 1; i <= tot; ++i) fa[i] = i;
  int ret = 0, sgn = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] != b[i]) {
      if (!vi[a[i]]) vi[a[i]] = 1, sgn = -1;
      if (!vi[b[i]]) vi[b[i]] = 1, sgn = -1;
      if (i < n) fa[Find(a[i])] = Find(b[i]), ++ret;
    }
  }
  for (int i = 1; i <= tot; ++i)
    if (vi[i] && Find(i) == i) ++ret;
  return printf("%d\n", ret + sgn), 0;
}
