#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int b[N];
int tmp_a[N];
int tmp_b[N];
map<int, int> id;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[n] ^= a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    b[n] ^= b[i];
  }
  n++;
  for (int i = 0; i < n; i++) {
    tmp_a[i] = a[i];
    tmp_b[i] = b[i];
  }
  sort(tmp_a, tmp_a + n);
  sort(tmp_b, tmp_b + n);
  for (int i = 0; i < n; i++) {
    if (tmp_a[i] != tmp_b[i]) {
      cout << "-1" << endl;
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    if (id.count(a[i]) == 0) id[a[i]] = id.size() - 1;
  }
  for (int i = 0; i < n; i++) {
    a[i] = id[a[i]];
    b[i] = id[b[i]];
  }
  vector<vector<int> > pos(n);
  set<int> bad_pos;
  for (int i = 0; i < n - 1; i++) {
    if (a[i] != b[i]) {
      pos[b[i]].push_back(i);
      bad_pos.insert(i);
    }
  }
  int res = 0;
  while (!bad_pos.empty()) {
    if (!pos[a[n - 1]].empty()) {
      int t = pos[a[n - 1]].back();
      pos[a[n - 1]].pop_back();
      swap(a[t], a[n - 1]);
      bad_pos.erase(t);
    } else {
      int t = *bad_pos.begin();
      swap(a[t], a[n - 1]);
    }
    res++;
  }
  cout << res << endl;
  return 0;
}
