#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1, N2 = 1e5 + 1, M = 2 * N2,
          SEGN = 2 << (32 - __builtin_clz(N));
int n, a[N], b[N], a2[N];
int freq[N];
bool valid(int x) {
  for (int i = 1; i <= n; i++) freq[i] = 0;
  for (int i = 0; i < x; i++) freq[a[n - i - 1]] = 1;
  int shifting = 0;
  bool flag = 1;
  for (int i = 0; i < n; i++) {
    if (freq[b[i]]) {
      shifting++;
      continue;
    }
    if (a2[b[i]] + shifting == i)
      continue;
    else {
      flag = 0;
      break;
    }
  }
  return flag;
}
int solve() {
  int st = 0, ed = n, ans = 0;
  while (st <= ed) {
    int mid = (st + ed) / 2;
    if (valid(mid))
      ed = mid - 1, ans = mid;
    else
      st = mid + 1;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a2[a[i]] = i;
  }
  for (int i = 0; i < n; i++) cin >> b[i];
  cout << solve();
}
