#include <bits/stdc++.h>
using namespace std;
int par[200002];
int rk[200002];
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
  }
}
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 (rk[x] < rk[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rk[x] == rk[y]) rk[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
int main() {
  int n;
  cin >> n;
  int a[100002], b[100002], as[100002], bs[100002], s = 0;
  set<pair<int, int> > st;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    as[i] = a[i];
    s ^= a[i];
  }
  as[n] = a[n] = s;
  sort(as, as + n + 1);
  s = 0;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    bs[i] = b[i];
    s ^= b[i];
    if (a[i] != b[i]) {
      st.insert(pair<int, int>(b[i], i));
    }
  }
  bs[n] = b[n] = s;
  sort(bs, bs + n + 1);
  for (int i = 0; i <= n; i++) {
    if (as[i] != bs[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  if (st.empty()) {
    cout << 0 << endl;
    return 0;
  }
  int ct = 0;
  pair<int, int> nuo = pair<int, int>(-1, -1);
  while (!st.empty()) {
    auto itr = st.lower_bound(pair<int, int>(a[n], -1));
    if (itr == st.end() || (*itr).first != a[n]) {
      break;
    }
    st.erase(itr);
    pair<int, int> p = *itr;
    int j = p.second;
    swap(a[j], a[n]);
    ct++;
  }
  if (st.empty()) {
    cout << ct << endl;
    return 0;
  }
  map<int, int> mp;
  bool ok[100001] = {};
  for (auto p : st) {
    mp[p.first] = 0;
    ok[p.second] = 1;
  }
  int c = n;
  for (auto& p : mp) {
    p.second = c;
    c++;
  }
  init(c);
  for (int i = 0; i < n; i++) {
    if (ok[i]) {
      unite(mp[a[i]], i);
      unite(mp[b[i]], i);
    }
  }
  set<int> st0;
  for (int i = 0; i < n; i++) {
    if (ok[i]) {
      st0.insert(find(i));
    }
  }
  cout << ct + (int)st.size() + (int)st0.size() << endl;
  return 0;
}
