#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N], b[N], a1[N], b1[N], f[N], sz[N];
int n, m, xsuma, xsumb, Cnt, ans;
void prework() {
  a1[0] = unique(a1 + 1, a1 + 1 + n) - a1 - 1;
  for (int i = 1; i <= n; ++i) {
    a[i] = lower_bound(a1 + 1, a1 + 1 + a1[0], a[i]) - a1;
    b[i] = lower_bound(a1 + 1, a1 + 1 + a1[0], b[i]) - a1;
  }
  for (int i = 1; i <= a1[0]; ++i) f[i] = i, sz[i] = 1;
}
int get_f(int x) { return f[x] = f[x] == x ? x : get_f(f[x]); }
void link(int x, int y) {
  get_f(x);
  get_f(y);
  if (f[x] == f[y]) return;
  sz[f[y]] += sz[f[x]];
  f[f[x]] = f[y];
}
void solve() {
  for (int i = 1; i <= n; ++i) {
    if (a[i] == b[i] && i != n) continue;
    ans += (i < n);
    link(a[i], b[i]);
  }
  if (f[a[n]] == a[n] && sz[a[n]] == 1) ++ans;
  for (int i = 1; i <= a1[0]; ++i)
    if (f[i] == i && sz[i] > 1) ++ans;
}
int main() {
  int tmp;
  bool flag = true;
  scanf("%d", &n);
  xsuma = 0;
  xsumb = 0;
  for (int i = 1; i <= n; ++i) scanf("%d", a + i), a1[i] = a[i], xsuma ^= a[i];
  for (int i = 1; i <= n; ++i) scanf("%d", b + i), b1[i] = b[i], xsumb ^= b[i];
  a[++n] = a1[n] = xsuma;
  b[n] = b1[n] = xsumb;
  sort(a1 + 1, a1 + 1 + n);
  sort(b1 + 1, b1 + 1 + n);
  for (int i = 1; i <= n; ++i)
    if (a1[i] != b1[i]) {
      printf("-1\n");
      return 0;
    }
  Cnt = 0;
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i]) ++Cnt;
  if (Cnt == 0) {
    printf("0\n");
    return 0;
  }
  prework();
  solve();
  printf("%d\n", ans - 1);
}
