#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 7;
inline int R() {
  char ch = getchar();
  int rt = 0;
  bool isn = false;
  for (; ch < '0' || ch > '9'; ch = getchar()) isn = ch == '-' ? true : isn;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) rt = rt * 10 + ch - '0';
  return isn ? -rt : rt;
}
int a[MAXN], b[MAXN];
int nxt[MAXN];
map<int, int> hs;
map<int, int> pos;
bool u[MAXN], ud[MAXN];
int main() {
  int n = R(), num = 0, ans = 0;
  for (int i = 1; i <= n; ++i) a[i] = R(), num ^= a[i], ++hs[a[i]];
  ++hs[num];
  for (int i = 1; i <= n; ++i) {
    b[i] = R();
    if (!hs[b[i]]) {
      puts("-1");
      return 0;
    } else
      --hs[b[i]];
  }
  int rem = 0, na = 1;
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i] && hs[a[i]]) {
      if (pos.count(b[i])) nxt[i] = pos[b[i]];
      pos[b[i]] = i;
      ++rem;
      ud[i] = true;
    }
  for (int i = 1; i <= n; ++i) {
    if (ud[i]) continue;
    if (a[i] != b[i]) {
      if (pos.count(b[i])) nxt[i] = pos[b[i]];
      pos[b[i]] = i;
      ++rem;
    } else
      u[i] = true;
  }
  while (u[na]) ++na;
  while (rem) {
    int nn = 0;
    if (pos[num]) {
      --rem, u[pos[num]] = true;
      int lb = num;
      swap(a[pos[num]], num);
      pos[lb] = nxt[pos[lb]];
      while (u[na]) ++na;
    } else
      swap(a[na], num);
    ++ans;
  }
  printf("%d\n", ans);
  return 0;
}
