#include <bits/stdc++.h>
using namespace std;
template <class T>
using vv = vector<vector<T>>;
template <class T>
ostream &operator<<(ostream &os, const vector<T> &t) {
  os << "{";
  for (int(i) = 0; (i) < (t.size()); ++(i)) {
    os << t[i] << ",";
  }
  os << "}" << endl;
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &t) {
  return os << "(" << t.first << "," << t.second << ")";
}
template <class T>
inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T>
inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}
vector<string> s_p_l_i_t(const string &s, char c) {
  vector<string> v;
  int d = 0, f = 0;
  string t;
  for (char c : s) {
    if (!d && c == ',')
      v.push_back(t), t = "";
    else
      t += c;
    if (c == '\"' || c == '\'') f ^= 1;
    if (!f && c == '(') ++d;
    if (!f && c == ')') --d;
  }
  v.push_back(t);
  return move(v);
}
void e_r_r(vector<string>::iterator it) {}
template <typename T, typename... Args>
void e_r_r(vector<string>::iterator it, T a, Args... args) {
  if (*it == " 1" || *it == "1")
    cerr << endl;
  else
    cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << ", ";
  e_r_r(++it, args...);
}
const long long MOD = 1e9 + 7;
struct UF {
  vector<int> data;
  UF(int size) : data(size, -1) {}
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};
int main() {
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(0);
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int(i) = 0; (i) < (n); ++(i)) cin >> a[i];
  for (int(i) = 0; (i) < (n); ++(i)) cin >> b[i];
  if (a == b) {
    cout << 0 << endl;
    return 0;
  }
  int s = 0, t = 0;
  for (int x : a) s ^= x;
  for (int x : b) t ^= x;
  vector<int> x, y;
  for (int(i) = 0; (i) < (n); ++(i))
    if (a[i] != b[i]) {
      x.push_back(a[i]);
      y.push_back(b[i]);
    }
  x.push_back(s);
  y.push_back(t);
  if (x.size() != y.size()) {
    cout << -1 << endl;
    return 0;
  }
  auto zip = x;
  zip.insert(zip.end(), (y).begin(), (y).end());
  sort((zip).begin(), (zip).end());
  (zip).erase(unique((zip).begin(), (zip).end()), (zip).end());
  int m = zip.size();
  UF uf(x.size() + m);
  for (int(i) = 0; (i) < (x.size()); ++(i)) {
    uf.unite(lower_bound((zip).begin(), (zip).end(), x[i]) - zip.begin(),
             m + i);
    uf.unite(lower_bound((zip).begin(), (zip).end(), y[i]) - zip.begin(),
             m + i);
  }
  sort((x).begin(), (x).end());
  sort((y).begin(), (y).end());
  if (x == y) {
    cout << x.size() - (uf.size(0) == m + x.size()) << endl;
    return 0;
  }
  cout << -1 << endl;
  return 0;
}
