#include <bits/stdc++.h>
using namespace std;
int pos[1000005];
int seq[1000005];
struct Node {
  int left, right;
  int max;
} node[1000005 * 4];
void init(int left, int right, int pos) {
  node[pos].left = left;
  node[pos].right = right;
  node[pos].max = 0;
  if (left == right) {
    return;
  }
  int mid = (left + right) >> 1;
  init(left, mid, pos * 2);
  init(mid + 1, right, pos * 2 + 1);
}
int getMax(int left, int right, int pos) {
  if (left <= node[pos].left && node[pos].right <= right) {
    return node[pos].max;
  } else {
    int mid = (node[pos].left + node[pos].right) >> 1;
    int ret = 0;
    if (left <= mid) {
      ret = max(ret, getMax(left, right, pos * 2));
    }
    if (mid + 1 <= right) {
      ret = max(ret, getMax(left, right, pos * 2 + 1));
    }
    return ret;
  }
}
void insert(int left, int num, int pos) {
  if (left <= node[pos].left && node[pos].right <= left) {
    node[pos].max = num;
  } else {
    int mid = (node[pos].left + node[pos].right) >> 1;
    if (left <= mid) {
      insert(left, num, pos * 2);
    } else {
      insert(left, num, pos * 2 + 1);
    }
    node[pos].max = max(node[pos * 2].max, node[pos * 2 + 1].max);
  }
}
int getNum(int n) {
  init(1, n, 1);
  for (int i = 1; i <= n; i++) {
    int maxNum = getMax(seq[i] + 1, n, 1);
    int dp = maxNum + 1;
    insert(seq[i], dp, 1);
  }
  return getMax(1, n, 1);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int t;
    scanf("%d", &t);
    pos[t] = i;
  }
  for (int i = 1; i <= n; i++) {
    int t;
    scanf("%d", &t);
    seq[i] = pos[t];
  }
  printf("%d\n", getNum(n));
  return 0;
}
