#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y;
  bool operator<(const node &b) const {
    if (b.y == y) return x < b.x;
    return y > b.y;
  }
} p[1000010];
int b[1000010], n;
int ask(int x) {
  int ret = 0;
  for (; x; x -= x & -x) ret = max(ret, b[x]);
  return ret;
}
void modify(int x, int d) {
  for (; x <= 1000010 - 10; x += x & -x) b[x] = max(b[x], d);
}
int main() {
  scanf("%d", &n);
  int x;
  for (int i = 1; i <= n; i++) scanf("%d", &x), p[x].x = i;
  for (int i = 1; i <= n; i++) scanf("%d", &x), p[x].y = i;
  sort(p + 1, p + n + 1);
  for (int i = 1; i <= n; i++) {
    int k = ask(p[i].x - 1);
    modify(p[i].x, k + 1);
  }
  printf("%d\n", ask(n));
  return 0;
}
