#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
bool chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
const int maxn = 1e5 + 1e2;
int n;
int Ans;
int a[maxn], b[maxn], c[maxn], d[maxn];
int fa[maxn];
map<int, int> e;
inline void file() {
  freopen("AGC016D.in", "r", stdin);
  freopen("AGC016D.out", "w", stdout);
}
inline int read() {
  int x = 0, p = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - '0');
    c = getchar();
  }
  return x * p;
}
int find(int x) {
  if (x != fa[x]) fa[x] = find(fa[x]);
  return fa[x];
}
int main() {
  n = read();
  for (int i = (int)1; i <= (int)n; ++i) a[i] = read(), a[n + 1] ^= a[i];
  for (int i = (int)1; i <= (int)n; ++i) b[i] = read(), b[n + 1] ^= b[i];
  int flag = 1;
  for (int i = (int)1; i <= (int)n; ++i)
    if (a[i] != b[i]) flag = 0;
  if (flag) {
    puts("0");
    return 0;
  }
  ++n;
  for (int i = (int)1; i <= (int)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 = (int)1; i <= (int)n; ++i)
    if (c[i] != d[i]) {
      puts("-1");
      return 0;
    }
  int N = unique(c + 1, c + n + 1) - (c + 1);
  for (int i = (int)1; i <= (int)N; ++i) e[c[i]] = i;
  memset(d, 0, sizeof(d));
  for (int i = (int)1; i <= (int)N; ++i) fa[i] = i;
  for (int i = (int)1; i <= (int)n - 1; ++i)
    if (a[i] != b[i]) {
      int x = e[a[i]], y = e[b[i]];
      d[e[a[i]]] = d[e[b[i]]] = 1;
      x = find(x);
      y = find(y);
      ++Ans;
      if (x != y) fa[x] = y;
    }
  for (int i = (int)1; i <= (int)N; ++i)
    if (d[i] && find(i) == i) ++Ans;
  printf("%d\n", Ans - 1);
  return 0;
}
