#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const double eps = 1e-9;
int in1[1000100];
int in2[1000100];
int p[1000100];
int N;
int main() {
  scanf("%d", &N);
  for (int i = (0); i < (N); i++) {
    int t;
    scanf("%d", &t);
    --t;
    in1[t] = i;
  }
  for (int i = (0); i < (N); i++) {
    int t;
    scanf("%d", &t);
    --t;
    in2[t] = i;
  }
  for (int i = (0); i < (N); i++) p[in1[i]] = in2[i];
  set<pair<int, int> > ac = set<pair<int, int> >();
  int maxi = 0;
  for (int i = (0); i < (N); i++) {
    set<pair<int, int> >::iterator it = ac.upper_bound(pair<int, int>(p[i], 0)),
                                   it2;
    int nv = 1;
    if (it != ac.end()) {
      nv = 1 + it->second;
    }
    maxi = max(maxi, nv);
    if (it == ac.begin()) {
      ac.insert(pair<int, int>(p[i], nv));
    } else {
      it2 = it;
      --it2;
      vector<pair<int, int> > torem = vector<pair<int, int> >();
      while (true) {
        if (it2->second > nv) break;
        torem.push_back(*it2);
        if (it2 == ac.begin()) break;
        --it2;
      }
      for (int j = (0); j < (int((torem).size())); j++) ac.erase(torem[j]);
      ac.insert(pair<int, int>(p[i], nv));
    }
  }
  printf("%d\n", maxi);
  return 0;
}
