#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T>
int chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
int chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
T sqr(T a) {
  return a * a;
}
template <class T>
T mmin(T a, T b) {
  return a < b ? a : b;
}
template <class T>
T mmax(T a, T b) {
  return a > b ? a : b;
}
template <class T>
T aabs(T a) {
  return a < 0 ? -a : a;
}
template <class T>
int dcmp(T a, T b) {
  return a > b;
}
template <int *a>
int cmp_a(int first, int second) {
  return a[first] < a[second];
}
int read() {
  int s = 0;
  int base = 0;
  char c;
  while (!isdigit(c = getchar()))
    if (c == '-') base = 1;
  while (isdigit(c)) {
    s = s * 10 + (c ^ 48);
    c = getchar();
  }
  if (base) s = -s;
  return s;
}
int a[102423], b[102423], c[102423], d[102423];
int fa[102423], in[102423], out[102423], In[102423], Out[102423], qaq[102423];
int find(int first) {
  return first == fa[first] ? first : fa[first] = find(fa[first]);
}
void add(int u, int v) {
  ++in[v];
  ++out[u];
  fa[find(u)] = find(v);
}
int main() {
  int n;
  n = read();
  for (int i = 1; i <= n; ++i) {
    a[n + 1] ^= (a[i] = read());
  }
  for (int i = 1; i <= n; ++i) {
    b[n + 1] ^= (b[i] = read());
  }
  for (int i = 1; i <= n + 1; ++i) {
    c[i] = a[i];
    d[i] = b[i];
  }
  sort(c + 1, c + n + 2);
  sort(d + 1, d + n + 2);
  for (int i = 1; i <= n + 1; ++i)
    if (c[i] != d[i]) {
      printf("-1\n");
      return 0;
    }
  int m = unique(c + 1, c + n + 2) - c - 1;
  for (int i = 1; i <= m; ++i) fa[i] = i;
  int w = 0, cnt1 = 0, cnt2 = 0, cnt3 = 0;
  for (int i = 1; i <= n + 1; ++i) {
    a[i] = lower_bound(c + 1, c + m + 1, a[i]) - c;
    b[i] = lower_bound(c + 1, c + m + 1, b[i]) - c;
    if (a[i] != b[i]) {
      add(a[i], b[i]);
      ++w;
    }
  }
  qaq[find(a[n + 1])] = 1;
  for (int i = 1; i <= m; ++i) {
    if (in[i] < out[i])
      Out[find(i)] += out[i] - in[i];
    else
      In[find(i)] += in[i] - out[i];
    if (in[i] || out[i]) qaq[find(i)] = 1;
  }
  for (int i = 1; i <= m; ++i)
    if (qaq[i]) {
      if (Out[i]) {
        cnt1 += Out[i];
        ++cnt2;
      } else {
        ++w;
        ++cnt3;
      }
    }
  if (cnt3 == 1) --w;
  printf("%d\n", w - 1);
  return 0;
}
