#include <bits/stdc++.h>
using namespace std;
const int MC = 100010;
int N;
int a[MC], c[MC];
int b[MC], d[MC];
bool w[MC];
int e[MC];
int BS(int x, int y, int p) {
  if (y - x < 2) return x;
  int z = (x + y) / 2;
  return d[e[z]] < p ? BS(z, y, p) : BS(x, z, p);
}
int main() {
  scanf("%d", &N);
  int x = 0;
  for (int i = (int)(0); i < (int)(N); ++i) {
    scanf("%d", a + i);
    c[i] = a[i];
    x ^= a[i];
  }
  for (int i = (int)(0); i < (int)(N); ++i) {
    scanf("%d", b + i);
    d[i] = b[i];
    e[i] = i;
  }
  sort(c, c + N);
  sort(d, d + N);
  sort(e, e + N, [&](const int m, const int n) { return d[m] < d[n]; });
  c[N] = -1;
  int z = 0;
  int r = 0;
  int q = c[N - 1];
  for (int i = (int)(0); i < (int)(N); ++i) {
    if (c[i + z - r] == d[i]) continue;
    if (r == 0 && x == d[i]) {
      r = 1;
      continue;
    }
    ++z;
    q = c[i + z - r];
    --i;
    if (r + z > 1) {
      printf("-1\n");
      return 0;
    }
  }
  if (r == 0) q = x;
  int p = 0;
  for (int i = (int)(0); i < (int)(N); ++i) {
    w[i] = a[i] == b[i];
    if (w[i]) ++p;
  }
  int s = 0;
  for (int i = (int)(0); i < (int)(N); ++i) {
    if (b[i] != a[i] && b[i] == x) {
      int f = BS(0, N, a[i]);
      if (b[e[f]] != a[i]) ++f;
      while (b[e[f]] == a[i]) {
        if (a[e[f]] != q) {
          s = -1;
          break;
        }
        ++f;
      }
      s = 1;
    }
    if (s < 0) break;
  }
  if (s < 0) s = 0;
  if (N - p == 2) s = 0;
  printf("%d\n", N - p + s);
  return 0;
}
