#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n;
int a[N], b[N];
int p[N], sz[N], ty[N];
void imp() {
  puts("-1");
  exit(0);
}
int group(int a) {
  if (p[a] == -1) return a;
  return p[a] = group(p[a]);
}
void merge(int a, int b) {
  a = group(a);
  b = group(b);
  if (a == b) return;
  if (sz[a] < sz[b]) swap(a, b);
  sz[a] += sz[b];
  p[b] = a;
  ty[a] |= ty[b];
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  int sm = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sm ^= a[i];
  }
  set<pair<int, int> > tr;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    tr.insert(pair<int, int>(b[i], i));
  }
  fill_n(p, n, -1);
  fill_n(sz, n, 1);
  int pos = -1;
  vector<pair<int, int> > mt;
  for (int i = 0; i < n; i++) {
    ty[i] = (a[i] == sm);
    set<pair<int, int> >::iterator it =
        tr.lower_bound(pair<int, int>(a[i], -1));
    if (it != tr.end() && it->first == a[i]) {
      mt.push_back(pair<int, int>(i, it->second));
      tr.erase(it);
    } else {
      pos = i;
      ty[i] = 1;
    }
  }
  if (tr.size() > 1) imp();
  if (tr.size() == 1) {
    mt.push_back(pair<int, int>(pos, tr.begin()->second));
  }
  for (int i = 0; i < int(mt.size()); i++) {
    pair<int, int>& c = mt[i];
    merge(c.first, c.second);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (i == group(i)) {
      if (sz[i] > 1) ans += sz[i] + (ty[i] == 0);
    }
  }
  cout << ans << endl;
  return 0;
}
