#include <bits/stdc++.h>
using namespace std;
template <class C>
inline bool in_(const typename C::key_type& k, const C& A) {
  return A.find(k) != A.end();
}
inline bool in_(const string& s, const string& S) {
  return S.find(s) != string::npos;
}
int main() {
  int N;
  while (cin >> N) {
    int vis[N + 1];
    memset(vis, 0, sizeof(vis));
    map<int, set<int> > M;
    int X[N + 1], a0 = 0;
    for (int i = (1); i < (N + 1); ++i) {
      int a;
      cin >> a;
      a0 ^= a;
      M[a].insert(i);
    }
    M[a0].insert(0);
    int possible = true;
    for (int i = (1); i < (N + 1); ++i) {
      int b;
      cin >> b;
      set<int>& index = M[b];
      if (index.empty()) {
        possible = false;
        break;
      }
      X[i] = *index.begin();
      vis[X[i]]++;
      index.erase(index.begin());
    }
    for (int i = (0); i < (N + 1); ++i)
      if (!vis[i]) {
        X[0] = i;
        break;
      }
    int ans = -1;
    if (possible) {
      memset(vis, 0, sizeof(vis));
      ans = 0;
      for (int i = (0); i < (N + 1); ++i)
        if (!vis[i]) {
          vis[i]++;
          int cnt = 1, has0 = i == 0;
          int j = i;
          while (!vis[X[j]]) {
            j = X[j];
            has0 |= j == 0;
            vis[j]++;
            cnt++;
          }
          if (cnt > 1) {
            if (has0)
              ans += cnt - 1;
            else
              ans += cnt + 1;
          }
        }
    }
    cout << ans << endl;
  }
  return 0;
}
