#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T& x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}
template <typename T, typename... U>
inline void read(T& x, U&... y) {
  read(x), read(y...);
}
const int N = 1e5 + 10;
map<int, int> S;
int n, p = 1, cnt, res;
int head[N], A[N], B[N], G[N];
bool vis[N], sign[N << 1];
struct Edge {
  int to, last;
  Edge() {}
  Edge(int a, int b) : to(a), last(b) {}
} edge[N << 1];
void ADD(int a, int b) {
  edge[++p] = Edge(b, head[a]);
  head[a] = p;
  edge[++p] = Edge(a, head[b]);
  head[b] = p;
}
void DFS(int u) {
  if (vis[u]) return;
  vis[u] = true;
  for (int i = head[u]; i; i = edge[i].last)
    if (!sign[i]) {
      sign[i] = sign[i ^ 1] = true;
      ++res;
      DFS(edge[i].to);
    }
}
int main() {
  read(n);
  for (int i = 1; i <= n; ++i) {
    read(A[i]);
    A[n + 1] ^= A[i];
    if (!S.count(A[i])) S[A[i]] = ++cnt;
    ++G[S[A[i]]];
  }
  if (!S.count(A[n + 1])) S[A[n + 1]] = ++cnt;
  ++G[S[A[n + 1]]];
  for (int i = 1; i <= n; ++i) {
    read(B[i]);
    if (!S.count(B[i])) return puts("-1"), 0;
    --G[S[B[i]]];
    if (G[S[B[i]]] < 0) return puts("-1"), 0;
  }
  int gg = 0;
  for (int i = 1; i <= n; ++i) {
    int u = S[A[i]], v = S[B[i]];
    if (u != v)
      ADD(u, v);
    else
      ++gg, vis[v] = true;
  }
  if (gg == n) return puts("0"), 0;
  if (head[S[A[n + 1]]])
    DFS(S[A[n + 1]]);
  else
    ++res;
  for (int i = 1; i <= n; ++i)
    if (!vis[S[B[i]]]) {
      ++res;
      DFS(S[B[i]]);
    }
  printf("%d\n", res);
  return 0;
}
