#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1e6;
set<pair<long long, long long> > st;
set<pair<long long, long long> >::iterator it;
map<long long, long long> maps;
long long N, CurXor, res, lol, A[MAX], B[MAX], nxt[MAX], pre[MAX], mark[MAX],
    deg[MAX];
signed main() {
  ios_base::sync_with_stdio(false);
  cin >> N;
  for (long long C = 1; C <= N; C++) {
    cin >> A[C];
    CurXor ^= A[C];
  }
  for (long long C = 1; C <= N; C++) {
    cin >> B[C];
    maps[B[C]]++;
  }
  for (long long C = 1; C <= N; C++) st.insert(make_pair(B[C], C));
  for (long long C = 1; C <= N; C++) {
    it = st.lower_bound(make_pair(A[C], 0));
    if (it == st.end() or it->first != A[C]) continue;
    nxt[C] = it->second;
    pre[it->second] = C;
  }
  long long ok, cnt, cur;
  for (long long C = 1; C <= N; C++) {
    if (mark[C] == 1) continue;
    ok = 0;
    cnt = 0;
    cur = C;
    while (cur and !mark[cur]) {
      if (B[cur] == CurXor) ok = 1;
      cnt++;
      mark[cur] = 1;
      deg[nxt[cur]]++;
      cur = nxt[cur];
    }
    if (cnt == 1) {
      if (cur == 0 and B[cur] != CurXor)
        return cout << -1, 0;
      else if (cur == 0)
        res++;
      continue;
    }
    res += cnt + 1 - ok;
    if (cur == 0) {
      cur = C;
      while (pre[cur]) cur = pre[cur];
      if (B[cur] != CurXor) return cout << -1, 0;
    }
  }
  for (long long A = 1; A <= N; A++) {
    if (!deg[A]) lol++;
  }
  if (lol > 1) return cout << -1, 0;
  cout << res;
  return 0;
}
