#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
const double eps = 1e-12;
template <typename T>
inline void read(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
const int N = 100010;
int a[N], b[N];
int tmp[2][N];
int n;
int fat[N], sz[N];
int father(int x) { return fat[x] == x ? x : fat[x] = father(fat[x]); }
int ans = 0;
bool used[N];
signed main() {
  read(n);
  for (int i = 1; i <= n; i++) read(a[i]);
  for (int i = 1; i <= n; i++) a[n + 1] ^= a[i];
  for (int i = 1; i <= n; i++) read(b[i]);
  for (int i = 1; i <= n; i++) b[n + 1] ^= b[i];
  memcpy(tmp[0], a, sizeof(a));
  memcpy(tmp[1], b, sizeof(b));
  for (int i = 0; i < 2; i++) sort(tmp[i] + 1, tmp[i] + n + 2);
  ++n;
  for (int i = 1; i <= n; i++)
    if (tmp[0][i] != tmp[1][i]) {
      puts("-1");
      return 0;
    }
  int tot = n;
  tot = unique(tmp[0], tmp[0] + tot + 1) - tmp[0] - 1;
  for (int i = 1; i <= n; i++)
    a[i] = lower_bound(tmp[0] + 1, tmp[0] + tot + 1, a[i]) - tmp[0];
  for (int i = 1; i <= n; i++)
    b[i] = lower_bound(tmp[0] + 1, tmp[0] + tot + 1, b[i]) - tmp[0];
  for (int i = 1; i <= tot; i++) fat[i] = i, sz[i] = 1;
  for (int i = 1; i <= n; i++) {
    if (a[i] != b[i])
      ans += (i < n);
    else
      continue;
    int fx = father(a[i]), fy = father(b[i]);
    if (a[i] != b[i]) {
      fat[fx] = fy;
      sz[fy] += sz[fx];
      sz[fx] = 0;
    }
  }
  for (int i = 1; i <= tot; i++)
    if (father(i) == i && sz[i] != 1) ++ans;
  ans -= 1;
  printf("%d\n", ans);
  return 0;
}
