#include <bits/stdc++.h>
const long long N = 100100;
const long long MX = sqrt(N);
const long long H = 17;
const long long MAGIC = 1000;
const long long mod = 1e9 + 7;
using namespace std;
int n;
int a[N];
int b[N];
set<pair<int, int> > s;
void solve() {
  cin >> n;
  int x = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    x ^= a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    if (a[i] != b[i]) {
      s.insert({b[i], i});
    }
  }
  int cnt = 0;
  while (s.size() > 0) {
    auto p = s.lower_bound({x, 0});
    if (p == s.end()) {
      break;
    } else if (p->first == x) {
      swap(x, a[p->second]);
      cnt++;
      s.erase(p);
    } else {
      break;
    }
  }
  vector<int> va, vb;
  for (auto p : s) {
    va.push_back(a[p.second]);
    vb.push_back(b[p.second]);
  }
  sort(va.begin(), va.end());
  sort(vb.begin(), vb.end());
  for (int i = 0; i < va.size(); i++) {
    if (va[i] != vb[i]) {
      cout << -1 << "\n";
      return;
    }
  }
  cout << cnt + s.size() + s.size() / 2 << "\n";
}
bool mtest = false;
int main() {
  ios_base::sync_with_stdio(0);
  int TE = 1;
  if (mtest) cin >> TE;
  while (TE--) solve();
  return 0;
}
