#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1e5 + 10;
int n, a[Maxn], lisan[Maxn], b[Maxn], fa[Maxn], f[Maxn];
int find(int x) {
  if (x != fa[x]) {
    int fat = find(fa[x]);
    fa[x] = fat;
    return fat;
  } else
    return x;
}
map<int, int> c;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    lisan[i] = a[i];
    a[n + 1] ^= a[i];
    c[a[i]]++;
  }
  lisan[n + 1] = a[n + 1];
  c[a[n + 1]]++;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    c[b[i]]--;
    b[n + 1] ^= b[i];
  }
  c[b[n + 1]]--;
  for (map<int, int>::iterator it = c.begin(); it != c.end(); it++) {
    if (it->second != 0) {
      printf("-1");
      return 0;
    }
  }
  int cnt = 1;
  sort(lisan + 1, lisan + 2 + n);
  n++;
  for (int i = 2; i <= n; i++) {
    if (lisan[i] != lisan[cnt]) {
      lisan[++cnt] = lisan[i];
    }
  }
  for (int i = 1; i <= cnt; i++) {
    fa[i] = i;
  }
  int ans = n;
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(lisan + 1, lisan + 1 + cnt, a[i]) - lisan;
    b[i] = lower_bound(lisan + 1, lisan + 1 + cnt, b[i]) - lisan;
    if (find(a[i]) != find(b[i])) {
      fa[find(a[i])] = b[i];
      f[find(b[i])] = 1;
    }
    if (a[i] == b[i]) ans--;
  }
  if (b[n] != a[n]) ans -= 2;
  for (int i = 1; i <= cnt; i++) {
    if (fa[i] == i && f[i]) ans++;
  }
  printf("%d\n", ans);
  return 0;
}
