#include <bits/stdc++.h>
using namespace std;
void _dbg(string) { cout << endl; }
template <class H, class... T>
void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream& operator<<(ostream& o, const pair<T, U>& p) {
  o << "(" << p.first << "," << p.second << ")";
  return o;
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}
class UnionFind {
 public:
  vector<int> par, rank;
  UnionFind(int sz) : par(sz, -1), rank(sz, 0) {}
  int find(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y]) {
      par[y] += par[x];
      par[x] = y;
    } else {
      par[x] += par[y];
      par[y] = x;
      if (rank[x] == rank[y]) rank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -par[find(x)]; }
};
int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1, 0), b(n + 1, 0);
  for (int i = (int)(0); i < (int)(n); i++) cin >> a[i];
  for (int i = (int)(0); i < (int)(n); i++) cin >> b[i];
  for (int i = (int)(0); i < (int)(n); i++) a[n] ^= a[i];
  for (int i = (int)(0); i < (int)(n); i++) b[n] ^= b[i];
  vector<int> sa(a), sb(b);
  sort((sa).begin(), (sa).end());
  sort((sb).begin(), (sb).end());
  if (sa != sb) {
    cout << -1 << endl;
    return 0;
  }
  sa.erase(unique((sa).begin(), (sa).end()), sa.end());
  UnionFind uf(sa.size());
  int cnt = 0;
  for (int i = (int)(0); i < (int)(n); i++) {
    if (a[i] != b[i]) {
      cnt++;
      int x = lower_bound((sa).begin(), (sa).end(), a[i]) - sa.begin();
      int y = lower_bound((sa).begin(), (sa).end(), b[i]) - sa.begin();
      uf.unite(x, y);
    }
  }
  if (cnt == 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> val(sa.size(), 0);
  for (int i = (int)(0); i < (int)(n); i++)
    if (a[i] != b[i]) {
      int x = lower_bound((sa).begin(), (sa).end(), a[i]) - sa.begin();
      val[uf.find(x)]++;
    }
  {
    int sz = val[lower_bound((sa).begin(), (sa).end(), a[n]) - sa.begin()];
    if (sz <= 2)
      cout << cnt + 1 << endl;
    else
      cout << cnt << endl;
  }
  return 0;
}
