#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline void chmin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, T2 b) {
  if (a < b) a = b;
}
struct UnionFind {
 private:
  vector<long long> child, tree;
  vector<vector<long long>> list;

 public:
  UnionFind(long long v) {
    tree.resize(v);
    list.resize(v);
    for (long long i = (0); i < ((v)); ++i) tree[i] = i, list[i].push_back(i);
  }
  long long root(long long i) {
    if (i == tree[i]) {
      for (long long j = (0); j < ((child.size())); ++j) tree[child[j]] = i;
      child.clear();
      return i;
    } else {
      child.push_back(i);
      return i = root(tree[i]);
    }
  }
  long long size(long long i) { return list[root(i)].size(); }
  vector<long long> nodes(long long no) { return list[root(no)]; }
  bool unit(long long x, long long y) {
    x = root(x);
    y = root(y);
    if (x == y) return false;
    if (list[x].size() < list[y].size()) swap(x, y);
    for (long long no : list[y]) list[x].emplace_back(no);
    tree[y] = x;
    return true;
  }
  bool isUnit(long long x, long long y) { return root(x) == root(y); }
};
template <typename tuple<long long, long long, long long>>
vector<tuple<long long, long long, long long>> compress(
    vector<tuple<long long, long long, long long>> v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  return v;
}
template <typename tuple<long long, long long, long long>>
map<tuple<long long, long long, long long>, long long> dict(
    const vector<tuple<long long, long long, long long>> &v) {
  map<tuple<long long, long long, long long>, long long> res;
  for (long long i = 0; i < (long long)v.size(); i++) res[v[i]] = i;
  return res;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  long long x = 0;
  vector<long long> a(n);
  for (long long i = (0); i < ((n)); ++i) cin >> a[i], x ^= a[i];
  a.push_back(x);
  x = 0;
  vector<long long> b(n);
  for (long long i = (0); i < ((n)); ++i) cin >> b[i], x ^= b[i];
  b.push_back(x);
  vector<long long> c = a, d = b;
  sort(c.begin(), c.end());
  sort(d.begin(), d.end());
  if (c != d) {
    cout << -1 << endl;
    return 0;
  }
  c.clear();
  d.clear();
  vector<long long> e;
  for (long long i = (0); i < ((n + 1)); ++i)
    if (a[i] != b[i]) c.push_back(a[i]), d.push_back(b[i]);
  for (long long i = (0); i < ((c.size())); ++i)
    e.push_back(c[i]), e.push_back(d[i]);
  auto r = compress(e);
  auto v = dict(r);
  long long m = c.size();
  if (m == 0) {
    cout << 0 << endl;
    return 0;
  }
  UnionFind uf(v.size());
  for (long long i = (0); i < ((m)); ++i) uf.unit(v[c[i]], v[d[i]]);
  set<long long> st;
  for (long long i = (0); i < ((v.size())); ++i) st.insert(uf.root(i));
  if (a[n] != b[n]) m--;
  cout << m + st.size() - 1 << endl;
  return 0;
}
