#include <bits/stdc++.h>
using namespace std;
class UnionFind {
 public:
  long long k;
  vector<long long> o;
  void init(long long n) {
    k = n;
    o.resize(n, -1);
  }
  long long find(long long n) {
    if (o[n] < 0) {
      return n;
    }
    return o[n] = find(o[n]);
  }
  void unite(long long a, long long b) {
    a = find(a);
    b = find(b);
    if (a != b) {
      k--;
      o[a] = b;
    }
  }
  long long num() { return k; }
};
long long a[114514];
long long b[114514];
signed main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < (n); i++) {
    cin >> a[i];
  }
  for (long long i = 0; i < (n); i++) {
    cin >> b[i];
  }
  long long all = 0;
  for (long long i = 0; i < (n); i++) {
    all = all ^ a[i];
  }
  long long ans = 0;
  map<long long, long long> mp;
  mp[all]++;
  vector<long long> v;
  for (long long i = 0; i < (n); i++) {
    if (a[i] != b[i]) {
      ans++;
      v.push_back(a[i]);
      v.push_back(b[i]);
    }
    mp[a[i]]++;
  }
  v.push_back(all);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  for (long long i = 0; i < (n); i++) {
    if (mp[b[i]] == 0) {
      cout << -1 << endl;
      return 0;
    }
    mp[b[i]]--;
  }
  if (ans == 0) {
    cout << 0 << endl;
  } else {
    UnionFind uf;
    uf.init(v.size());
    for (long long i = 0; i < (n); i++) {
      if (a[i] != b[i]) {
        uf.unite(lower_bound(v.begin(), v.end(), a[i]) - v.begin(),
                 lower_bound(v.begin(), v.end(), b[i]) - v.begin());
      }
    }
    if (uf.num() == 1) {
      cout << ans << endl;
    } else {
      cout << ans + 1 << endl;
    }
  }
}
