#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];
long long c[N + 1], d[N + 1];
map<long long, vector<long long> > graph;
map<long long, long long> color;
long long ans;
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];
  for (long long i = 0; i < n; i++) {
    if (a[i] != b[i]) ans++;
  }
  if (!ans) {
    cout << 0;
    return 0;
  }
  for (long long i = 0; i <= n; i++) c[i] = a[i];
  for (long long i = 0; i <= n; i++) d[i] = b[i];
  sort(c, c + n + 1);
  sort(d, d + n + 1);
  for (long long i = 0; i <= n; i++) {
    if (c[i] != d[i]) {
      cout << -1;
      return 0;
    }
  }
  for (long long i = 0; i <= n; i++) {
    if (a[i] != b[i]) {
      graph[a[i]].push_back(b[i]);
      color[a[i]] = 0, color[b[i]] = 0;
    }
  }
  color[a[n]] = 0, color[b[n]] = 0;
  for (long long i = 0, x = c[0]; i <= n; i++, x = c[i]) {
    if (a[i] != b[i] && color[x] == 0) {
      dfs(x);
      ans++;
    }
  }
  cout << ans - 1;
}
