#include <bits/stdc++.h>
using namespace std;
int F() {
  char ch;
  int x, a;
  while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-')
    ;
  if (ch == '-')
    ch = getchar(), a = -1;
  else
    a = 1;
  x = ch - '0';
  while (ch = getchar(), ch >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + ch - '0';
  return a * x;
}
const int N = 1e5 + 10;
int n, a[N], b[N], sum, ans;
int c[N], d[N];
int t[N << 1], m, fa[N];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void unit(int x, int y) {
  if (find(x) != find(y)) fa[find(x)] = find(y);
}
int main() {
  ((n) = F());
  for (int i = 1; i <= n; ++i) ((a[i]) = F()), sum ^= a[i];
  a[n + 1] = sum, sum = 0;
  for (int i = 1; i <= n; ++i) ((b[i]) = F()), sum ^= b[i];
  b[n + 1] = sum, sum = 0;
  ++n;
  for (int i = 1; i <= n; ++i) c[i] = a[i], d[i] = b[i];
  sort(c + 1, c + n + 1);
  sort(d + 1, d + n + 1);
  for (int i = 1; i <= n; ++i)
    if (c[i] != d[i]) {
      puts("-1");
      return 0;
    }
  for (int i = 1; i <= n; ++i) {
    if (a[i] != b[i] && i < n) ++ans;
    if (a[i] != b[i]) t[++m] = a[i], t[++m] = b[i];
  }
  if (!ans) {
    puts("0");
    return 0;
  }
  sort(t + 1, t + m + 1);
  m = unique(t + 1, t + m + 1) - t - 1;
  for (int i = 1; i <= m; ++i) fa[i] = i;
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i])
      unit(lower_bound(t + 1, t + m + 1, a[i]) - t,
           lower_bound(t + 1, t + m + 1, b[i]) - t);
  for (int i = 1; i <= m; ++i)
    if (find(i) == i) ++ans;
  printf("%d\n", ans - 1);
  return 0;
}
