#include <bits/stdc++.h>
using namespace std;
class UnionFind {
 private:
  int sz;
  vector<int> par, size_;

 public:
  UnionFind() {}
  UnionFind(int node_size) : sz(node_size), par(sz), size_(sz, 1) {
    iota(par.begin(), par.end(), 0);
  }
  int find(int x) {
    if (par[x] == x)
      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 (size_[x] < size_[y]) swap(x, y);
    par[y] = x;
    size_[x] += size_[y];
  }
  int size(int x) {
    x = find(x);
    return size_[x];
  }
  bool same(int x, int y) { return find(x) == find(y); }
};
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (int)(n); ++i) cin >> a[i];
  vector<int> b(n);
  for (int i = 0; i < (int)(n); ++i) cin >> b[i];
  int sa = 0;
  int sb = 0;
  for (int i = 0; i < (int)(n); ++i) {
    sa ^= a[i];
    sb ^= b[i];
  }
  a.push_back(sa);
  b.push_back(sb);
  n++;
  map<int, int> mpa, mpb;
  map<int, int> num;
  int aaa = 0;
  for (int i = 0; i < (int)(n); ++i) {
    if (a[i] != b[i] && num.find(a[i]) == num.end()) {
      num[a[i]] = aaa;
      aaa++;
    }
    mpa[a[i]]++;
    mpb[b[i]]++;
  }
  for (auto x : mpa) {
    if (x.second != mpb[x.first]) {
      cout << -1 << endl;
      return 0;
    }
  }
  UnionFind uf(aaa);
  int c = 0;
  map<int, multiset<int> > mp;
  for (int i = 0; i < (int)(n); ++i) {
    if (a[i] != b[i] || i == n - 1) {
      if (i == n - 1) c++;
      uf.unite(num[a[i]], num[b[i]]);
      mp[b[i]].insert(a[i]);
    }
  }
  set<int> st;
  for (int i = 0; i < (int)(aaa); ++i) {
    st.insert(uf.find(i));
  }
  int k = st.size();
  if (c == 0) {
    cout << 0 << endl;
  } else {
    int p = sa;
    int res = 0;
    cout << c + k - 1 << endl;
  }
  return 0;
}
