#include <bits/stdc++.h>
using namespace std;
long long n;
long long a[(long long)(1e6 + 3)], b[(long long)(1e6 + 3)];
long long c[(long long)(1e6 + 3)], d[(long long)(1e6 + 3)];
long long seen[(long long)(1e6 + 3)];
map<long long, vector<long long> > ma, mb;
long long cur, cnt;
long long par;
vector<long long> tmp;
void fail() {
  cout << -1 << endl;
  exit(0);
}
void dfs(long long u) {
  if (seen[u]) {
    if (u == n) fail();
    return;
  }
  seen[u] = true;
  cur++;
  tmp.push_back(a[u]);
  long long nx = -1;
  if (ma[a[u]].size() == 0) {
    nx = n;
    par = u;
  } else {
    vector<long long> v = ma[a[u]];
    nx = v[v.size() - 1];
    ma[a[u]].pop_back();
  }
  dfs(nx);
}
void solve() {
  cin >> n;
  long long x = 0;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    x ^= a[i];
  }
  a[n] = x;
  for (long long i = 0; i < n; i++) {
    cin >> b[i];
    ma[b[i]].push_back(i);
  }
  vector<long long> v;
  for (long long i = 0; i < n; i++) {
    if (a[i] == b[i]) seen[i] = true;
  }
  for (long long i = 0; i < n; i++) {
    if (seen[i]) continue;
    cur = 0;
    tmp.clear();
    dfs(i);
    v.push_back(cur);
    for (long long j = 0; j < tmp.size(); j++) {
      mb[tmp[j]].push_back(i);
    }
  }
  if (!seen[n]) {
    long long sum = 0;
    for (long long i = 0; i < v.size(); i++) sum += (v[i] + 1);
    sum -= mb[a[n]].size();
    cout << sum << endl;
  } else {
    long long sum = 0;
    for (long long i = 0; i < v.size(); i++) sum += (v[i] + 1);
    set<long long> s;
    for (long long i = 0; i < mb[a[par]].size(); i++) s.insert(mb[a[par]][i]);
    for (long long i = 0; i < mb[a[n]].size(); i++) s.insert(mb[a[n]][i]);
    sum -= s.size();
    cout << sum - 1 << endl;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
}
