#include <bits/stdc++.h>
using namespace std;
int N, a[100010], b[100010];
set<int> badp;
map<int, set<int>> mp;
int get_p(int x) {
  if (!mp.count(x)) return -1;
  set<int> &st = mp[x];
  if (st.empty()) return -1;
  return (*st.begin());
}
void rmv(int x, int p) {
  set<int> &st = mp[x];
  st.erase(p);
  badp.erase(p);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> N;
  int i, x = 0;
  for (i = 1; i <= N; i++) {
    cin >> a[i];
    x ^= a[i];
  }
  for (i = 1; i <= N; i++) {
    cin >> b[i];
    if (a[i] != b[i]) {
      badp.insert(i);
      mp[b[i]].insert(i);
    }
  }
  int res = 0;
  while (!badp.empty() && res <= 7 * N) {
    int p = get_p(x);
    if (p == -1) {
      p = (*badp.begin());
    }
    swap(a[p], x);
    res++;
    if (a[p] == b[p]) rmv(b[p], p);
  }
  if (res > 7 * N)
    cout << -1 << "\n";
  else
    cout << res << "\n";
  return 0;
}
