#include <bits/stdc++.h>
using namespace std;
inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
inline T read() {
  register T sum(0), fg(1);
  register char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') fg = -1;
  for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) - '0' + ch;
  return sum * fg;
}
const int MAXN = (int)2e5;
int n;
int A[MAXN + 5], S = 0;
int B[MAXN + 5];
inline void input() {
  n = read<int>();
  for (int i = 1; i <= n; ++i) A[i] = read<int>(), S ^= A[i];
  for (int i = 1; i <= n; ++i) B[i] = read<int>();
}
inline bool check() {
  static int a[MAXN + 5], b[MAXN + 5];
  a[n + 1] = b[n + 1] = 0;
  for (int i = 1; i <= n; ++i) {
    a[i] = A[i], a[n + 1] ^= a[i];
    b[i] = B[i], b[n + 1] ^= b[i];
  }
  sort(a + 1, a + n + 1 + 1);
  sort(b + 1, b + n + 1 + 1);
  for (int i = 1; i <= n + 1; ++i)
    if (a[i] != b[i]) return 0;
  return 1;
}
int N = 0;
inline void discrete() {
  static int val[MAXN + 5];
  for (int i = 1; i <= n; ++i)
    if (A[i] != B[i]) val[N++] = A[i], val[N++] = B[i];
  val[N++] = S;
  sort(val, val + N), N = unique(val, val + N) - val;
  for (int i = 1; i <= n; ++i)
    if (A[i] != B[i]) {
      A[i] = lower_bound(val, val + N, A[i]) - val + 1;
      B[i] = lower_bound(val, val + N, B[i]) - val + 1;
    }
  S = lower_bound(val, val + N, S) - val + 1;
}
namespace DSU {
int fa[MAXN + 5];
inline void init() {
  for (int i = 1; i <= N; ++i) fa[i] = i;
}
inline int get_fa(int u) { return u == fa[u] ? u : fa[u] = get_fa(fa[u]); }
inline void link(int u, int v) { fa[u] = get_fa(fa[v]); }
}  // namespace DSU
inline void solve() {
  if (!check()) {
    puts("-1");
    return;
  }
  discrete();
  DSU::init();
  for (int i = 1; i <= n; ++i)
    if (A[i] != B[i]) DSU::link(A[i], B[i]);
  int ans = 0;
  for (int i = 1; i <= n; ++i) ans += A[i] != B[i];
  for (int i = 1; i <= N; ++i) ans += i == DSU::fa[i];
  printf("%d\n", ans - 1);
}
int main() {
  input();
  solve();
  return 0;
}
