#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
void Fail() {
  printf("-1\n");
  exit(0);
}
int n, a[MAXN], b[MAXN], cx;
queue<int> q[2];
int sol;
void floor(int bit) {
  cx = 0;
  for (int i = 0; i < n; i++) cx ^= a[i];
  while (!q[0].empty()) q[0].pop();
  while (!q[1].empty()) q[1].pop();
  for (int i = 0; i < n; i++)
    if (((a[i] ^ b[i]) & bit)) q[!!(b[i] & bit)].push(i);
  while (true) {
    int hq = !!(cx & bit);
    if (q[!!(cx & bit)].empty()) return;
    int h = q[hq].front();
    q[hq].pop();
    int bx = cx;
    cx ^= a[h];
    a[h] = bx;
    cx ^= a[h];
    ++sol;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", a + i);
  for (int i = 0; i < n; i++) scanf("%d", b + i);
  for (int i = 0; i < 29; i++) floor(1 << i);
  for (int i = 0; i < n; i++)
    if (a[i] != b[i]) Fail();
  printf("%d\n", sol);
  return 0;
}
