#include <bits/stdc++.h>
inline int read() {
  int ans = 0, f = 1;
  char c = getchar();
  while ((c < '0') | (c > '9')) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while ((c >= '0') & (c <= '9')) {
    ans = (ans << 1) + (ans << 3) + int(c) - 48;
    c = getchar();
  }
  return ans * f;
}
inline void write(int x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x) {
    write(x / 10);
    putchar(char(x % 10 + 48));
  }
}
int n, num, ans, a[100001], b[100001], xa, xb, ua[100001], ub[100001],
    father[100001], uni[100001];
std::map<int, int> mp;
inline int find(register int x) {
  return father[x] == x ? father[x] : father[x] = find(father[x]);
}
int main(register int FUCK) {
  n = read();
  for (register int i = 1; i <= n; i = -~i) a[i] = read();
  for (register int i = 1; i <= n; i = -~i) b[i] = read();
  for (register int i = 1; i <= n; i = -~i) xa ^= a[i];
  for (register int i = 1; i <= n; i = -~i) xb ^= b[i];
  n = -~n;
  a[n] = xa;
  b[n] = xb;
  for (register int i = 1; i <= n; i = -~i) ua[i] = a[i];
  for (register int i = 1; i <= n; i = -~i) ub[i] = b[i];
  std::sort(ua + 1, ua + 1 + n);
  std::sort(ub + 1, ub + 1 + n);
  for (register int i = 1; i <= n; i = -~i)
    if (ua[i] - ub[i]) {
      printf("-1\n");
      return 0;
    }
  for (register int i = 1; i <= n; i = -~i)
    if (a[i] - b[i] || i == n) {
      uni[++num] = a[i];
      uni[++num] = b[i];
      if (i < n) ans = -~ans;
    }
  if (!ans) {
    printf("0\n");
    return 0;
  }
  std::sort(uni + 1, uni + num + 1);
  num = std::unique(uni + 1, uni + num + 1) - uni - 1;
  for (register int i = 1; i <= num; i = -~i) father[i] = i;
  for (register int i = 1; i <= n; i = -~i)
    if (a[i] - b[i]) {
      a[i] = std::lower_bound(uni + 1, uni + num + 1, a[i]) - uni;
      b[i] = std::lower_bound(uni + 1, uni + num + 1, b[i]) - uni;
      if (!mp[a[i]]) mp[a[i]] = 1;
      if (!mp[b[i]]) mp[b[i]] = 1;
      father[find(a[i])] = find(b[i]);
    }
  for (register int i = 1; i <= num; i = -~i)
    if (father[i] == i) ans++;
  write(ans - 1);
  putchar('\n');
  return 0;
}
