#include <bits/stdc++.h>
using namespace std;
using cat = long long;
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
  int N;
  cin >> N;
  vector<int> A(N + 1, 0), B(N + 1, 0);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[N] ^= A[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
    B[N] ^= B[i];
  }
  int dif = 0;
  for (int i = 0; i < N; i++)
    if (A[i] != B[i]) dif++;
  map<int, int> compr;
  for (int i = 0; i <= N; i++)
    if (A[i] != B[i]) compr[A[i]] = 0;
  for (int i = 0; i <= N; i++)
    if (A[i] != B[i]) compr[B[i]] = 0;
  int m = 0;
  for (auto it = compr.begin(); it != compr.end(); it++) it->second = m++;
  vector<vector<int> > G(m);
  for (int i = 0; i <= N; i++)
    if (A[i] != B[i]) {
      G[compr[A[i]]].push_back(compr[B[i]]);
      G[compr[B[i]]].push_back(compr[A[i]]);
    }
  int x = A[N], eq_last = (A[N] == B[N]);
  sort(begin(A), end(A));
  sort(begin(B), end(B));
  for (int i = 0; i <= N; i++)
    if (A[i] != B[i]) {
      cout << "-1\n";
      return 0;
    }
  int comp = 0;
  vector<int> vis(m, false);
  queue<int> q;
  for (int i = 0; i < m; i++)
    if (!vis[i]) {
      comp++;
      vis[i] = true;
      q.push(i);
      while (!q.empty()) {
        for (auto it = G[q.front()].begin(); it != G[q.front()].end(); it++)
          if (!vis[*it]) {
            vis[*it] = true;
            q.push(*it);
          }
        q.pop();
      }
    }
  int ans = (comp == 0) ? 0 : (dif + comp - 1);
  if (eq_last && compr.count(x) == 0) ans++;
  cout << ans << "\n";
  return 0;
}
