#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, all, size;
int a[N], b[N], p[N], fa[N], sz[N];
map<int, int> mp, S;
template <typename T>
inline void chkmin(T &x, T y) {
  x = min(x, y);
}
template <typename T>
inline void chkmax(T &x, T y) {
  x = max(x, y);
}
template <typename T>
inline void read(T &x) {
  T f = 1;
  x = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -f;
  for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
  x *= f;
}
inline int get_root(int x) {
  if (p[x] == x)
    return x;
  else
    return p[x] = get_root(p[x]);
}
int main() {
  read(n);
  for (int i = 1; i <= n; ++i) {
    read(a[i]);
    all ^= a[i];
    if (!mp.count(a[i])) mp[a[i]] = ++size;
    a[i] = mp[a[i]];
    ++S[a[i]];
  }
  if (!mp.count(all)) mp[all] = ++size;
  all = mp[all];
  S[all] = 1;
  int ans = -1;
  for (int i = 1; i <= n; ++i) {
    read(b[i]);
    b[i] = mp[b[i]];
    --S[b[i]];
    if (S[b[i]] < 0) {
      printf("-1\n");
      return 0;
    }
  }
  for (int i = 1; i <= size; ++i) p[i] = i, sz[i] = 1;
  for (int i = 1; i <= n; ++i) {
    if (a[i] != b[i]) ++ans;
    int u = get_root(a[i]), v = get_root(b[i]);
    if (u != v) {
      p[u] = v;
      sz[v] += sz[u];
    }
  }
  for (int i = 1; i <= size; ++i)
    if (sz[p[i]] != 1 || i == all) ans += get_root(i) == i;
  printf("%d\n", ans);
  return 0;
}
