#include <bits/stdc++.h>
using namespace std;
const int MX_N = 1e5 + 5;
int N;
int A[MX_N], B[MX_N];
map<int, queue<int> > f;
map<pair<int, int>, vector<int> > g;
struct UnionFind {
  vector<int> sz, p;
  UnionFind(int n) {
    sz.assign(n, 1);
    p.resize(n);
    iota(begin(p), end(p), 0);
  }
  int findset(int i) { return (p[i] == i) ? i : (p[i] = findset(p[i])); }
  bool sameset(int i, int j) { return findset(i) == findset(j); }
  bool unionset(int i, int j) {
    int x = findset(i), y = findset(j);
    if (x != y) {
      if (sz[x] > sz[y])
        p[y] = x, sz[x] += sz[y];
      else
        p[x] = y, sz[y] += sz[x];
      return true;
    }
    return false;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  int X = 0;
  int ans = 0;
  bool p = 0;
  for (int i = 1; i <= N; ++i) {
    cin >> A[i];
    X ^= A[i];
  }
  for (int i = 1; i <= N; ++i) {
    cin >> B[i];
  }
  for (int i = 1; i <= N; ++i)
    if (A[i] != B[i]) {
      f[A[i]].push(i);
      g[pair<int, int>(A[i], B[i])].push_back(i);
      p |= (A[i] == X);
    }
  f[X].push(0);
  if (p) ans -= 1;
  UnionFind UF(N + 1);
  for (int i = 1; i <= N; ++i)
    if (A[i] != B[i]) {
      if (f[B[i]].empty()) {
        cout << -1;
        return 0;
      } else {
        int j = f[B[i]].front();
        UF.unionset(i, j);
        ++ans;
        f[B[i]].pop();
      }
    }
  for (auto& e : g)
    if (e.first.first < e.first.second) {
      int i = e.second[0];
      if (g.count(pair<int, int>(e.first.second, e.first.first))) {
        for (auto& j : e.second) UF.unionset(i, j);
        for (auto& j : g[pair<int, int>(e.first.second, e.first.first)])
          UF.unionset(i, j);
      }
    }
  for (int i = 1; i <= N; ++i) ans += (UF.p[i] == i && UF.sz[i] > 1);
  cout << ans;
}
