#include <bits/stdc++.h>
using namespace std;
int NOEL() {
  cout << -1 << endl;
  return 0;
}
long long n, a[100010], b[100010];
unordered_map<long long, long long> A, B;
bool Cand() {
  a[n] = 0;
  for (long long i = 0; i < n; i++) a[n] = a[n] xor a[i];
  for (long long i = 0; i < n + 1; i++) A[a[i]]++;
  for (long long i = 0; i < n; i++) B[b[i]]++;
  b[n] = -1;
  for (long long i = 0; i < n + 1; i++) {
    if (A[a[i]] != B[a[i]]) {
      if (b[n] != -1) return 0;
      b[n] = a[i];
      B[a[i]]++;
    }
  }
  return 1;
}
unordered_map<long long, vector<long long> > g;
unordered_set<long long> vis;
unordered_set<long long> cur;
void dfs(long long x) {
  if (cur.find(x) != cur.end()) return;
  vis.insert(x);
  cur.insert(x);
  for (auto y : g[x]) dfs(y);
}
int main() {
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < n; i++) cin >> b[i];
  if (!Cand()) return NOEL();
  for (long long i = 0; i < n + 1; i++) {
    if (a[i] != b[i]) g[a[i]].push_back(b[i]);
  }
  long long ans = -2 + (a[n] == b[n]);
  for (long long i = 0; i < n + 1; i++) {
    if (vis.find(a[i]) != vis.end()) continue;
    cur.clear();
    dfs(a[i]);
    if (cur.size() == 1 && i == n) ans++;
    if (cur.size() != 1) ans++;
  }
  for (auto x : vis) {
    ans += g[x].size();
  }
  cout << ans << endl;
  return 0;
}
