#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int const MAXN = 110000;
int N;
int a[MAXN];
int b[MAXN];
map<int, vector<int>> posB;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  cin >> N;
  for (int i = 0; i < (N); ++i) cin >> a[i];
  for (int i = 0; i < (N); ++i) cin >> b[i];
  for (int i = 0; i < (N); ++i)
    if (a[i] != b[i]) posB[b[i]].push_back(i);
  int cur = 0;
  for (int i = 0; i < (N); ++i) cur ^= a[i];
  int l = 0;
  int res = 0;
  while (!posB.empty()) {
    res++;
    if (posB.count(cur)) {
      auto &p = posB[cur];
      int i = p.back();
      p.pop_back();
      if (p.empty()) posB.erase(cur);
      int tmp = cur;
      cur = a[i];
      a[i] = tmp;
      continue;
    }
    while (l < N && a[l] == b[l]) l++;
    assert(l < N);
    int tmp = cur;
    cur = a[l];
    a[l] = tmp;
    if (!posB.count(cur)) {
      cout << "-1\n";
      return 0;
    }
  }
  cout << res << "\n";
}
