#include <bits/stdc++.h>
struct edge {
  int to, next;
  edge(int _to = 0, int _next = 0) : to(_to), next(_next) {}
} e[2000010 << 1];
int n, m, S, T;
int g[2000010], nume;
int e2[2000010][2];
int d[2000010];
int visit[2000010];
void addEdge(int u, int v) {
  e[nume] = edge(v, g[u]);
  g[u] = nume++;
}
void dfs(int x) {
  visit[x] = 1;
  for (int i = g[x]; ~i; i = e[i].next)
    if (!visit[e[i].to]) dfs(e[i].to);
}
int gao() {
  int res = 0;
  for (int i = 0; i <= n; i++)
    if (g[i] != -1 && !visit[i]) {
      dfs(i);
      res++;
    }
  if (visit[S]) res--;
  for (int i = 0; i <= n; i++)
    if (visit[i]) res++;
  return res;
}
int main() {
  memset(g, -1, sizeof g);
  scanf("%d", &m);
  n = 150000;
  for (int i = 1; i <= m; i++) scanf("%d", &e2[i][1]);
  for (int i = 1; i <= m; i++) scanf("%d", &e2[i][0]);
  for (int i = 1; i <= m; i++) {
    d[e2[i][0]]++;
    d[e2[i][1]]--;
    S ^= e2[i][1];
    if (e2[i][0] ^ e2[i][1]) addEdge(e2[i][0], e2[i][1]);
  }
  bool flag = 0;
  if (d[S] == 1) {
    T = -1;
    for (int i = 1; i <= n; i++)
      if (d[i] == -1) {
        if (T != -1) {
          puts("-1");
          return 0;
        }
        T = i;
      }
    d[S]--;
    d[T]++;
    addEdge(T, S);
    flag = 1;
  } else {
    T = S;
    for (int i = 0; i <= n; i++)
      if (d[i] != 0) {
        puts("-1");
        return 0;
      }
  }
  int ans = gao();
  if (flag) ans--;
  printf("%d\n", ans);
  return 0;
}
