#include <bits/stdc++.h>
using namespace std;
int arr[1000005];
int m[1000005];
int len = 0;
int mx[1000005];
int mn[1000005];
int rlen[1000005];
int llen[1000005];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int tec;
    scanf("%d", &tec);
    m[tec] = i;
  }
  for (int i = 0; i < n; i++) {
    int tec;
    scanf("%d", &tec);
    tec = m[tec];
    arr[i] = tec;
  }
  memset(mx, -1, sizeof mx);
  memset(mn, -1, sizeof mn);
  int len = 0;
  for (int i = 0; i < n; i++) {
    int lb = -1, ub = len;
    while (ub - lb > 1) {
      int mb = (ub + lb) >> 1;
      if (mx[mb] < arr[i]) {
        ub = mb;
      } else {
        lb = mb;
      }
    }
    mx[ub] = arr[i];
    if (mx[len] != -1) {
      len++;
    }
    llen[i] = len;
  }
  len = 0;
  for (int i = n - 1; i > -1; i--) {
    int lb = -1, ub = len;
    while (ub - lb > 1) {
      int mb = (ub + lb) >> 1;
      if (mn[mb] >= arr[i]) {
        ub = mb;
      } else {
        lb = mb;
      }
    }
    mn[ub] = arr[i];
    if (mn[len] != -1) {
      len++;
    }
    rlen[i] = len;
  }
  int ret = 0;
  for (int i = 0; i < n; i++) {
    ret = max(ret, max(rlen[i], llen[i]));
  }
  cout << ret << endl;
  return 0;
}
