#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1001, INF = 1e8;
int n, a[MAXN], b[MAXN], first[MAXN], next[MAXN], last[MAXN], to[MAXN], tot, k,
    ans = INF, xor_a;
int f[MAXN][MAXN], visited[MAXN], no[MAXN];
void add(int x, int y) {
  ++tot;
  if (!first[x])
    first[x] = tot;
  else
    next[last[x]] = tot;
  last[x] = tot;
  to[tot] = y;
}
void dfs(int now, int sum, int steps) {
  if (f[now][sum] <= steps && f[now][sum] != 0) return;
  if (sum == k) {
    if (steps < ans) ans = steps;
    return;
  }
  f[now][sum] = steps;
  int ss = 0;
  for (int i = first[now]; i; i = next[i]) {
    if (!visited[to[i]]) {
      ss = 1;
      visited[to[i]] = true;
    }
    if (first[to[i]] == 0) ss = 0;
    dfs(to[i], sum + ss, steps + 1);
    if (ss) {
      visited[i] = false;
    }
  }
}
int main() {
  scanf("%d", &n);
  bool bo = false;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    if (a[i] != 1 && a[i] != 0) bo = true;
    xor_a ^= a[i];
  }
  k = n;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &b[i]);
    if (a[i] != b[i])
      add(b[i], a[i]);
    else
      --k;
  }
  if (bo == false) {
    ans = 0;
    for (int i = 1; i <= n; ++i)
      if (a[i] != b[i]) ++ans;
    printf("%d", ans);
    return 0;
  };
  visited[xor_a] = true;
  dfs(xor_a, 1, 1);
  if (ans != INF)
    printf("%d", ans);
  else
    printf("-1");
  return 0;
}
