#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 200005;
int n;
int a[maxn],b[maxn];
int l[maxn],r[maxn];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
map<int,int> pos;
for(int i=1;i<=n;i++){
cin>>a[i];
pos[a[i]]=i;
}
for(int i=1;i<=n;i++) cin>>b[i];
stack<int> st;
for(int i=1;i<=n;i++){
while(!st.empty() && a[st.top()]<a[i]) st.pop();
if(st.empty()) l[i]=1;
else l[i]=st.top()+1;
st.push(i);
}
while(!st.empty()) st.pop();
for(int i=n;i>=1;i--){
while(!st.empty() && a[st.top()]<a[i]) st.pop();
if(st.empty()) r[i]=n;
else r[i]=st.top()-1;
st.push(i);
}
int ans=0;
for(int i=1;i<=n;i++){
int x=b[i];
if(!pos.count(x)) continue;
int j=pos[x];
if(i>=l[j] && i<=r[j]) ans++;
}
cout<<ans<<"\n";
return 0;
}