#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 10;
int a[MAXN], b[MAXN], cnt[MAXN], c[MAXN];
bool mark[MAXN];
map<int, int> ind;
vector<int> adj[MAXN];
void dfs(int v) {
  mark[v] = true;
  for (auto u : adj[v])
    if (mark[u] == false) dfs(u);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, mis = 0, total = 0, in = 1;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i], c[i] = a[i], total ^= a[i];
  for (int i = 0; i < n; i++) cin >> b[i], c[i + n] = b[i];
  c[2 * n] = total;
  sort(c, c + 2 * n + 1);
  for (int i = 0; i < 2 * n + 1; i++) {
    if (i and c[i] == c[i - 1]) continue;
    ind[c[i]] = in++;
  }
  total = ind[total];
  for (int i = 0; i < n; i++) a[i] = ind[a[i]];
  for (int i = 0; i < n; i++) b[i] = ind[b[i]];
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      b[i] = -1;
      a[i] = -1;
      continue;
    }
    cnt[a[i]]++;
    adj[a[i]].push_back(b[i]);
    adj[b[i]].push_back(a[i]);
    mis++;
  }
  cnt[total]++;
  a[n] = total;
  for (int i = 0; i < n; i++) {
    if (b[i] < 0) continue;
    cnt[b[i]]--;
    if (cnt[b[i]] == -1) return cout << -1, 0;
  }
  int tedad = 0;
  for (int i = 0; i < n + 1; i++) {
    if (a[i] == -1) continue;
    if (adj[a[i]].size() == 0) continue;
    if (mark[a[i]] == false) dfs(a[i]), tedad++;
  }
  if (mis == 0)
    cout << 0 << endl;
  else
    cout << mis + tedad - 1 << endl;
  return 0;
}
