#include <bits/stdc++.h>
using namespace std;
const int kInf = 1e9;
void Read(vector<int>& a, int n) {
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sum ^= a[i];
  }
  a[n + 1] = sum;
}
void Solve() {
  int n;
  cin >> n;
  vector<int> a(n + 2), b(n + 2);
  Read(a, n);
  Read(b, n);
  vector<int> go(n + 1);
  vector<bool> viz(n + 1);
  map<int, vector<int>> where;
  map<int, int> count;
  set<int> seen;
  for (int i = 1; i <= n + 1; ++i) {
    if (a[i] == b[i]) continue;
    where[b[i]].push_back(i);
  }
  for (int i = 1; i <= n + 1; ++i) {
    if (a[i] == b[i]) continue;
    if (where[a[i]].empty()) {
      cout << -1 << "\n";
      return;
    }
    go[i] = where[a[i]].back();
    where[a[i]].pop_back();
  }
  if (a[n + 1] == b[n + 1]) {
    seen.insert(a[n + 1]);
  }
  int ans = 0;
  for (int i = 1; i <= n + 1; ++i) {
    if (!viz[i] && a[i] != b[i]) {
      int cnt = 0;
      int special = 0;
      bool merged = false;
      vector<int> now;
      for (int j = i; !viz[j]; j = go[j]) {
        ++cnt;
        viz[j] = true;
        if (j == n + 1) special += 2;
        if (seen.find(a[j]) != seen.end() && !merged) {
          ++special;
          merged = true;
        }
        now.push_back(a[j]);
      }
      for (auto el : now) {
        seen.insert(el);
      }
      ans += cnt + 1 - special;
    }
  }
  cout << ans << "\n";
}
int main() {
  int tests = 1;
  for (; tests; --tests) {
    Solve();
  }
}
