#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 50;
int n, a[N], b[N], p[N], c[N], f[N], ans, tot, d[N];
int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }
int main(int argc, char *argv[]) {
  scanf("%d", &n);
  int s;
  s = 0;
  for (int i = 1; i <= (n); ++i) scanf("%d", a + i), s ^= a[i], c[i] = a[i];
  c[n + 1] = a[n + 1] = s;
  s = 0;
  for (int i = 1; i <= (n); ++i) scanf("%d", b + i), s ^= b[i], d[i] = b[i];
  d[n + 1] = b[n + 1] = s;
  ++n, 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]) {
      ans += i < n;
      if (!f[a[i]]) f[a[i]] = ++tot;
      if (!f[b[i]]) f[b[i]] = ++tot;
    }
  if (!ans) {
    puts("0");
    return 0;
  }
  if (!f[a[n]]) f[a[n]] = ++tot;
  if (!f[b[n]]) f[b[n]] = ++tot;
  for (int i = 1; i <= (tot); ++i) p[i] = i;
  for (int i = 1; i <= (n); ++i)
    if (a[i] != b[i]) p[find(f[a[i]])] = find(f[b[i]]);
  for (int i = 1; i <= (tot); ++i) ans += find(i) == i;
  printf("%d\n", --ans);
  return 0;
}
