#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
constexpr int inf = 1000000010;
constexpr long long INF = 1000000000000000010;
constexpr long double eps = 1e-12;
constexpr long double pi = 3.141592653589793238;
template <class T, class U>
inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U>
inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
class unionfind {
  vector<int> par;
  vector<int> sz;

 public:
  unionfind(int n) {
    par = vector<int>(n);
    for (int i = 0; i < n; i++) par[i] = i;
    sz = vector<int>(n, 1);
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }
  int si(int x) { return sz[find(x)]; }
  bool same(int x, int y) { return find(x) == find(y); }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (sz[x] < sz[y]) {
      par[x] = y;
      sz[y] += sz[x];
    } else {
      par[y] = x;
      sz[x] += sz[y];
    }
  }
};
vector<int> shrink(vector<int> vec) {
  int vecsize = vec.size();
  vector<int> tmpvec = vec;
  sort(tmpvec.begin(), tmpvec.end());
  tmpvec.erase(unique(tmpvec.begin(), tmpvec.end()), tmpvec.end());
  vector<int> res(vecsize);
  for (int i = 0; i < vecsize; i++)
    res[i] = lower_bound(tmpvec.begin(), tmpvec.end(), vec[i]) - tmpvec.begin();
  return res;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  int suma = 0, sumb = 0;
  for (long long i = 0; i < (n); i++) {
    cin >> a[i];
    suma ^= a[i];
  }
  for (long long i = 0; i < (n); i++) {
    cin >> b[i];
    sumb ^= b[i];
  }
  vector<int> c = a, d = b;
  c.push_back(suma);
  d.push_back(sumb);
  sort(c.begin(), c.end());
  sort(d.begin(), d.end());
  if (c != d) {
    cout << "-1\n";
    return 0;
  }
  a.push_back(suma);
  b.push_back(sumb);
  a = shrink(a);
  b = shrink(b);
  unionfind uni(100010);
  int ans = n;
  for (long long i = 0; i < (n); i++) {
    if (a[i] == b[i])
      ans--;
    else
      uni.unite(a[i], b[i]);
  }
  uni.unite(a[n], b[n]);
  set<int> st;
  for (long long i = 0; i < (n + 1); i++)
    if (a[i] != b[i]) st.insert(uni.find(a[i]));
  cout << max(0, ans - 1 + (int)st.size()) << '\n';
}
