#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 1LL << 30;
const ll LINF = 1LL << 62;
const double eps = 1e-5;
const ll MOD = 1000000007LL;
template <typename T>
void chmin(T &a, T b) {
  a = min(a, b);
};
template <typename T>
void chmax(T &a, T b) {
  a = max(a, b);
};
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
struct UnionFind {
  vector<int> par, siz;
  UnionFind(int n) {
    par.resize(n);
    iota(par.begin(), par.end(), 0);
    siz.resize(n, 1);
  }
  int find(int x) {
    if (par[x] == x) return x;
    return par[x] = find(par[x]);
  }
  void unite(int u, int v) {
    u = find(u);
    v = find(v);
    if (u == v) return;
    if (siz[u] < siz[v]) swap(u, v);
    par[v] = u;
    siz[u] += siz[v];
  }
};
int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  map<int, int> m1, m2;
  int x = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m1[a[i]]++;
    x ^= a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    m2[b[i]]++;
  }
  m1[x]++;
  bool f = false;
  for (auto itr = m2.begin(); itr != m2.end(); itr++) {
    if (m1[itr->first] < itr->second) f = true;
  }
  if (f) {
    cout << -1 << endl;
    return 0;
  }
  bool ff = (m1[x] == m2[x]);
  vector<int> v;
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) v.push_back(i);
  }
  if (v.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  map<int, int> make_pair;
  for (int i = 0; i < v.size(); i++) make_pair[v[i]] = i;
  UnionFind uf(v.size());
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      uf.unite(make_pair[a[i]], make_pair[b[i]]);
      if (b[i] != x) ans++;
    }
  }
  set<int> st;
  for (int i = 0; i < v.size(); i++) st.insert(uf.find(i));
  ans += st.size();
  cout << ans << endl;
}
