#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
int pa[1000010];
int par(int x) {
  if (pa[x] == -1) return x;
  return pa[x] = par(pa[x]);
}
inline void uni(int x, int y) {
  x = par(x);
  y = par(y);
  if (x == y) return;
  pa[x] = y;
  return;
}
long long n, i, ans;
long long a[1000010], b[1000010];
long long c[1000010], d[1000010];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (i = 0; i < n; i++) cin >> a[i];
  for (i = 0; i < n; i++) cin >> b[i];
  for (i = 0; i < n; i++) a[n] ^= a[i];
  for (i = 0; i < n; i++) b[n] ^= b[i];
  n++;
  for (i = 0; i < n; i++) c[i] = a[i], d[i] = b[i];
  sort(c, c + n);
  sort(d, d + n);
  for (i = 0; i < n; i++)
    if (c[i] != d[i]) return cout << -1 << endl, 0;
  fill(pa, pa + n, -2);
  for (i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      int t1 = lower_bound(c, c + n, a[i]) - c;
      int t2 = lower_bound(c, c + n, b[i]) - c;
      if (pa[t1] == -2) pa[t1] = -1;
      if (pa[t2] == -2) pa[t2] = -1;
      uni(t1, t2);
      if (i < n - 1) ans++;
    }
  }
  for (i = 0; i < n; i++)
    if (pa[i] == -1) ans++;
  cout << ans - 1 << endl;
  return 0;
}
