#include <bits/stdc++.h>
using namespace std;
set<long long> s;
map<long long, int> m;
map<long long, set<int> > pos;
void add(long long x) {
  s.insert(x);
  m[x] += 1;
}
bool del(long long x) {
  m[x] -= 1;
  if (m[x] == 0) {
    s.erase(x);
  }
  if (m[x] < 0) return false;
  return true;
}
int main() {
  int n = 0;
  cin >> n;
  vector<long long> a(n, 0);
  vector<long long> b(n, 0);
  long long x = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    add(a[i]);
    x ^= a[i];
  }
  add(x);
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (not del(b[i])) {
      cout << "-1";
      return 0;
    }
  }
  s.clear();
  m.clear();
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) continue;
    pos[b[i]].insert(i);
    add(b[i]);
  }
  int ans = 0;
  while (true) {
    if (pos[x].size() != 0) {
      ans++;
      int new_pos = *pos[x].begin();
      del(b[new_pos]);
      pos[x].erase(new_pos);
      swap(a[new_pos], x);
      continue;
    }
    if (s.size() == 0) break;
    int new_b = *s.begin();
    int new_pos = *pos[new_b].begin();
    ans++;
    swap(x, a[new_pos]);
  }
  cout << ans << endl;
}
