#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e8;
const double EPS = 1e-8;
const long long MOD = 1e9 + 7;
class UnionFind {
  vector<int> p, r;
  int s;

 public:
  UnionFind(int n) : p(n, -1), r(n, 0), s(n) {}
  int find(int x) { return (p[x] < 0) ? x : (p[x] = find(p[x])); }
  bool same(int x, int y) { return find(x) == find(y); }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    s--;
    if (r[x] < r[y])
      p[x] = y;
    else
      p[y] = x;
    if (r[x] == r[y]) r[x]++;
  }
  int size() { return s; };
};
int main() {
  int n;
  cin >> n;
  map<int, int> cnt;
  vector<int> a(n);
  int x = 0;
  for (int i = 0; i < (int)(n); i++) {
    cin >> a[i];
    x ^= a[i];
    cnt[a[i]]++;
  }
  cnt[x]++;
  vector<int> b(n);
  for (int i = 0; i < (int)(n); i++) {
    cin >> b[i];
    cnt[b[i]]--;
    if (cnt[b[i]] < 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  int num = 0;
  vector<int> ida;
  ida.emplace_back(x);
  set<int> idb;
  for (int i = 0; i < (int)(n); i++) {
    if (a[i] != b[i]) {
      ida.emplace_back(a[i]);
      idb.emplace(b[i]);
      num++;
    }
  }
  sort((ida).begin(), (ida).end());
  ida.erase(unique((ida).begin(), (ida).end()), ida.end());
  if (ida.size() == 1) {
    cout << 0 << endl;
    return 0;
  }
  UnionFind uf(ida.size());
  for (int i = 0; i < (int)(n); i++) {
    if (a[i] != b[i]) {
      int ai =
          distance(ida.begin(), lower_bound((ida).begin(), (ida).end(), a[i]));
      int bi =
          distance(ida.begin(), lower_bound((ida).begin(), (ida).end(), b[i]));
      uf.unite(ai, bi);
    }
  }
  cerr << uf.size() << endl;
  cout << num + uf.size() - idb.count(x) << endl;
}
