#include <bits/stdc++.h>
using namespace std;
const int MN = 100010;
class unionfind {
  vector<int> par, rank;

 public:
  void init(int n) {
    par.resize(n);
    rank.resize(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 0;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y])
      par[x] = y;
    else {
      par[y] = x;
      if (rank[x] == rank[y]) ++rank[x];
    }
  }
  bool same(int x, int y) { return (find(x) == find(y)); }
} uf;
int N;
int a[MN], b[MN];
int main() {
  cin >> N;
  vector<int> aa(N + 1), bb(N + 1);
  int xa = 0, xb = 0;
  map<int, int> ID;
  for (int i = 0; i < (N); i++) {
    cin >> a[i];
    aa[i] = a[i];
    xa ^= a[i];
  }
  a[N] = xa;
  for (int i = 0; i < (N); i++) {
    cin >> b[i];
    bb[i] = b[i];
    xb ^= b[i];
  }
  b[N] = xb;
  aa[N] = xa, bb[N] = xb;
  sort((aa).begin(), (aa).end());
  sort((bb).begin(), (bb).end());
  for (int i = 0; i < (aa.size()); i++) {
    if (aa[i] != bb[i]) {
      puts("-1");
      return 0;
    }
  }
  bool diff = (a[N] != b[N]);
  int edge = 0;
  vector<pair<int, int> > vec;
  int now = 0;
  N++;
  for (int i = 0; i < (N); i++) {
    if (a[i] != b[i]) {
      if (!ID.count(a[i])) {
        ID[a[i]] = now++;
      }
      if (!ID.count(b[i])) {
        ID[b[i]] = now++;
      }
      vec.emplace_back(ID[a[i]], ID[b[i]]);
      ++edge;
    }
  }
  if (now == 0) {
    puts("0");
    return 0;
  }
  uf.init(now);
  for (pair<int, int> e : vec) uf.unite(e.first, e.second);
  set<int> st;
  for (int i = 0; i < (now); i++) {
    st.insert(uf.find(i));
  }
  cout << (int)st.size() + edge - 1 - diff << endl;
  return 0;
}
