#include <bits/stdc++.h>
using namespace std;
inline int read() {
  bool sgn = 0;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-') c = getchar();
  if (c == '-') sgn = 1, c = getchar();
  int x = 0;
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return sgn ? -x : x;
}
inline void write(int x) {
  if (x < 0) {
    putchar('-');
    write(-x);
  } else {
    if (x >= 10) write(x / 10);
    putchar(x % 10 + '0');
  }
}
int a[100002], b[100002], t[35], n, ma, ans = 2000000000;
bool vis[100002];
inline int Xor() {
  int x = 0;
  for (register int j = 0; (1 << j) <= ma; j++) {
    x |= ((t[j] & 1) << j);
  }
  return x;
}
inline bool same() {
  for (register int i = 1; i <= n; i++)
    if (a[i] != b[i]) return 0;
  return 1;
}
void dfs(int step) {
  if (step > ans) return;
  if (same()) {
    if (step < ans) ans = step;
    return;
  }
  int x = Xor(), tmp;
  for (register int i = 1; i <= n; i++) {
    if (vis[i]) continue;
    vis[i] = 1;
    tmp = a[i];
    a[i] = x;
    int ma_tmp_x = tmp > x ? tmp : x;
    for (register int j = 0; (1 << j) <= ma_tmp_x; j++) {
      t[j] = t[j] - bool(tmp & (1 << j)) + bool(x & (1 << j));
    }
    dfs(step + 1);
    vis[i] = 0;
    a[i] = tmp;
    for (register int j = 0; (1 << j) <= ma_tmp_x; j++) {
      t[j] = t[j] + bool(tmp & (1 << j)) - bool(x & (1 << j));
    }
  }
}
inline void spj() {
  int k = 1, l = 1, step = 0, cnt = 0;
  while (k <= n && (a[k] || !b[k])) k++;
  while (l <= n && (!a[l] || b[l])) l++;
  while (k <= n || l <= n) {
    if (cnt++ > 30000000) {
      printf("-1");
      exit(0);
    }
    if (t[0] & 1) {
      if (k > n) continue;
      t[0]++;
      step++;
      k++;
      while (k <= n && (a[k] || !b[k])) k++;
    } else {
      if (l > n) continue;
      t[0]--;
      step++;
      l++;
      while (l <= n && (!a[l] || b[l])) l++;
    }
  }
  printf("%d", step);
  exit(0);
}
int main() {
  n = read();
  bool flag = 1;
  for (register int i = 1; i <= n; i++) {
    a[i] = read();
    for (register int j = 0; (1 << j) <= a[i]; j++) {
      t[j] += bool(a[i] & (1 << j));
    }
    if (a[i] > ma) ma = a[i];
    if (a[i] != 1 && a[i]) flag = 0;
  }
  for (register int i = 1; i <= n; i++) {
    b[i] = read();
    if (b[i] != 1 && b[i]) flag = 0;
  }
  if (flag) spj();
  dfs(0);
  write(ans == 2000000000 ? -1 : ans);
  return 0;
}
