#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007ll;
const double EPS = 1e-11;
int n;
int a[125252], b[125252];
int c[125252], d[125252];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (int)(n); ++i) scanf("%d", a + i);
  for (int i = 0; i < (int)(n); ++i) scanf("%d", b + i);
  for (int i = 0; i < (int)(n); ++i) c[i] = a[i], c[n] ^= a[i];
  for (int i = 0; i < (int)(n); ++i) d[i] = b[i], d[n] ^= b[i];
  a[n] = c[n];
  b[n] = d[n];
  sort(c, c + n + 1);
  sort(d, d + n + 1);
  for (int i = 0; i < (int)(n + 1); ++i)
    if (c[i] != d[i]) {
      puts("-1");
      return 0;
    }
  multiset<pair<int, int> > bag;
  for (int i = 0; i < (int)(n); ++i)
    if (a[i] != b[i]) {
      bag.insert(pair<int, int>(b[i], a[i]));
    }
  int have = a[n];
  int amari = b[n];
  int amarest = 0;
  for (pair<int, int> complex<double> : bag) {
    if (complex<double>.first == amari) amarest++;
  }
  int ans = 0;
  while (bag.size() > 0) {
    auto it = bag.lower_bound(pair<int, int>(have, -1));
    if (amarest == 0 && bag.size() >= 2 && it != bag.end() &&
        it->second == amari)
      it++;
    if (it == bag.end() || it->first != have) {
      it = bag.begin();
      if (amarest == 0 && bag.size() >= 2 && it->second == amari) it++;
      pair<int, int> complex<double> = *it;
      swap(complex<double>.second, have);
      bag.erase(it);
      bag.insert(complex<double>);
    } else {
      if (it->first == amari) amarest--;
      have = it->second;
      bag.erase(it);
    }
    ans++;
  }
  printf("%d\n", ans);
  return 0;
}
