#include <bits/stdc++.h>
using namespace std;
const long long oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
map<long long, long long> par;
long long Find(long long x) {
  if (par[x] == x) return x;
  return par[x] = Find(par[x]);
}
bool Union(long long x, long long y) {
  x = Find(x), y = Find(y);
  if (x == y) return false;
  if (rand() % 1) swap(x, y);
  par[x] = y;
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  n++;
  vector<long long> a(n), b(n);
  for (long long i = (1); i < (n); i++) {
    cin >> a[i];
    a[0] ^= a[i];
  }
  for (long long i = (1); i < (n); i++) {
    cin >> b[i];
    b[0] ^= b[i];
  }
  vector<long long> sa = a, sb = b;
  sort(begin(sa), end(sa)), sort(begin(sb), end(sb));
  if (sa != sb) {
    cout << -1 << endl;
    return 0;
  }
  long long len = 0;
  for (long long i = (0); i < (n); i++)
    if (a[i] != b[i]) {
      len++;
      par[b[i]] = b[i];
    }
  long long comp = long long((par).size());
  for (long long i = (0); i < (n); i++)
    if (a[i] != b[i]) {
      comp -= Union(a[i], b[i]);
    }
  long long res = comp + len - 1 - ((par).find(a[0]) != end(par));
  if (res < 0) res = 0;
  cout << res << endl;
}
