#include <bits/stdc++.h>
using namespace std;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const long long INF = 1LL << 29;
const long long mod = 1e9 + 7;
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  int n = v.size();
  for (int(i) = 0; (i) < (long long)(n); ++(i))
    os << v[i] << (i == n - 1 ? "" : " ");
  return os;
}
int main() {
  int n;
  cin >> n;
  vector<int> a[2];
  for (int(i) = 0; (i) < (long long)(2); ++(i)) a[i] = vector<int>(n + 1, 0);
  for (int(i) = 0; (i) < (long long)(2); ++(i))
    for (int(j) = 0; (j) < (long long)(n); ++(j)) cin >> a[i][j];
  for (int(i) = 0; (i) < (long long)(2); ++(i))
    for (int(j) = 0; (j) < (long long)(n); ++(j)) a[i][n] ^= a[i][j];
  long long x = a[0][n], y = a[1][n];
  multiset<pair<long long, long long> > v;
  for (int(i) = 0; (i) < (long long)(n); ++(i))
    if (a[0][i] != a[1][i])
      v.insert(pair<long long, long long>(a[1][i], a[0][i]));
  for (int(i) = 0; (i) < (long long)(2); ++(i))
    sort((a[i]).begin(), (a[i]).end());
  if (a[0] != a[1]) {
    cout << -1 << endl;
    return 0;
  }
  int res = 0;
  while (!v.empty()) {
    auto it = v.lower_bound(pair<long long, long long>(x, 0));
    pair<long long, long long> p = pair<long long, long long>(it->first, x);
    x = it->second;
    res++;
    v.erase(it);
    if (p.first != p.second) v.insert(p);
  }
  cout << res << endl;
  return 0;
}
