#include <bits/stdc++.h>
using namespace std;
const double M_PI = acos(-1.0);
const int MX = 1200500;
int a[MX];
int b[MX];
map<int, int> m;
map<int, list<int>> sp;
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  int s = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    s ^= a[i];
  }
  m[s]++;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (a[i] != b[i]) {
      sp[b[i]].push_back(i);
    }
    auto &z = m[b[i]];
    z--;
    if (z < 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  int ans = 0;
  while (sp[s].begin() != sp[s].end()) {
    int u = sp[s].front();
    sp[s].pop_front();
    if (a[u] == b[u]) continue;
    swap(a[u], s);
    ans++;
  }
  for (int pos = 0; pos < n; pos++) {
    if (a[pos] == b[pos]) continue;
    int s = a[pos];
    int r = 0;
    while (sp[s].begin() != sp[s].end()) {
      int u = sp[s].front();
      sp[s].pop_front();
      if (a[u] == b[u]) continue;
      swap(a[u], s);
      r++;
    }
    ans += r + 1;
  }
  cout << ans << endl;
  return 0;
}
