#include <bits/stdc++.h>
const int inf = (int)1e9;
const int mod = 1e9 + 7;
using namespace std;
int a[200011], b[200011], pos[200011], n;
bool check(int length) {
  int k = 1;
  for (int i = 1; i <= n - length; i++) {
    while (k <= n && pos[b[k]] > n - length) {
      k++;
    }
    if (b[k] != a[i]) {
      return 0;
    }
    k++;
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pos[a[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  int l = 0, r = n;
  int mn = n;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid) == 1) {
      r = mid - 1;
      mn = min(mn, mid);
    } else
      l = mid + 1;
  }
  cout << mn;
  return 0;
}
