#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long N = 504002;
long long n, x;
long long a[N + 1], b[N + 1];
pair<long long, long long> c[N + 1], d[N + 1];
vector<long long> graph[N + 1];
long long color[N + 1];
void dfs(long long v) {
  color[v] = 1;
  for (auto u : graph[v]) {
    if (color[u] == 0) {
      dfs(u);
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < n; i++) cin >> b[i];
  for (long long i = 0; i < n; i++) a[n] = a[n] ^ a[i];
  for (long long i = 0; i < n; i++) b[n] = b[n] ^ b[i];
  long long n0 = 0;
  for (long long x = 0; x < n; x++) {
    if (a[x] != b[x]) {
      a[n0] = a[x];
      b[n0] = b[x];
      n0++;
    }
  }
  a[n0] = a[n];
  b[n0] = b[n];
  n = n0;
  if (n == 0) {
    cout << 0;
    return 0;
  }
  for (long long i = 0; i <= n; i++) c[i] = {a[i], i};
  for (long long i = 0; i <= n; i++) d[i] = {b[i], i};
  sort(c, c + n + 1);
  sort(d, d + n + 1);
  for (long long i = 0; i <= n; i++) {
    if (c[i].first != d[i].first) {
      cout << -1;
      return 0;
    }
    if (!i || c[i].first != c[i - 1].first) x = c[i].second;
    graph[x].push_back(d[i].second);
  }
  long long same = (a[n] == b[n]), one = 1;
  for (long long i = 0; i < n; i++) {
    if (a[i] == a[n]) one = 0;
  }
  long long ans = n;
  for (long long i = 0; i <= n; i++) {
    if (color[i] == 0) {
      dfs(i);
      ans++;
    }
  }
  cout << n;
}
