#include <bits/stdc++.h>
using namespace std;
long long int pw(long long int a, long long int b, long long int mod) {
  if (!b) return 1;
  if (b & 1) return a * pw(a * a % mod, b / 2, mod) % mod;
  return pw(a * a % mod, b / 2, mod) % mod;
}
const long long int N = 1e5 + 10;
const long long int MOD = 1e9 + 7;
const long long int INF = 1e18;
int n, A[N], B[N], ind[N], mark[N], ans;
map<int, vector<int>> mp;
int Fix(int i) {
  mark[i] = 1;
  int res = 1;
  if (!mark[ind[i]]) res += Fix(ind[i]);
  ind[i] = i;
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    A[n] ^= A[i];
  }
  for (int j = 0; j < n; j++) {
    cin >> B[j];
    B[n] ^= B[j];
  }
  for (int i = 0; i <= n; i++) {
    if (i < n && A[i] == B[i]) {
      ind[i] = i;
      continue;
    }
    mp[B[i]].push_back(i);
  }
  for (int i = 0; i <= n; i++) {
    if (i < n && A[i] == B[i]) continue;
    if (!mp.count(A[i]) || mp[A[i]].empty()) return cout << -1 << '\n', 0;
    ind[i] = mp[A[i]].back();
    mp[A[i]].pop_back();
  }
  ans = Fix(n) - 1;
  for (int i = 0; i < n; i++) {
    if (i != ind[i]) ans += Fix(i) + 1;
  }
  cout << ans << '\n';
  return 0;
}
