#include <bits/stdc++.h>
using namespace std;
int read() {
  int nm = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
  return nm * f;
}
long long a[200010], b[200010], n, ans, tm, now, tp, an[200010], bn[200010],
    sta[200010], tot;
multiset<long long> st;
multiset<long long>::iterator it;
long long bude = 0;
map<long long, int> mp;
long long tmp;
int father[200010];
int find(int x) { return father[x] == x ? x : father[x] = find(father[x]); }
int main() {
  n = read();
  for (int i = 1; i <= n; i++) a[i] = read(), now ^= a[i];
  for (int i = 1; i <= n; i++) b[i] = read(), st.insert(b[i]);
  for (int i = 1; i <= n; i++) {
    it = st.find(a[i]);
    if (it == st.end())
      tm++, bude = a[i];
    else
      st.erase(it);
    if (a[i] == b[i]) continue;
    ans++;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] != b[i]) break;
    if (i == n) {
      cout << "0\n";
      return 0;
    }
  }
  if (st.find(now) == st.end())
    tm++, bude = now;
  else
    st.erase(st.find(now));
  if (tm >= 2 || st.size()) {
    cout << "-1"
         << "\n";
    return 0;
  }
  tp = 0, tot = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == b[i]) continue;
    sta[++tot] = a[i];
    an[++tp] = a[i];
    bn[tp] = b[i];
  }
  sta[0] = -1;
  sort(sta + 1, sta + tot + 1);
  int cnt = 0;
  for (int i = 1; i <= tot; i++) {
    if (sta[i] == sta[i - 1]) continue;
    mp[sta[i]] = ++cnt;
  }
  for (int i = 1; i <= cnt + 1; i++) father[i] = i;
  for (int i = 1; i <= tp; i++) {
    int vi = mp[an[i]], vj = mp[bn[i]];
    vi = find(vi), vj = find(vj);
    if (vi == vj) continue;
    father[vi] = vj;
  }
  ans--;
  for (int i = 1; i <= n; i++) {
    if (a[i] == b[i]) continue;
    if (b[i] == now) break;
    if (i == n) {
      ans++;
    }
  }
  for (int i = 1; i <= cnt + 1; i++)
    if (father[i] == i) ans++;
  cout << ans - 1 << "\n";
  return 0;
}
