#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100010;
const double eps = 1e-6;
const long long mod = 1000000007;
const int inf = 1 << 30;
void Debug() { cout << '\n'; }
template <class FIRST, class... REST>
void Debug(FIRST arg, REST... rest) {
  cout << arg << " ";
  Debug(rest...);
}
template <class T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  out << "[";
  if (!v.empty()) {
    for (int i = 0; i < (int)v.size() - 1; i++) out << v[i] << ", ";
    out << v.back();
  }
  out << "]";
  return out;
}
template <class S, class T>
ostream& operator<<(ostream& out, const pair<S, T>& v) {
  out << "(" << v.first << ", " << v.second << ")";
  return out;
}
int par[MAX_N];
int ran[MAX_N];
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    ran[i] = 0;
  }
}
int find(int x) {
  if (par[x] == x) {
    return x;
  } else
    return par[x] = find(par[x]);
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return;
  if (ran[x] < ran[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (ran[x] == ran[y]) ran[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
int N;
int AA[MAX_N];
int BB[MAX_N];
int A[MAX_N], B[MAX_N];
int C[MAX_N];
vector<int> vec;
int fd(int a) {
  return lower_bound((vec).begin(), (vec).end(), a) - vec.begin();
}
void solve() {
  cin >> N;
  int a = 0;
  for (int i = 0; i < N; i++) {
    cin >> AA[i];
    a ^= AA[i];
  }
  for (int i = 0; i < N; i++) cin >> BB[i];
  int at = 0;
  for (int i = 0; i < N; i++) {
    if (AA[i] != BB[i]) {
      A[at] = AA[i];
      B[at] = BB[i];
      at++;
    }
  }
  N = at;
  A[N] = a;
  for (int i = 0; i < N; i++) vec.push_back(B[i]);
  sort((vec).begin(), (vec).end());
  vec.erase(unique((vec).begin(), (vec).end()), vec.end());
  int n = (int)vec.size();
  for (int i = 0; i < N; i++) {
    int a = fd(B[i]);
    C[a]++;
  }
  bool ok = true;
  for (int i = 0; i < N + 1; i++) {
    int a = fd(A[i]);
    if (a >= n) continue;
    if (vec[a] == A[i]) C[a]--;
  }
  for (int i = 0; i < n; i++) {
    if (C[i] > 0) {
      ok = false;
      break;
    }
  }
  if (!ok)
    cout << "-1\n";
  else {
    init(n);
    for (int i = 0; i < N; i++) unite(fd(A[i]), fd(B[i]));
    set<int> s;
    for (int i = 0; i < n; i++) s.insert(find(i));
    cout << N + (int)s.size() - 1 << "\n";
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed;
  cout.precision(20);
  solve();
  return 0;
}
