#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
const int N = 200000 + 10;
const long long MOD = 1000000000 + 7;
const long long INF = 1000000010;
const long long LOG = 25;
int n, a[N], b[N], cnt[N], par[N], ans, sz[N];
vector<int> num, G[N], GG[N];
bool mark[N];
int getpar(int v) { return (par[v] == v ? v : par[v] = getpar(par[v])); }
void merge(int v, int u) {
  v = getpar(v), u = getpar(u);
  if (v == u) return;
  if (sz[v] < sz[u]) swap(v, u);
  ans--;
  par[u] = v;
  sz[v] += sz[u];
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int X = 0;
  cin >> n;
  for (int i = 1; i < N; i++) par[i] = i, sz[i] = 1;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    X ^= a[i];
    num.push_back(a[i]);
  }
  a[n + 1] = X;
  num.push_back(X);
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    num.push_back(b[i]);
  }
  sort(num.begin(), num.end());
  num.resize(unique(num.begin(), num.end()) - num.begin());
  for (int i = 1; i <= n + 1; i++) {
    a[i] = lower_bound(num.begin(), num.end(), a[i]) - num.begin() + 1;
    cnt[a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    b[i] = lower_bound(num.begin(), num.end(), b[i]) - num.begin() + 1;
    if (cnt[b[i]] == 0) return cout << -1, 0;
    cnt[b[i]]--;
  }
  for (int i = 0; i < N; i++) {
    if (cnt[i] == 1) {
      X = i;
      break;
    }
  }
  memset(cnt, 0, sizeof cnt);
  for (int i = 1; i <= n; i++) {
    if (a[i] == b[i]) continue;
    ans++;
    cnt[a[i]]++;
    G[b[i]].push_back(i);
  }
  G[b[n + 1]].push_back(n + 1);
  if (ans == 0) return cout << 0, 0;
  ans++;
  ans *= 2;
  ans -= 2;
  for (int i = 1; i <= n; i++) {
    if (a[i] == b[i]) continue;
    GG[a[i]].push_back(i);
  }
  GG[a[n + 1]].push_back(n + 1);
  for (int i = 1; i <= n; i++) {
    if (a[i] == b[i]) continue;
    if (!mark[a[i]]) {
      mark[a[i]] = 1;
      for (auto u : G[a[i]]) {
        merge(i, u);
      }
      for (auto u : GG[a[i]]) merge(i, u);
    }
  }
  if (!mark[a[n + 1]]) {
    for (auto u : G[a[n + 1]]) {
      merge(n + 1, u);
    }
  }
  cout << ans;
  return 0;
}
