#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch - '0' < 0 || ch - '0' > 9) {
    if (ch == '-') f = -1;
  }
  while (ch - '0' >= 0 && ch - '0' <= 9) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
map<int, int> ma;
int n;
int a[100010], b[100010], c[100010], d[100010];
int book[100010];
int fa[100010];
int ans;
int find(int x) {
  if (fa[x] == x) return x;
  return fa[x] = find(fa[x]);
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  for (int i = 1; i <= n; i++) b[i] = read();
  for (int i = 1; i <= n; i++) a[n + 1] ^= a[i];
  for (int i = 1; i <= n; i++) b[n + 1] ^= b[i];
  int flag = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] != b[i]) flag = 1;
  }
  if (flag == 0) {
    puts("0");
    return 0;
  }
  n++;
  for (int i = 1; i <= n; i++) c[i] = a[i];
  for (int i = 1; i <= n; 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 N = unique(c + 1, c + n + 1) - (c + 1);
  for (int i = 1; i <= N; i++) ma[c[i]] = i;
  for (int i = 1; i <= N; i++) fa[i] = i;
  for (int i = 1; i <= n - 1; i++) {
    if (a[i] == b[i]) continue;
    int x = ma[a[i]], y = ma[b[i]];
    book[x] = book[y] = 1;
    if (find(x) != find(y)) fa[x] = y;
    ans++;
  }
  book[ma[a[n]]] = 1;
  for (int i = 1; i <= N; i++)
    if (book[i] && find(i) == i) ans++;
  printf("%d\n", ans - 1);
  return 0;
}
