#include <bits/stdc++.h>
using namespace std;
long long a[100006], b[100006];
long long sa[100006], sb[100006];
long long XA, XB;
vector<long long> idx[100006];
long long par[100006], siz[100006];
void init(long long n) {
  for (long long i = 0; i < n; i++) {
    par[i] = i;
    siz[i] = 1;
  }
}
long long find(long long x) {
  if (par[x] == x) return x;
  return par[x] = find(par[x]);
}
void unite(long long x, long long y) {
  x = find(x), y = find(y);
  if (x == y) return;
  if (siz[x] < siz[y]) swap(x, y);
  par[y] = x;
  siz[x] += siz[y];
}
signed main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < n; i++) cin >> b[i];
  for (long long i = 0; i < n; i++) {
    sa[i] = a[i];
    sb[i] = b[i];
    sa[n] ^= a[i];
    sb[n] ^= b[i];
  }
  a[n] = sa[n], b[n] = sb[n];
  sort(sa, sa + n + 1);
  sort(sb, sb + n + 1);
  for (long long i = 0; i < n + 1; i++) {
    if (sa[i] != sb[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  long long m = 0;
  for (long long i = 0; i < n + 1; i++) {
    if (a[i] != b[i] || i == n) {
      sa[m] = a[i];
      sb[m] = b[i];
      m++;
    }
  }
  map<long long, long long> M;
  for (long long i = 0; i < m; i++) M[sa[i]]++;
  long long c = 0;
  for (auto itr = M.begin(); itr != M.end(); itr++) {
    M[itr->first] = c;
    c++;
  }
  for (long long i = 0; i < m; i++) {
    sa[i] = M[sa[i]];
    sb[i] = M[sb[i]];
    idx[sa[i]].push_back(i);
  }
  init(m);
  for (long long i = 0; i < 100001; i++) {
    if (idx[i].size()) {
      for (long long j = 0; j < idx[i].size() - 1; j++) {
        unite(idx[i][j], idx[i][j + 1]);
      }
    }
  }
  for (long long i = 0; i < m; i++) {
    unite(idx[sa[i]][0], idx[sb[i]][0]);
  }
  set<long long> S;
  long long ans = 0;
  for (long long i = 0; i < m; i++) {
    if (S.count(par[i])) continue;
    if (find(i) == find(m - 1))
      ans += siz[find(i)] - 1;
    else
      ans += siz[find(i)] + 1;
    S.insert(par[i]);
  }
  cout << ans << endl;
}
