#include <bits/stdc++.h>
using namespace std;
int n, a[100100], b[100100], cnt, ans, ta, tb, now, pos[100100];
map<int, int> mm, ca, cb;
map<int, int>::iterator ia, ib, it;
vector<int> need[100100];
int main() {
  int i, j, p, q;
  cin >> n;
  for (i = 1; i <= n; i++) scanf("%d", &a[i]), ta ^= a[i];
  ca[ta]++;
  for (i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    if (b[i] != a[i]) {
      if (!mm.count(b[i])) {
        mm[b[i]] = ++cnt;
      }
      need[mm[b[i]]].push_back(a[i]);
      ca[a[i]]++;
      cb[b[i]]++;
    }
    tb ^= b[i];
  }
  cb[tb]++;
  for (ia = ca.begin(), ib = cb.begin(); ia != ca.end(); ia++, ib++) {
    if ((*ia).first != (*ib).first || (*ia).second != (*ib).second) {
      puts("-1");
      return 0;
    }
  }
  for (now = ta, it = mm.begin();;) {
    if (need[mm[now]].size() == pos[mm[now]]) {
      for (; it != mm.end(); it++) {
        p = (*it).second;
        if (need[p].size() == pos[p]) continue;
        swap(now, need[p][pos[p]]);
        ans++;
        break;
      }
      if (it == mm.end()) break;
    }
    ans++;
    p = mm[now];
    now = need[p][pos[p]];
    pos[p]++;
  }
  cout << ans;
}
