#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000000 + 7;
map<int, vector<int> > e;
map<int, int> cnt;
std::vector<int> a, b;
void solve() {
  int n, x, y;
  y = 0;
  cin >> n;
  cnt.clear();
  for (int i = 0; i < int(n); ++i) {
    cin >> x;
    a.push_back(x);
    y ^= x;
    cnt[x]++;
  }
  cnt[y]++;
  bool no = 0;
  for (int i = 0; i < int(n); ++i) {
    cin >> x;
    b.push_back(x);
    cnt[x]--;
    if (cnt[x] < 0) no = 1;
  }
  if (no) {
    cout << -1;
    return;
  }
  int ans = 0;
  e.clear();
  for (int i = 0; i < int(n); ++i) {
    if (a[i] != b[i]) e[b[i]].push_back(a[i]);
  }
  {
    while (!e.empty()) {
      int z = y;
      if (e.count(z) == 0) {
        z = e.begin()->first;
      }
      ans++;
      y = e[z].back();
      e[z].pop_back();
      if (e[z].empty()) e.erase(z);
    }
  }
  cout << ans;
}
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(0);
  solve();
}
