#include <bits/stdc++.h>
using namespace std;
void play() {
  long long t = 1000;
  long long n;
  cin >> n;
  vector<long long> vec(n);
  for (int i = 0; i < n; i++) cin >> vec[i];
  while (t--) {
    long long x;
    cin >> x;
    long long xr = 0;
    for (int i = 0; i < n; i++) {
      xr ^= vec[i];
    }
    for (int i = 0; i < n; i++) {
      if (vec[i] == x) {
        vec[i] = xr;
        i = n + 2;
      }
    }
    xr = 0;
    for (int i = 0; i < n; i++) {
      xr ^= vec[i];
    }
    cout << "-----------------\n";
    cout << xr << "\n";
    for (auto cur : vec) cout << cur << " ";
    cout << "\n----------------\n";
  }
}
long long n;
vector<long long> vec, vec2;
void input() {
  cin >> n;
  vec.resize(n);
  vec2.resize(n);
  for (int i = 0; i < n; i++) cin >> vec[i];
  for (int i = 0; i < n; i++) cin >> vec2[i];
}
bool possib() {
  map<long long, long long> cnt;
  long long x = 0;
  for (auto cur : vec) {
    cnt[cur]++;
    x ^= cur;
  }
  cnt[x]++;
  for (auto cur : vec2) {
    cnt[cur]--;
    if (cnt[cur] < 0) return false;
  }
  return true;
}
const long long SZ = 1e5 + 10;
long long dsu[SZ];
int find(int v) {
  if (dsu[v] == v)
    return v;
  else
    return dsu[v] = find(dsu[v]);
}
void uni(int v, int u) {
  v = find(v);
  u = find(u);
  if (u != v) {
    dsu[u] = v;
  }
}
void debug() { debug(); }
long long sol() {
  if (!possib()) return -1;
  cout << 2 / 0;
  for (int i = 0; i < n; i++) dsu[i] = i;
  long long needed = 0;
  map<long long, vector<long long>> poss;
  long long x = 0, dis = 0;
  for (int i = 0; i < vec.size(); i++) {
    x ^= vec[i];
    if (vec[i] != vec2[i]) needed++;
    poss[vec[i]].push_back(i);
    poss[vec2[i]].push_back(i);
  }
  for (int i = 0; i < vec2.size(); i++) {
    if (x == vec2[i]) dis = -1;
  }
  for (auto cur : poss) {
    for (auto c : cur.second) {
      uni(cur.second[0], c);
    }
  }
  set<long long> dif;
  for (int i = 0; i < n; i++) {
    if (vec[i] != vec2[i]) dif.insert(dsu[i]);
  }
  return needed + long long(dif.size()) + dis;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  ;
  input();
  cout << sol();
  return 0;
}
