#include <bits/stdc++.h>
using namespace std;
int fa[100010], size[100010];
int a[100010], b[100010], c[100010], d[100010], n;
inline int rd() {
  int x = 0;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    ;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
  return x;
}
inline int find(int x) { return (x == fa[x]) ? x : fa[x] = find(fa[x]); }
int main() {
  n = rd();
  for (int i = 1; i <= n; i++) a[n + 1] ^= (a[i] = rd());
  for (int i = 1; i <= n; i++) b[n + 1] ^= (b[i] = rd());
  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;
    }
  int ans = 0;
  for (int i = 1; i < n; i++)
    if (a[i] ^ b[i]) ans++;
  if (!ans) {
    puts("0");
    return 0;
  }
  int m = unique(c + 1, c + n + 1) - c - 1;
  for (int i = 1; i <= m; i++) fa[i] = i, size[i] = 1;
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(c + 1, c + n + 1, a[i]) - c;
    b[i] = lower_bound(c + 1, c + n + 1, b[i]) - c;
    int f1 = find(a[i]), f2 = find(b[i]);
    if (f1 != f2) fa[f1] = f2, size[f2] += f1;
  }
  for (int i = 1; i <= m; i++)
    if (fa[i] == i && size[i] > 1) ans++;
  printf("%d\n", ans + (a[n] == b[n] && size[find(a[n])] == 1) - 1);
  return 0;
}
