#include <bits/stdc++.h>
using namespace std;
const int MX_N = 1e5 + 5;
int N;
int A[MX_N], B[MX_N];
map<int, int> sa;
vector<int> ord;
struct UnionFind {
  vector<int> s, p;
  UnionFind(int n) {
    s.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 unionset(int i, int j) {
    int x = findset(i), y = findset(j);
    if (x != y) {
      if (s[x] > s[y])
        p[y] = x, s[x] += s[y];
      else
        p[x] = y, s[y] += s[x];
      return true;
    } else
      return false;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    cin >> A[i];
    A[0] ^= A[i];
    ++sa[A[i]];
  }
  ++sa[A[0]];
  for (int i = 1; i <= N; ++i) {
    cin >> B[i];
  }
  for (int i = 0; i <= N; ++i) ord.push_back(A[i]);
  sort(begin(ord), end(ord));
  ord.resize(unique(begin(ord), end(ord)) - ord.begin());
  int ans = 0;
  UnionFind UF((int)(ord).size());
  for (int i = 1; i <= N; ++i)
    if (A[i] != B[i]) {
      if (!sa[B[i]]) {
        cout << -1;
        return 0;
      }
      --sa[B[i]];
      ++ans;
      int x = lower_bound(begin(ord), end(ord), A[i]) - ord.begin();
      int y = lower_bound(begin(ord), end(ord), B[i]) - ord.begin();
      UF.unionset(x, y);
    }
  set<int> comp;
  for (int i = 0; i <= (int)(ord).size() - 1; ++i) {
    if (UF.p[i] == i && UF.s[i] > 1) comp.insert(i);
  }
  for (int i = 0; i <= N; ++i)
    if (A[i] == A[0]) {
      int x = UF.findset(lower_bound(begin(ord), end(ord), A[i]) - ord.begin());
      if (comp.count(x)) comp.erase(x);
    }
  cout << ans + (int)(comp).size();
}
