#include <bits/stdc++.h>
using namespace std;
template <typename T>
void get_int(T &x) {
  char t = getchar();
  bool neg = false;
  x = 0;
  for (; (t > '9' || t < '0') && t != '-'; t = getchar())
    ;
  if (t == '-') neg = true, t = getchar();
  for (; t <= '9' && t >= '0'; t = getchar()) x = x * 10 + t - '0';
  if (neg) x = -x;
}
template <typename T>
void print_int(T x) {
  if (x < 0) putchar('-'), x = -x;
  short a[20] = {}, sz = 0;
  while (x > 0) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
const int inf = 0x3f3f3f3f;
const long long Linf = 1ll << 62;
const double pi = acos(-1.0);
map<int, int> id;
int tot;
int getid(int x) {
  int &v = id[x];
  if (v) return v;
  return v = ++tot;
}
int dgr[200111], f[200111];
int gf(int x) { return x == f[x] ? x : f[x] = gf(f[x]); }
int a[100111], b[100111], n;
bool check() {
  multiset<int> x;
  for (int i = 0; i <= n; i++) x.insert(a[i]);
  for (int i = 0; i <= n; i++) x.erase(b[i]);
  if (x.size())
    return false;
  else
    return true;
}
int main() {
  for (int i = 0; i < 200111; i++) f[i] = i;
  get_int(n);
  for (int i = 1; i <= n; i++) {
    get_int(a[i]);
    a[0] ^= a[i];
  }
  for (int i = 1; i <= n; i++) {
    get_int(b[i]);
    b[0] ^= b[i];
  }
  if (!check()) {
    puts("-1");
    return 0;
  }
  int ans = -1;
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) {
      int idx = getid(a[i]), idy = getid(b[i]);
      dgr[idx]++;
      dgr[idy]--;
      f[gf(idx)] = gf(idy);
      ans++;
    }
  getid(a[0]);
  getid(b[0]);
  for (int i = 1; i <= tot; i++)
    if (f[i] == i) ans++;
  print_int(ans), puts("");
  return 0;
}
