#include <bits/stdc++.h>
using namespace std;
ifstream in("file.in");
ofstream out("file.out");
int n, a[100005], b[100005], t, ans, first[100005], second[100005];
map<int, queue<int>> m;
int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[n + 1] ^= a[i];
    first[i] = a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    b[n + 1] ^= b[i];
    second[i] = b[i];
  }
  first[n + 1] = a[n + 1];
  second[n + 1] = b[n + 1];
  n++;
  sort(first + 1, first + n + 1);
  sort(second + 1, second + n + 1);
  for (int i = 1; i <= n; i++) {
    if (first[i] != second[i]) {
      cout << -1;
      return 0;
    }
  }
  t = n + 1;
  for (int i = 1; i <= n; i++) {
    if (a[i] != b[i] && t == n + 1) t = i;
    m[b[i]].push(i);
  }
  while (t < n) {
    while (m[a[n]].size() && a[m[a[n]].front()] == b[m[a[n]].front()])
      m[a[n]].pop();
    if (m[a[n]].size()) {
      int first = m[a[n]].front();
      m[a[n]].pop();
      swap(a[n], a[first]);
      ans++;
    } else {
      while (a[t] == b[t] && t < n) {
        t++;
      }
      if (t < n) {
        swap(a[n], a[t]);
        ans++;
      }
    }
  }
  cout << ans;
}
