#include <bits/stdc++.h>
using namespace std;
const int inf = (1 << 30);
const long long INF = 1LL << 62;
int best, d[2 * 1100000], n, i, j, f[1100000][2], e[1100000], x;
int get(int l, int r) {
  int res = 0;
  l += n - 1, r += n - 1;
  while (l <= r) {
    res = ((res) < (d[l]) ? (d[l]) : (res));
    res = ((res) < (d[r]) ? (d[r]) : (res));
    l = (l + 1) / 2;
    r = (r - 1) / 2;
  }
  return res;
}
void upd(int v, int w) {
  v += n - 1;
  d[v] = w;
  best = ((best) < (w) ? (w) : (best));
  while (v > 1) {
    v /= 2;
    d[v] = ((d[v * 2]) < (d[v * 2 + 1]) ? (d[v * 2 + 1]) : (d[v * 2]));
  }
}
int main() {
  scanf("%d", &n);
  for (j = 0; j < 2; j++)
    for (i = 1; i <= n; i++) {
      scanf("%d", &x);
      f[x][j] = i;
    }
  for (i = 1; i <= n; i++) e[f[i][0]] = f[i][1];
  for (i = 1; i <= n; i++) {
    x = e[i];
    upd(x, get(x + 1, n) + 1);
  }
  printf("%d", best);
  return 0;
}
