#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    if (1) {
      ios::sync_with_stdio(false);
      cin.tie(0);
    }
  }
} star;
template <typename T>
inline bool chmin(T &l, T r) {
  bool a = l > r;
  if (a) l = r;
  return a;
}
template <typename T>
inline bool chmax(T &l, T r) {
  bool a = l < r;
  if (a) l = r;
  return a;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &it : v) is >> it;
  return is;
}
namespace _DSU {
int cnt = 0;
int mem[2][212345];
int *get() { return mem[cnt++]; }
}  // namespace _DSU
class UF {
 public:
  int *par, *rank;
  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }
  UF(int n, int *par, int *rank) : par(par), rank(rank) {
    for (int i = (0); i < (n); i++) par[i] = i, rank[i] = 0;
  }
  UF(int n) : UF(n, _DSU::get(), _DSU::get()) {}
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return false;
    if (rank[x] < rank[y])
      par[x] = y;
    else {
      par[y] = x;
      if (rank[x] == rank[y]) rank[x]++;
    }
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
};
int main() {
  int N;
  cin >> N;
  vector<int> a(N), b(N);
  cin >> a >> b;
  int x = 0;
  for (int i = (0); i < (N); i++) x ^= a[i];
  map<int, int> cnt;
  for (int i = (0); i < (N); i++) cnt[a[i]]++;
  cnt[x]++;
  for (int i = (0); i < (N); i++) cnt[b[i]]--;
  for (auto &it : cnt) {
    if (it.second < 0) {
      cout << -1 << endl;
      return 0;
    }
    if (it.second > 0) b.push_back(it.first);
  }
  a.push_back(x);
  reverse((a).begin(), (a).end());
  reverse((b).begin(), (b).end());
  x = 0;
  for (auto &it : cnt) it.second = x++;
  for (auto &it : a) it = cnt[it];
  for (auto &it : b) it = cnt[it];
  UF uf(x);
  for (int i = (0); i < (N + 1); i++)
    if (a[i] != b[i]) uf.unite(a[i], b[i]);
  vector<int> CC(x, 0);
  for (int i = (0); i < (N + 1); i++)
    if (a[i] != b[i]) CC[uf.find(a[i])]++;
  if (a[0] != b[0]) {
    int res = -2;
    for (int i = (0); i < (x); i++)
      if (CC[i]) res += CC[i] + 1;
    cout << res << endl;
  } else {
    int res = 0;
    for (int i = (1); i < (N + 1); i++)
      if (a[i] != b[i] && a[0] == b[i]) res = -2;
    for (int i = (0); i < (x); i++)
      if (CC[i]) res += CC[i] + 1;
    cout << res << endl;
  }
  return 0;
}
