#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, cnt = 0;
  for (i = 1; i <= n; i++)
    if (a[i] == b[i]) {
      cnt++;
      f[i] = 1;
    }
  if (cnt == n)
    fprintf(fout, "0\n");
  else {
    for (i = 1; i <= n; i++) mp[b[i]]++;
    i = 1;
    while (i <= n && mp[a[i]] > 0) {
      mp[a[i]]--;
      i++;
    }
    if (i <= n) {
      it = poz.find(val);
      int p = it->second;
      poz.erase(it);
      while (f[p] == 0) {
        f[p] = 1;
        ans++;
        it = poz.find(a[p]);
        val = a[p];
        if (it != poz.end()) {
          p = it->second;
          poz.erase(it);
        }
      }
    }
    for (i = 1; i <= n; i++)
      if (f[i] == 0) {
        int p = i;
        if (val == a[p]) ans--;
        while (f[p] == 0) {
          f[p] = 1;
          it = poz.find(a[p]);
          val = a[p];
          ans++;
          p = it->second;
          poz.erase(it);
        }
        ans++;
      }
    fprintf(fout, "%d\n", ans);
  }
  return 0;
}
