#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
long long read() {
  long long ans = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    ans = (ans << 3) + (ans << 1) + ch - '0';
  return ans * f;
}
int n;
int a[maxn], b[maxn], c[maxn], d[maxn], f[maxn], sz[maxn], ans;
int _find(int x) { return f[x] = f[x] == x ? x : _find(f[x]); }
void _union(int x, int y) {
  int hx = _find(x), hy = _find(y);
  if (hx == hy) return;
  sz[hx] += sz[hy];
  f[hy] = hx;
}
int main() {
  scanf("%d", &n);
  int sa = 0, sb = 0;
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), c[i] = a[i], sa ^= a[i];
  for (int i = 1; i <= n; ++i) scanf("%d", &b[i]), d[i] = b[i], sb ^= b[i];
  c[n + 1] = a[n + 1] = sa;
  d[n + 1] = b[n + 1] = sb;
  sort(c + 1, c + 2 + n);
  sort(d + 1, d + 2 + n);
  for (int i = 1; i <= n + 1; ++i)
    if (c[i] != d[i]) {
      printf("-1");
      return 0;
    }
  int cnt = 0;
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i]) cnt++;
  if (cnt == 0) {
    printf("0\n");
    return 0;
  }
  c[0] = unique(c + 1, c + 1 + n) - c - 1;
  for (int i = 1; i <= n + 1; ++i) {
    a[i] = lower_bound(c + 1, c + 1 + c[0], a[i]) - c;
    b[i] = lower_bound(c + 1, c + 1 + c[0], b[i]) - c;
  }
  for (int i = 1; i <= c[0]; ++i) f[i] = i, sz[i] = 1;
  for (int i = 1; i <= n + 1; ++i) {
    if (a[i] == b[i] && i != n + 1) continue;
    ans += (i < n + 1);
    _union(a[i], b[i]);
  }
  if (f[a[n + 1] == a[n + 1] && sz[a[n + 1]]] == 1) ++ans;
  for (int i = 1; i <= c[0]; ++i)
    if (f[i] == i && sz[i] > 1) ++ans;
  printf("%d", ans - 1);
  return 0;
}
