#include <bits/stdc++.h>
using namespace std;
int n, i, ans;
int a[400011], b[400011], xoro, dif;
map<int, int> M, M_a, M_b, pp;
vector<int> vals;
vector<int> neg, poz;
vector<int> list[400011];
bool us[400011], any;
void no_sol() {
  printf("-1");
  exit(0);
}
void give_sol(int x) {
  printf("%d", x);
  exit(0);
}
void dfs(int node) {
  us[node] = true;
  for (auto to : list[node]) {
    any = true;
    if (!us[to]) dfs(to);
  }
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]), xoro ^= a[i], vals.push_back(a[i]), M[a[i]]++;
  for (i = 1; i <= n; i++)
    scanf("%d", &b[i]), vals.push_back(b[i]), M[b[i]]--, dif += (a[i] != b[i]);
  for (i = 1; i <= n; i++) {
    if (a[i] == b[i]) continue;
    M_a[a[i]]++;
    M_b[b[i]]++;
  }
  vals.push_back(xoro);
  sort(vals.begin(), vals.end());
  vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
  for (i = 0; i < vals.size(); i++) pp[vals[i]] = i + 1;
  for (auto e : vals) {
    if (M[e] < -1 || M[e] > 1) no_sol();
    if (M[e] == -1) neg.push_back(e);
    if (M[e] == 1) poz.push_back(e);
  }
  if (neg.size() > 1) no_sol();
  if (poz.size() > 1) no_sol();
  n++;
  a[n] = xoro;
  for (i = 1; i < n; i++) b[n] ^= b[i];
  for (i = 1; i <= n; i++) {
    if (a[i] == b[i]) continue;
    list[pp[a[i]]].push_back(pp[b[i]]);
    list[pp[b[i]]].push_back(pp[a[i]]);
  }
  ans = dif;
  for (i = 1; i <= vals.size(); i++) {
    if (us[i]) continue;
    any = false;
    dfs(i);
    if (any) ans++;
    if (any == false && a[n] == b[n] && i == pp[a[n]]) ans++;
  }
  cout << max(0, ans - 1);
  return 0;
}
