#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int const MAXN = 110000;
int N;
int a[MAXN];
int b[MAXN];
map<int, vector<int>> posB;
bool was[MAXN];
void dfs(int v) {
  was[v] = true;
  for (int u : posB[a[v]])
    if (!was[u]) dfs(u);
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  cin >> N;
  for (int i = 0; i < (N); ++i) cin >> a[i];
  for (int i = 0; i < (N); ++i) cin >> b[i];
  for (int i = 0; i < (N); ++i) a[N] ^= a[i];
  for (int i = 0; i < (N); ++i) b[N] ^= b[i];
  vector<int> vA(a, a + N + 1);
  vector<int> vB(b, b + N + 1);
  if (vA == vB) {
    cout << "0\n";
    return 0;
  }
  sort(vA.begin(), vA.end());
  sort(vB.begin(), vB.end());
  if (vA != vB) {
    cout << "-1\n";
    return 0;
  }
  int res = 0;
  for (int i = 0; i < (N); ++i)
    if (a[i] != b[i]) {
      res++;
      posB[b[i]].push_back(i);
    }
  int cc = 0;
  for (int i = 0; i < (N); ++i)
    if (a[i] != b[i] && !was[i]) {
      cc++;
      dfs(i);
    }
  res += cc;
  if (a[N] != b[N] || posB.count(b[N])) {
    res--;
  }
  cout << res << "\n";
}
