#include <bits/stdc++.h>
std::unordered_map<int, int> mp;
std::multimap<int, int> poz;
std::multimap<int, int>::iterator it;
int a[100000 + 1];
int b[100000 + 1];
bool f[100000 + 1];
int main() {
  FILE *fi, *fout;
  int i, n;
  fi = stdin;
  fout = stdout;
  fscanf(fi, "%d", &n);
  int val = 0;
  for (i = 1; i <= n; i++) {
    fscanf(fi, "%d", &a[i]);
    mp[a[i]]++;
    val ^= a[i];
  }
  mp[val]++;
  for (i = 1; i <= n; i++) {
    fscanf(fi, "%d", &b[i]);
    if (a[i] != b[i]) poz.insert({b[i], i});
    if (mp[b[i]] == 0) {
      fprintf(fout, "-1\n");
      return 0;
    }
    mp[b[i]]--;
  }
  mp.clear();
  int ans = 0;
  for (i = 1; i <= n; i++)
    if (a[i] == b[i]) f[i] = 1;
  it = poz.find(val);
  if (it != poz.end()) {
    int p = it->second;
    while (f[p] == 0) {
      f[p] = 1;
      ans++;
      poz.erase(it);
      it = poz.find(a[p]);
      if (it != poz.end()) p = it->second;
    }
  }
  for (i = 1; i <= n; i++)
    if (f[i] == 0) {
      it = poz.find(a[i]);
      int p = it->second;
      ans++;
      while (f[p] == 0) {
        f[p] = 1;
        ans++;
        poz.erase(it);
        it = poz.find(a[p]);
        if (it != poz.end()) p = it->second;
      }
    }
  fprintf(fout, "%d\n", ans);
  return 0;
}
