#include <bits/stdc++.h>
using namespace std;
const int Size = 1 << 16;
char buffer[Size], *head, *tail;
inline char Getchar() {
  if (head == tail) {
    int l = fread(buffer, 1, Size, stdin);
    tail = (head = buffer) + l;
  }
  if (head == tail) return -1;
  return *head++;
}
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int maxn = 100010;
int n, A[maxn], B[maxn], p[maxn], la[maxn];
int C[maxn], D[maxn];
int check() {
  for (int i = (1), i_end_ = (n + 1); i <= i_end_; i++)
    C[i] = A[i], D[i] = B[i];
  sort(C + 1, C + n + 2);
  sort(D + 1, D + n + 2);
  for (int i = (1), i_end_ = (n + 1); i <= i_end_; i++)
    if (C[i] != D[i]) return 0;
  for (int i = (1), i_end_ = (n + 1); i <= i_end_; i++)
    A[i] = lower_bound(C + 1, C + n + 2, A[i]) - C,
    B[i] = lower_bound(D + 1, D + n + 2, B[i]) - D;
  return 1;
}
vector<int> G[maxn], T[maxn];
int vis[maxn], tmp[maxn], clo;
int main() {
  n = read();
  for (int i = (1), i_end_ = (n); i <= i_end_; i++) A[n + 1] ^= (A[i] = read());
  for (int i = (1), i_end_ = (n); i <= i_end_; i++) B[n + 1] ^= (B[i] = read());
  if (!check()) {
    puts("-1");
    return 0;
  }
  int ans = 0;
  for (int i = (1), i_end_ = (n + 1); i <= i_end_; i++)
    G[A[i]].push_back(i), T[B[i]].push_back(i);
  for (int i = (1), i_end_ = (n + 1); i <= i_end_; i++) {
    int m = G[A[i]].size(), cnt = 0;
    clo++;
    for (int j = (0), j_end_ = (m - 1); j <= j_end_; j++) vis[G[A[i]][j]] = clo;
    for (int j = (0), j_end_ = (m - 1); j <= j_end_; j++)
      if (vis[T[A[i]][j]] == clo)
        p[T[A[i]][j]] = T[A[i]][j], la[T[A[i]][j]] = T[A[i]][j];
    for (int j = (0), j_end_ = (m - 1); j <= j_end_; j++)
      if (!la[T[A[i]][j]]) tmp[++cnt] = T[A[i]][j];
    cnt = 0;
    for (int j = (0), j_end_ = (m - 1); j <= j_end_; j++)
      if (!p[G[A[i]][j]]) p[G[A[i]][j]] = tmp[++cnt], la[tmp[cnt]] = G[A[i]][j];
  }
  memset(vis, 0, sizeof(vis));
  for (int i = (1), i_end_ = (n + 1); i <= i_end_; i++)
    if (!vis[i]) {
      int j = i, c = 0;
      clo++;
      while (vis[j] != clo) vis[j] = clo, c++, j = p[j];
      if (c == 1) continue;
      if (vis[n + 1] == clo)
        ans += c - 1;
      else
        ans += c + 1;
    }
  printf("%d\n", ans);
  return 0;
}
