#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int n;
int a[MAXN], b[MAXN], p[2][MAXN];
set<int> vis1, vis2;
void work(int id, int x) {
  for (int i = 0; (1 << i) <= x; i++)
    if (x & (1 << i)) p[id][i] = 1;
}
int main() {
  int sum = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  for (int i = 1; i <= n; i++) {
    sum ^= a[i];
    work(0, a[i]);
    work(1, b[i]);
    if (a[i] == b[i]) continue;
    if (vis1.count(a[i]) || vis2.count(b[i])) return puts("-1"), 0;
    vis1.insert(a[i]);
    vis2.insert(b[i]);
  }
  for (int i = 0; i < 100 && p[1][i]; i++)
    if (p[1][i] && !p[0][i]) return puts("-1"), 0;
  int cnt = 0, flag = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == b[i]) continue;
    if (b[i] == sum) flag = 1;
    cnt++;
  }
  cnt /= 2;
  cnt *= 3;
  if (flag) cnt--;
  printf("%d", cnt);
}
