#include <bits/stdc++.h>
using namespace std;
const int mxn = 2e5;
int ans = -1;
int dsu[mxn], sz[mxn];
bool active[mxn];
void init() {
  for (int i = 0; i < mxn; i++) dsu[i] = i, sz[i] = 1, active[i] = 0;
}
int root(int v) { return dsu[v] = dsu[v] == v ? v : root(dsu[v]); }
void merge(int v, int w) {
  active[v] = active[w] = 1;
  v = root(v);
  w = root(w);
  if (v == w) return;
  if (sz[v] < sz[w]) swap(v, w);
  sz[v] += sz[w];
  dsu[w] = v;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  n++;
  int a[n], b[n];
  for (int i = 1; i < n; i++) cin >> a[i];
  for (int i = 1; i < n; i++) cin >> b[i];
  a[0] = b[0] = 0;
  for (int i = 1; i < n; i++) a[0] ^= a[i];
  for (int i = 1; i < n; i++) b[0] ^= b[i];
  int c[n], d[n];
  for (int i = 0; i < n; i++) c[i] = a[i], d[i] = b[i];
  sort(c, c + n);
  sort(d, d + n);
  for (int i = 0; i < n; i++)
    if (c[i] != d[i]) {
      cout << -1 << endl;
      return 0;
    }
  map<int, int> cp;
  for (int i = 0; i < n; i++) cp[a[i]] = 0;
  int t = 0;
  for (pair<int, int> pp : cp) cp[pp.first] = t++;
  for (int i = 0; i < n; i++) a[i] = cp[a[i]], b[i] = cp[b[i]];
  init();
  if (a[0] != b[0]) ans++;
  merge(a[0], b[0]);
  for (int i = 1; i < n; i++)
    if (a[i] != b[i]) {
      ans++;
      merge(a[i], b[i]);
    }
  for (int i = 0; i < mxn; i++)
    if (active[i] && dsu[i] == i) ans++;
  cout << ans << endl;
  return 0;
}
