#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const long long mod = 1e9 + 7;
int n, s[N], t[N], ans;
int s2[N], t2[N];
map<int, int> cnt1, cnt2, par;
int first(int x) {
  if (par[x] == x) return x;
  return par[x] = first(par[x]);
}
void un(int x, int y) {
  x = first(x);
  y = first(y);
  if (x != y) par[y] = x;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> s[i];
  for (int i = 1; i <= n; i++) cin >> t[i];
  for (int i = 1; i <= n; i++) {
    s[n + 1] ^= s[i];
    t[n + 1] ^= t[i];
  }
  for (int i = 1; i <= n + 1; i++) cnt1[s[i]]++;
  for (int i = 1; i <= n + 1; i++) cnt2[t[i]]++;
  for (int i = 1; i <= n; i++)
    if (cnt2[t[i]] != cnt1[t[i]]) {
      cout << -1;
      return 0;
    }
  bool ok = true;
  for (int i = 1; i <= n + 1; i++)
    if (s[i] != t[i]) ok = false;
  if (ok) {
    cout << 0;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    if (s[i] != t[i]) {
      ans++;
      if (!par[s[i]]) par[s[i]] = s[i];
      if (!par[t[i]]) par[t[i]] = t[i];
      un(s[i], t[i]);
    }
  }
  for (auto &it : par)
    if (first(it.first) == it.first) ans++;
  cout << ans - 1;
  return 0;
}
