#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 7;
const long long M = 1e3 + 7;
const long long INF = 1e9 + 7;
const long long Mod = 1e9 + 7;
long long n;
namespace Fen {
long long F[N];
void Upd(long long id, long long x) {
  for (; id <= n; id |= id + 1) {
    F[id] = max(F[id], x);
  }
}
long long Max(long long x) {
  long long res = 0;
  for (; x > 0; x = (x & (x + 1)) - 1) {
    res = max(res, F[x]);
  }
  return res;
}
}  // namespace Fen
long long a[N], p[N];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n;
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
    p[a[i]] = i + 1;
  }
  for (long long i = 0; i < n; ++i) {
    long long x;
    cin >> x;
    a[p[x]] = i + 1;
  }
  reverse(a + 1, a + n + 1);
  long long res = 0;
  for (long long i = 1; i <= n; ++i) {
    long long x = Fen ::Max(a[i] - 1);
    Fen ::Upd(a[i], x + 1);
  }
  cout << Fen ::Max(n) << '\n';
}
