#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000005;
int N;
int bit[MAX];
int posA[MAX], posB[MAX];
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= (int)N; i++) {
    int x;
    scanf("%d", &x);
    posA[i] = x;
  }
  for (int i = 1; i <= (int)N; i++) {
    int x;
    scanf("%d", &x);
    posB[x] = i;
  }
  int best = 0;
  for (int i = N; i >= 1; i--) {
    int pos = posB[posA[i]];
    int q = 0;
    for (int j = pos; j; j -= j & -j) q = max(q, bit[j]);
    best = max(best, q + 1);
    for (int j = pos; j <= N; j += j & -j) bit[j] = max(bit[j], q + 1);
  }
  printf("%d\n", best);
}
