#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, poola[N], poolb[N], a[N], b[N], jdga, jdgb, tot, ans, f[N + N];
struct data {
  int a, b;
} dat[N];
int getint() {
  char ch;
  while (!isdigit(ch = getchar()))
    ;
  int x = ch - 48;
  while (isdigit(ch = getchar())) x = x * 10 + ch - 48;
  return x;
}
int find(int x) {
  int l = 0, r = n;
  while (l <= r)
    poola[(l + r >> 1)] <= x ? l = (l + r >> 1) + 1 : r = (l + r >> 1) - 1;
  return l;
}
int getfa(int x) { return f[x] == x ? x : f[x] = getfa(f[x]); }
void merge(int x, int y) {
  x = getfa(x), y = getfa(y);
  if (x != y) f[x] = y;
}
int query(bool jdg) {
  tot = ans = 0;
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i]) dat[++tot] = (data){a[i], b[i]};
  if (jdg) dat[++tot] = (data){jdga, jdgb};
  for (int i = 1; i <= tot; ++i)
    dat[i].a = find(dat[i].a), dat[i].b = find(dat[i].b);
  for (int i = 1; i <= tot + n + 1; ++i) f[i] = i;
  for (int i = 1; i <= tot; ++i)
    merge(i, dat[i].a + tot), merge(i, dat[i].b + tot);
  for (int i = 1; i <= tot; ++i)
    if (f[i] == i) ++ans;
  for (int i = 1; i <= tot; ++i) poola[i] = dat[i].a;
  sort(poola + 1, poola + 1 + tot);
  for (int i = 1; i <= tot; ++i)
    if (i == 1 || poola[i] != poola[i - 1])
      if (f[tot + poola[i]] == tot + poola[i]) ++ans;
  return tot + ans - jdg - jdg;
}
int main() {
  n = getint();
  for (int i = 1; i <= n; ++i) poola[i] = a[i] = getint(), jdga ^= a[i];
  for (int i = 1; i <= n; ++i) poolb[i] = b[i] = getint(), jdgb ^= b[i];
  poola[0] = jdga, poolb[0] = jdgb;
  sort(poola, poola + 1 + n), sort(poolb, poolb + 1 + n);
  for (int i = 0; i <= n; ++i)
    if (poola[i] != poolb[i]) {
      puts("-1");
      return 0;
    }
  printf("%d\n", min(query(0), query(1)));
  return 0;
}
