#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n + 1), b(n + 1);
  for (int i = 0; i < n; i++) cin >> a[i], a[n] ^= a[i];
  for (int i = 0; i < n; i++) cin >> b[i], b[n] ^= b[i];
  vector<int> pa(n + 1), pb(n + 1);
  for (int i = 0; i < n + 1; i++) {
    pa[i] = i;
    pb[i] = i;
  }
  sort(pa.begin(), pa.end(), [&](int i, int j) { return a[i] < a[j]; });
  sort(pb.begin(), pb.end(), [&](int i, int j) { return b[i] < b[j]; });
  bool ok = true;
  for (int i = 0; i < n + 1; i++) {
    if (a[pa[i]] != b[pb[i]]) ok = false;
  }
  if (!ok)
    cout << "-1\n";
  else {
    vector<int> p(n + 1);
    for (int i = 0; i < n + 1; i++) p[pa[i]] = pb[i];
    vector<bool> vis(n + 1, false);
    int ret = 0;
    for (int i = 0; i < n + 1; i++) {
      if (vis[i]) continue;
      int pos = i, cnt = 0, key = 1;
      while (!vis[pos]) {
        cnt++;
        vis[pos] = true;
        if (pos == n)
          key = -1;
        else if (a[pos] == a[n])
          key = 0;
        pos = p[pos];
      }
      if (cnt >= 2) ret += cnt + key;
    }
    cout << ret << "\n";
  }
  return 0;
}
