#include <bits/stdc++.h>
const int N = 1e5 + 10;
int a[N], b[N], n, a1[N], b1[N], ans, cnt;
bool v[N];
std::map<int, int> m;
std::vector<int> x, y, id[N], to[N];
inline bool dfs(int cur, int dis) {
  if (dis == static_cast<int>(x.size())) return true;
  if (static_cast<int>(to[cur].size()) == 0) return false;
  int u = *(to[cur].end() - 1);
  to[cur].pop_back();
  dfs(u, dis + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    a[n] ^= a[i];
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", b + i);
    b[n] ^= b[i];
  }
  memcpy(a1, a, sizeof a);
  memcpy(b1, b, sizeof b);
  std::sort(a1, a1 + n);
  std::sort(b1, b1 + n);
  memcpy(a1, a, sizeof a);
  memcpy(b1, b, sizeof b);
  std::sort(a1, a1 + n + 1);
  std::sort(b1, b1 + n + 1);
  for (int i = 0; i <= n; i++)
    if (a1[i] != b1[i]) {
      printf("-1\n");
      return 0;
    }
  for (int i = 0; i < n; i++)
    if (a[i] != b[i]) {
      x.push_back(a[i]);
      y.push_back(b[i]);
    }
  if (a[n] != b[n]) {
    printf("%d\n", static_cast<int>(x.size()));
    return 0;
  }
  for (int i : x) m[i] = ++cnt;
  m[a[n]] = ++cnt;
  for (int i = 0; i < static_cast<int>(x.size()); i++)
    to[m[x[i]]].push_back(m[y[i]]);
  if (dfs(m[a[n]], 0))
    printf("%d\n", static_cast<int>(x.size()));
  else
    printf("%d\n", static_cast<int>(x.size()) + 1);
  return 0;
}
