#include <bits/stdc++.h>
#pragma comment(linker, "/stack:64000000")
using namespace std;
const int N = 2 * 100000 + 10;
int a[N];
int b[N];
int main() {
  memset(a, -1, sizeof(a));
  int n, t;
  scanf("%d", &n);
  for (int(i) = 0; (i) < (n); (i)++) {
    scanf("%d", &t);
    a[t] = i;
  }
  for (int(i) = 0; (i) < (n); (i)++) scanf("%d", &b[i]);
  int res = 0, lf = n;
  for (int i = n - 1; i >= 0; i--) {
    t = b[i];
    if (a[t] < lf) {
      res = max(res, i - a[t]);
      lf = a[t];
    } else {
      res = max(res, n - a[t]);
    }
  }
  printf("%d\n", res);
  return 0;
}
