#include <bits/stdc++.h>
using namespace std;
const int N = 100500;
int read(int &n) {
  char ch = ' ';
  int q = 0, w = 1;
  for (; (ch != '-') && ((ch < '0') || (ch > '9')); ch = getchar())
    ;
  if (ch == '-') w = -1, ch = getchar();
  for (; ch >= '0' && ch <= '9'; ch = getchar()) q = q * 10 + ch - 48;
  n = q * w;
  return n;
}
int m, n, m1, ans;
int a[N], b[N];
map<int, int> zc;
map<int, int> A;
map<int, bool> z;
int B[2 * N][2], B0;
void link(int q, int w) { B[++B0][0] = A[q], A[q] = B0, B[B0][1] = w; }
void dfs(int q) {
  z[q] = 1;
  for (int i = A[q]; i; i = B[i][0])
    if (!z[B[i][1]]) dfs(B[i][1]);
}
int main() {
  int q, w, e;
  read(n);
  q = w = 0;
  for (register int i = 1; i <= n; ++i) q = q ^ read(a[i]), ++zc[a[i]];
  for (register int i = 1; i <= n; ++i) {
    if (a[i] != read(b[i])) {
      ++ans;
      link(b[i], a[i]);
    }
    if ((--zc[b[i]]) < 0) ++w, e = b[i];
  }
  if (!ans) return printf("0\n"), 0;
  if (w > 1) return printf("-1\n"), 0;
  for (register int i = 1; i <= n; ++i)
    if (a[i] != b[i] && !z[b[i]]) dfs(b[i]), ++ans;
  if (z[q] && !w || e == q) --ans;
  if (w && e != q)
    printf("-1\n");
  else
    printf("%d\n", ans);
  return 0;
}
