#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
inline bool smin(T &a, const T &b) {
  return a > b ? a = b : a;
}
template <typename T>
inline bool smax(T &a, const T &b) {
  return a < b ? a = b : a;
}
const int N = (int)1e6 + 6, mod = (int)0;
int a[N], b[N], c[N], d[N];
map<int, int> par;
int find(int first) {
  if (par.find(first) == par.end()) return par[first] = first;
  if (par[first] == first) return first;
  return par[first] = find(par[first]);
}
void unite(int u, int v) {
  u = find(u);
  v = find(v);
  if (u == v) return;
  par[u] = v;
}
int main() {
  int n;
  cin >> n;
  int s0 = 0;
  for (int j = 0; j < n; ++j) cin >> a[j], s0 ^= a[j], c[j] = a[j];
  c[n] = s0;
  int s1 = 0;
  for (int j = 0; j < n; ++j) cin >> b[j], s1 ^= b[j], d[j] = b[j];
  d[n] = s1;
  ++n;
  sort(d, d + n);
  sort(c, c + n);
  for (int j = 0; j < n; ++j)
    if (c[j] != d[j]) {
      cout << -1 << endl;
      return 0;
    }
  --n;
  set<int> all;
  unite(s0, s1);
  int res = 0;
  for (int j = 0; j < n; ++j) {
    res += (a[j] != b[j]);
    unite(a[j], b[j]);
  }
  for (int j = 0; j < n; ++j)
    if (a[j] != b[j]) all.insert(find(a[j]));
  if (s0 != s1) all.insert(find(s0));
  cout << max(0, res + (int)all.size() - 1) << endl;
}
