#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;
}
const int maxn = 1e5 + 5;
int A[maxn], B[maxn], C[maxn], D[maxn], vis[maxn];
stack<int> pos[maxn];
void dfs(const int i) {
  vis[i]++;
  const int b = B[i];
  while (pos[b].size()) {
    const int j = pos[b].top();
    pos[b].pop();
    if (!vis[j]) dfs(j);
  }
}
int main() {
  int N;
  while (cin >> N) {
    for (int i = (0); i < (N + 1); ++i)
      while (pos[i].size()) pos[i].pop();
    int a0 = 0, b0 = 0;
    for (int i = (1); i < (N + 1); ++i) {
      cin >> A[i];
      a0 ^= A[i];
      C[i] = A[i];
    }
    A[0] = C[0] = a0;
    for (int i = (1); i < (N + 1); ++i) {
      cin >> B[i];
      b0 ^= B[i];
      D[i] = B[i];
    }
    B[0] = D[0] = b0;
    sort(C, C + N + 1);
    sort(D, D + N + 1);
    bool possible = true;
    for (int i = (0); i < (N + 1); ++i) possible &= (C[i] == D[i]);
    int ans = -1;
    if (possible) {
      ans = 0;
      for (int i = (0); i < (N + 1); ++i) {
        A[i] = lower_bound(C, C + N + 1, A[i]) - C;
        B[i] = lower_bound(D, D + N + 1, B[i]) - D;
        pos[A[i]].push(i);
      }
      memset(vis, 0, sizeof(vis));
      for (int i = (0); i < (N + 1); ++i) {
        if (A[i] == B[i])
          vis[i]++;
        else
          ans += i != 0;
      }
      for (int i = (0); i < (N + 1); ++i)
        if (!vis[i]) {
          dfs(i);
          ans++;
        }
      ans--;
      ans = max(0, ans);
    }
    cout << ans << endl;
  }
  return 0;
}
