#include <bits/stdc++.h>
using namespace std;
inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
inline void priv(vector<int> a) {
  for (int i = 0; i < ((int)(a).size()); ++i)
    printf("%d%c", a[i], i == (int)(a).size() - 1 ? '\n' : ' ');
}
template <typename T>
istream& operator>>(istream& i, vector<T>& v) {
  for (int j = 0; j < ((int)(v).size()); ++j) i >> v[j];
  return i;
}
template <typename T>
string join(const vector<T>& v) {
  stringstream s;
  for (int i = 0; i < ((int)(v).size()); ++i) s << ' ' << v[i];
  return s.str().substr(1);
}
template <typename T>
ostream& operator<<(ostream& o, const vector<T>& v) {
  if ((int)(v).size()) o << join(v);
  return o;
}
template <typename T1, typename T2>
istream& operator>>(istream& i, pair<T1, T2>& v) {
  return i >> v.first >> v.second;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& o, const pair<T1, T2>& v) {
  return o << v.first << "," << v.second;
}
const int MX = 100005, INF = 1001001001;
const long long int LINF = 1e18;
const double eps = 1e-10;
struct X {
  vector<int> d;
  void add(int x) { d.push_back(x); }
  void init() {
    sort(d.begin(), d.end());
    d.erase(unique(d.begin(), d.end()), d.end());
  }
  int size() const { return (int)(d).size(); }
  int operator[](int i) const { return d[i]; }
  int operator()(int x) const {
    return upper_bound(d.begin(), d.end(), x) - d.begin() - 1;
  }
};
vector<vector<int> > to, ot;
vector<int> used;
void dfs(int v) {
  if (used[v]) return;
  used[v] = 1;
  for (int u : to[v]) {
    dfs(u);
  }
  for (int u : ot[v]) {
    dfs(u);
  }
}
int main() {
  int n;
  scanf("%d", &n);
  vector<int> a(n), b(n);
  cin >> a >> b;
  if (a == b) {
    puts("0");
    return 0;
  }
  int x = 0;
  for (int i = 0; i < (n); ++i) x ^= a[i];
  X xs;
  for (int i = 0; i < (n); ++i) xs.add(a[i]);
  for (int i = 0; i < (n); ++i) xs.add(b[i]);
  xs.add(x);
  xs.init();
  int m = (int)(xs).size();
  to = ot = vector<vector<int> >(m);
  int ans = 0;
  for (int i = 0; i < (n); ++i) {
    if (a[i] == b[i]) continue;
    ++ans;
    to[xs(b[i])].push_back(xs(a[i]));
    ot[xs(a[i])].push_back(xs(b[i]));
  }
  int cnt = 0;
  for (int i = 0; i < (m); ++i) {
    if ((int)(to).size() < (int)(ot).size()) cnt++;
    if ((int)(to).size() > (int)(ot).size() && xs[i] != x) cnt = INF;
    if (abs((int)(to).size() - (int)(ot).size()) > 1) cnt = INF;
  }
  if (cnt > 1) {
    puts("-1");
    return 0;
  };
  int com = 1;
  used = vector<int>(m);
  dfs(xs(x));
  for (int i = 0; i < (m); ++i) {
    if (used[i]) continue;
    if ((int)(to[i]).size() == 0) continue;
    com++;
    dfs(i);
  }
  ans += com - 1;
  cout << ans << endl;
  return 0;
}
