#include <bits/stdc++.h>
using namespace std;
int N, a[1000111], b[1000111], d[1000111];
;
int B[1000111];
int get(int p) {
  int left = 0, right = N, middle;
  while (left < right) {
    middle = (left + right + 1) / 2;
    if (d[middle] < p)
      right = middle - 1;
    else
      left = middle;
  }
  return left;
}
void process(int x) {
  int p = B[x] + 1;
  d[get(p) + 1] = p;
}
int main(int argc, char *argv[]) {
  int i;
  scanf("%d", &N);
  for (i = 0; i < N; i++) scanf("%d", a + i);
  for (i = 0; i < N; i++) scanf("%d", b + i);
  for (i = 0; i < N; i++) B[b[i]] = i;
  d[0] = N;
  for (i = 0; i < N; i++) process(a[i]);
  i = 0;
  while (d[i]) i++;
  printf("%d\n", i - 1);
  return 0;
}
