#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, fa[100005], a[100005], b[100005], ta[100005], tb[100005], sa,
    sb, ans, vis[100005];
vector<int> all;
int fnd(int x) {
  if (fa[x] == x) return x;
  return fa[x] = fnd(fa[x]);
}
int main() {
  cin >> n;
  for ((i) = 1; (i) <= (n + 1); (i)++) fa[i] = i;
  for ((i) = 1; (i) <= (n); (i)++) {
    cin >> a[i];
    ta[i] = a[i];
    sa ^= a[i];
  }
  for ((i) = 1; (i) <= (n); (i)++) {
    cin >> b[i];
    tb[i] = b[i];
    sb ^= b[i];
  }
  n++;
  a[n] = ta[n] = sa;
  b[n] = tb[n] = sb;
  sort(ta + 1, ta + n + 1);
  sort(tb + 1, tb + n + 1);
  for ((i) = 1; (i) <= (n); (i)++)
    if (ta[i] != tb[i]) break;
  if (i <= n) {
    puts("-1");
    return 0;
  }
  for ((i) = 1; (i) <= (n); (i)++)
    if (a[i] != b[i]) break;
  if (i > n) {
    puts("0");
    return 0;
  }
  for ((i) = 1; (i) <= (n); (i)++) all.push_back(a[i]);
  sort(all.begin(), all.end());
  all.resize(unique(all.begin(), all.end()) - all.begin());
  for ((i) = 1; (i) <= (n); (i)++) {
    a[i] = upper_bound(all.begin(), all.end(), a[i]) - all.begin();
    b[i] = upper_bound(all.begin(), all.end(), b[i]) - all.begin();
    if (a[i] != b[i]) {
      if (i < n) ans++;
      fa[fnd(a[i])] = fnd(b[i]);
      vis[a[i]] = vis[b[i]] = 1;
    }
  }
  for ((i) = 1; (i) <= (n); (i)++)
    if (vis[i] && fa[i] == i) ans++;
  cout << ans - 1 << endl;
  return 0;
}
