#include <bits/stdc++.h>
using namespace std;
const int maxn = (1 << 17) + 10;
int n, ans, tag, f[maxn], g[maxn];
vector<pair<int, int> > a, b;
int F(int i) { return f[i] == i ? i : f[i] = F(f[i]); }
int main() {
  scanf("%d", &n);
  for (int i = 0, s = 0; i < n; ++i) {
    int first;
    scanf("%d", &first);
    a.push_back(make_pair(first, i));
    s ^= first;
    if (i == n - 1) {
      a.push_back(make_pair(s, n));
    }
  }
  for (int i = 0, s = 0; i < n; ++i) {
    int first;
    scanf("%d", &first);
    b.push_back(make_pair(first, i));
    s ^= first;
    if (i == n - 1) {
      b.push_back(make_pair(s, n));
    }
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for (int i = 0; i <= n; ++i) {
    if (a[i].first != b[i].first) {
      return !puts("-1");
    }
    g[a[i].first = b[i].first =
          (i ? a[i - 1].first + (a[i].first != a[i - 1].first) : 0)] = 1;
    f[a[i].first] = a[i].first;
  }
  auto cmp = [&](pair<int, int> first, pair<int, int> second) {
    return first.second < second.second;
  };
  sort(a.begin(), a.end(), cmp);
  sort(b.begin(), b.end(), cmp);
  for (int i = 0; i < n; ++i) {
    if (a[i] != b[i]) {
      ++ans;
      f[F(a[i].first)] = F(b[i].first);
    }
  }
  assert(F(a[n].first) == F(b[n].first));
  for (int i = 0; i < n; ++i) {
    if (a[i] != b[i] && F(a[i].first) != F(a[n].first) &&
        F(a[i].first) != F(b[n].first)) {
      ans += g[F(a[i].first)];
      g[F(a[i].first)] = 0;
    }
  }
  return !printf("%d\n", ans);
}
