#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 1000000001;
const long long LINF = 1000000000000000001LL;
struct FAU {
  vector<int> p, r;
  FAU(int n) : p(n, -1), r(n, 0) {}
  int find(int x) {
    if (p[x] == -1) return x;
    return p[x] = find(p[x]);
  }
  void join(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (r[x] > r[y])
      p[y] = x;
    else
      p[x] = y;
    if (r[x] == r[y]) ++r[y];
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = (0); i <= (n - 1); ++i) cin >> a[i];
  for (int i = (0); i <= (n - 1); ++i) cin >> b[i];
  int xr = 0;
  for (int x : a) xr ^= x;
  multiset<int> s = {xr};
  for (int x : a) s.insert(x);
  for (int x : b) {
    if (!s.count(x)) {
      cout << -1;
      return 0;
    }
    s.erase(s.find(x));
  }
  int rest = *s.begin();
  int ans = n;
  vector<pair<int, int> > pairs;
  for (int i = (0); i <= (n - 1); ++i) {
    if (a[i] == b[i]) {
      ans--;
    } else {
      pairs.push_back({a[i], b[i]});
    }
  }
  vector<int> values;
  for (auto &e : pairs) {
    values.push_back(e.first);
    values.push_back(e.second);
  }
  sort(values.begin(), values.end());
  values.resize(unique(values.begin(), values.end()) - values.begin());
  FAU fau((int)(values).size());
  for (auto &e : pairs) {
    int a = e.first, b = e.second;
    int aid = lower_bound(values.begin(), values.end(), a) - values.begin();
    int bid = lower_bound(values.begin(), values.end(), b) - values.begin();
    fau.join(aid, bid);
  }
  int comps = 0;
  for (int i = (0); i <= ((int)(values).size() - 1); ++i)
    comps += (fau.find(i) == i);
  ans += comps;
  if (find(b.begin(), b.end(), xr) != b.end()) ans--;
  cout << ans;
  return 0;
}
