#include <bits/stdc++.h>
using namespace std;
struct UF {
  int par[100005], rank[100005];
  void init(int n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 1;
    }
  }
  int find(int x) {
    if (x == par[x]) return x;
    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 uf;
int A[100005], B[100005];
bool use[100005];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &A[i]);
  A[n] = 0;
  for (int i = 0; i < n; i++) A[n] ^= A[i];
  for (int i = 0; i < n; i++) scanf("%d", &B[i]);
  B[n] = 0;
  for (int i = 0; i < n; i++) B[n] ^= B[i];
  n++;
  vector<int> va, vb;
  for (int i = 0; i < n; i++) {
    va.push_back(A[i]);
    vb.push_back(B[i]);
  }
  sort(va.begin(), va.end());
  sort(vb.begin(), vb.end());
  for (int i = 0; i < n; i++) {
    if (va[i] != vb[i]) {
      puts("-1");
      return 0;
    }
  }
  va.erase(unique(va.begin(), va.end()), va.end());
  uf.init(n + 2);
  int ret = 0;
  int nm = 0;
  for (int i = 0; i < n; i++) {
    A[i] = lower_bound(va.begin(), va.end(), A[i]) - va.begin();
    B[i] = lower_bound(va.begin(), va.end(), B[i]) - va.begin();
    if (A[i] != B[i]) {
      uf.unite(A[i], B[i]);
      use[A[i]] = use[B[i]] = true;
      ret++;
    }
  }
  int sz = 0;
  for (int i = 0; i < va.size(); i++)
    if (use[i] && uf.find(i) == i) sz++;
  ret += sz;
  if (A[n - 1] != B[n - 1])
    ret -= 2;
  else if (sz > 1)
    ret--;
  printf("%d\n", ret);
  return 0;
}
