#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T>
inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
queue<int> Q;
int a[N], b[N], Va[N], Vb[N], fa[N], vis[N];
int e;
inline int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-') ch = getchar();
  if (ch == '-') flag = -1, ch = getchar();
  while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch - '0'), ch = getchar();
  return x * flag;
}
inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  int n = read() + 1;
  for (int i = (1), iend = (n - 1); i <= iend; i++) a[i] = read(), a[n] ^= a[i];
  for (int i = (1), iend = (n - 1); i <= iend; i++) b[i] = read(), b[n] ^= b[i];
  for (int i = (1), iend = (n); i <= iend; i++) Va[i] = a[i], Vb[i] = b[i];
  sort(Va + 1, Va + n + 1);
  sort(Vb + 1, Vb + n + 1);
  for (int i = (1), iend = (n); i <= iend; i++)
    if (Va[i] != Vb[i]) return puts("-1"), 0;
  int cnt = unique(Va + 1, Va + n + 1) - Va - 1;
  for (int i = (1), iend = (n); i <= iend; i++) fa[i] = i;
  for (int i = (1), iend = (n); i <= iend; i++) {
    a[i] = lower_bound(Va + 1, Va + cnt + 1, a[i]) - Va;
    b[i] = lower_bound(Va + 1, Va + cnt + 1, b[i]) - Va;
    int x = find(a[i]), y = find(b[i]);
    if (x == y) continue;
    fa[x] = y;
  }
  int ans = 0, sum = 0;
  for (int i = (1), iend = (n - 1); i <= iend; i++)
    if (a[i] != b[i]) {
      ans++;
      vis[a[i]] = vis[b[i]] = true;
    }
  vis[a[n]] = vis[b[n]] = true;
  for (int i = (1), iend = (cnt); i <= iend; i++)
    if (vis[i] && find(i) == i) sum++;
  printf("%d\n", ans + sum - 1);
  return 0;
}
