#pragma comment(linker, "/STACK:512000000")
#define _CRT_SECURE_NO_WARNINGS
//#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

#define all(a) a.begin(), a.end()
typedef long long li;
typedef long double ld;
void solve(bool);
void precalc();
clock_t start;
int main() {
#ifdef AIM
  freopen("/home/alexandero/ClionProjects/ACM/input.txt", "r", stdin);
  //freopen("/home/alexandero/ClionProjects/ACM/output.txt", "w", stdout);
  //freopen("out.txt", "w", stdout);
#else
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
#endif
  start = clock();
  int t = 1;
  cout.sync_with_stdio(0);
  cin.tie(0);
  precalc();
  cout.precision(10);
  cout << fixed;
  //cin >> t;
  int testNum = 1;
  while (t--) {
    //cout << "Case #" << testNum++ << ": ";
    //cerr << testNum << endl;
    solve(true);
    //cerr << testNum - 1 << endl;
  }
  cout.flush();
#ifdef AIM1
  while (true) {
      solve(false);
  }
#endif

#ifdef AIM
  cerr << "\n\n time: " << (clock() - start) / 1.0 / CLOCKS_PER_SEC << "\n\n";
#endif

  return 0;
}

//BE CAREFUL: IS INT REALLY INT?

template<typename T>
T binpow(T q, T w, T mod) {
  if (!w)
    return 1 % mod;
  if (w & 1)
    return q * 1LL * binpow(q, w - 1, mod) % mod;
  return binpow(q * 1LL * q % mod, w / 2, mod);
}

template<typename T>
T gcd(T q, T w) {
  while (w) {
    q %= w;
    swap(q, w);
  }
  return q;
}
template<typename T>
T lcm(T q, T w) {
  return q / gcd(q, w) * w;
}

void precalc() {

}

template<typename T>
void relax_min(T& cur, T val) {
  cur = min(cur, val);
}

template<typename T>
void relax_max(T& cur, T val) {
  cur = max(cur, val);
}

//#define int li
//const int mod = 1000000007;


void solve(bool read) {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  int all_xor = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    all_xor ^= a[i];
  }
  a.push_back(all_xor);
  map<int, int> nums_b;
  int ham = 0;
  all_xor = 0;
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    all_xor ^= b[i];
  }
  b.push_back(all_xor);
  ++n;

  map<int, int> nums;
  for (int i = 0; i < n; ++i) {
    ++nums[a[i]];
    --nums[b[i]];
  }
  for (auto item : nums) {
    if (item.second) {
      cout << "-1\n";
      return;
    }
  }

  int cur_ans = 0;
  for (int i = 0; i + 1 < n; ++i) {
    if (a[i] != b[i]) {
      ++cur_ans;
    }
  }

  set<int> not_used;
  map<int, vector<int>> g;
  for (int i = 0; i < n; ++i) {
    g[a[i]].push_back(b[i]);
    g[b[i]].push_back(a[i]);
    not_used.insert(a[i]);
  }

  for (auto& item : g) {
    sort(all(item.second));
    item.second.erase(unique(all(item.second)), item.second.end());
  }

  int cnt = 0;
  while (!not_used.empty()) {
    int v = *not_used.begin();
    not_used.erase(not_used.begin());
    vector<int> q;
    q.push_back(v);
    for (int i = 0; i < q.size(); ++i) {
      int cur_v = q[i];
      for (int to : g[cur_v]) {
        if (not_used.count(to)) {
          not_used.erase(to);
          q.push_back(to);
        }
      }
    }

    if (q.size() > 1) {
      bool f = false;
      for (int cur_v : q) {
        if (cur_v == all_xor) {
          f = true;
          break;
        }
      }
      if (!f) {
        ++cur_ans;
      }
    }
  }

  cout << cur_ans << endl;

}

Measured memory_bytes: 22256