#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define pp pair<ll,ll>
#define pq_min priority_queue<ll,vector<ll>,greater<ll>>
#define pq_max priority_queue<ll>
#define forx(i,a,b) for(ll i=0;i<a;i++)
#define ld long double
#define autt set<ll>::iterator
const ll mod=1e9 + 7;
ll power(ll a,ll b)
{
	a=a%mod;
	ll ans=1;
	while(b>0)
	{
		if(b%2==1)
		{
			ans=(ans*a)%mod;
		}
		b=b/2;
		a=(a*a)%mod;
	}
	return ans;
}

int main()
{
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   ll n;
   cin>>n;
   ll a[n+1];
   ll b[n+1];
   for(ll i=1;i<=n;i++)
   {
   	  cin>>a[i];
   }
   for(ll i=1;i<=n;i++)
   {
   	 cin>>b[i];
   }
   ll f=1;
   map<ll,ll> h;
   ll c=0;
   for(ll i=1;i<n;i++)
   {
   	  if(a[f]!=b[i] || h[a[f]]!=0)
   	  {
   	  	 c++;
   	  	 if(h[a[f]]!=0)
   	  	 {
   	  	 	f++;
   	  	 	i--;
		 }
	  }
	  else
	  {
	  	 h[a[f]]=1;
	  	 f++;
	  }
   }
   cout<<c<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define pp pair<ll,ll>
#define pq_min priority_queue<ll,vector<ll>,greater<ll>>
#define pq_max priority_queue<ll>
#define forx(i,a,b) for(ll i=0;i<a;i++)
#define ld long double
#define autt set<ll>::iterator
const ll mod=1e9 + 7;
ll power(ll a,ll b)
{
	a=a%mod;
	ll ans=1;
	while(b>0)
	{
		if(b%2==1)
		{
			ans=(ans*a)%mod;
		}
		b=b/2;
		a=(a*a)%mod;
	}
	return ans;
}

int main()
{
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   ll n;
   cin>>n;
   ll a[n+1];
   for(ll i=1;i<=n;i++)
   {
   	  cin>>a[i];
   }
   ll c=0;
   ll t=1;
   map<ll,ll> f;
   for(ll i=1;i<=n;i++)
   {
   	  ll x;
   	  cin>>x;
   	  if(a[t]==x)
   	  {
   	  	 t++;
	  }
	  else if(f[a[t]]!=0)
	  {
	  	 t++;
	  }
	  else
	  {
	  	 f[x]++;
	  	 c++;
	  }
   }
   cout<<c<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define pp pair<ll,ll>
#define pq_min priority_queue<ll,vector<ll>,greater<ll>>
#define pq_max priority_queue<ll>
#define forx(i,a,b) for(ll i=0;i<a;i++)
#define ld long double
#define autt set<ll>::iterator
const ll mod=1e9 + 7;
ll power(ll a,ll b)
{
	a=a%mod;
	ll ans=1;
	while(b>0)
	{
		if(b%2==1)
		{
			ans=(ans*a)%mod;
		}
		b=b/2;
		a=(a*a)%mod;
	}
	return ans;
}

int main()
{
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   ll n;
   cin>>n;
   ll a[n+1];
   for(ll i=1;i<=n;i++)
   {
   	  ll x;
   	  cin>>x;
   	  a[x]=i;
   }
   ll c=0;
   ll f=0;
   for(ll i=1;i<=n;i++)
   {
   	  ll x;
   	  cin>>x;
   	  if(a[x] > i)
   	  {
   	  	 c++;
   	  	 f=1;
	  }
	  if(a[x]==i && f==1)
	  {
	  	 c++;
	  }
   }

   cout<<c;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define pp pair<ll,ll>
#define pq_min priority_queue<ll,vector<ll>,greater<ll>>
#define pq_max priority_queue<ll>
#define forx(i,a,b) for(ll i=0;i<a;i++)
#define ld long double
#define autt set<ll>::iterator
const ll mod=1e9 + 7;
ll power(ll a,ll b)
{
	a=a%mod;
	ll ans=1;
	while(b>0)
	{
		if(b%2==1)
		{
			ans=(ans*a)%mod;
		}
		b=b/2;
		a=(a*a)%mod;
	}
	return ans;
}

int main()
{
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   ll n;
   cin>>n;
   ll a[n+1];
   ll b[n+1];
   for(ll i=1;i<=n;i++)
   {
   	  cin>>a[i];
   }
   for(ll i=1;i<=n;i++)
   {
   	 cin>>b[i];
   }
   ll f=1;
   map<ll,ll> h;
   ll c=0;
   for(ll i=1;i<n;i++)
   {
   	  if(a[f]!=b[i] || h[a[f]]!=0)
   	  {
   	  	 c++;
   	  	 if(h[a[f]]!=0)
   	  	 {
   	  	 	f++;
		 }
	  }
	  else
	  {
	  	 h[a[f]]=1;
	  	 f++;
	  }
   }
   cout<<c<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define pp pair<ll,ll>
#define pq_min priority_queue<ll,vector<ll>,greater<ll>>
#define pq_max priority_queue<ll>
#define forx(i,a,b) for(ll i=0;i<a;i++)
#define ld long double
#define autt set<ll>::iterator
const ll mod=1e9 + 7;
ll power(ll a,ll b)
{
	a=a%mod;
	ll ans=1;
	while(b>0)
	{
		if(b%2==1)
		{
			ans=(ans*a)%mod;
		}
		b=b/2;
		a=(a*a)%mod;
	}
	return ans;
}

int main()
{
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   ll n;
   cin>>n;
   ll a[n+1];
   for(ll i=1;i<=n;i++)
   {
   	  cin>>a[i];
   }
   ll c=0;
   ll t=1;
   map<ll,ll> f;
   for(ll i=1;i<=n;i++)
   {
   	  ll x;
   	  cin>>x;
   	  if(a[t]==x)
   	  {
   	  	 t++;
	  }
	  else if(f[a[t]]!=0)
	  {
	  	 t++;
	  	 i--;
	  }
	  else
	  {
	  	 f[x]++;
	  	 c++;
	  }
   }
   cout<<c<<endl;

}


#include <iostream>
#include <sstream>
#include <vector>
#include <utility>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <string.h>
#include <iomanip>
#include <queue>
#include <cmath>#define ll long long
#define ld long double
#define lp(i,n) for (int i = 0; i < n; i++)
#define rep(i, v) for (int i = 0; i < int(v.size()); i++)
using namespace std;
const int N=3e3+3;
long long ans,cnt,sum;
void solve()
{

}
map<pair<int,int>,bool> exist;
int x[N],y[N];
int main()
{
    queue <int> q1,q2;
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int a;
        cin>>a;
        q1.push(a);
    }
    for(int i=0;i<n;i++){
        int a;
        cin>>a;
        q2.push(a);
    }
    vector <bool> taken(n+1); /// false initially
    while(!q2.empty()){
        if(taken[q1.front()]){
            q1.pop();
            continue;
        }
        if(q1.front()==q2.front()){
            q1.pop();
            q2.pop();
            continue;
        }
        cnt++;
        taken[q2.front()]=true;
        q2.pop();
    }
    cout<<cnt;
    return 0;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#define ll long long
#define lp(i,n) for (int i = 0; i < n; i++)
#define rep(i, v) for (int i = 0; i < int(v.size()); i++)
using namespace std;
int main()
{
   ll n,ans=0;
   cin>>n;
   ll enter[n],exit[n],pos[n+1000];
   lp(i,n){
    cin>>enter[i];
    pos[enter[i]]=i+1;
   }
   lp(i,n)
    {
        cin>>exit[i];
        if(pos[exit[i]]!=i+1){
            ans++;
            int x=pos[enter[i]];
            lp(j,x)
                pos[enter[j]]++;
        }
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#define ll long long
#define lp(i,n) for (int i = 0; i < n; i++)
#define rep(i, v) for (int i = 0; i < int(v.size()); i++)
using namespace std;
int main()
{
   ll n,ans=0;
   cin>>n;
   ll enter[n],exit[n],pos[n+1];
   lp(i,n){
    cin>>enter[i];
    pos[enter[i]]=i+1;
   }
   lp(i,n)
    {
        cin>>exit[i];
        if(pos[exit[i]]!=i+1){
            ans++;
            ll x=pos[exit[i]];
            for(int j=0;j<n;j++)
                if(pos[enter[j]]<x)
                    pos[enter[j]]++;
        }
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#define ll long long
#define lp(i,n) for (int i = 0; i < n; i++)
#define rep(i, v) for (int i = 0; i < int(v.size()); i++)
using namespace std;
int main()
{
   ll n,ans=0;
   cin>>n;
   ll enter[n],exit[n],pos[n];
   lp(i,n){
    cin>>enter[i];
    pos[enter[i]]=i+1;
   }
   lp(i,n)
    {
        cin>>exit[i];
        if(pos[exit[i]]!=i+1){
            ans++;
            int x=pos[enter[i]];
            lp(j,x)
                pos[enter[j]]++;
        }
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <utility>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <string.h>
#include <iomanip>
#include <cmath>
#define all(v) v.begin(), v.end()
#define ll long long
#define MP make_pair
#define sz(v) v.size()
#define PI 3.14159265358979323846
#define lp(i, n) for (long long i = 0; i < n; i++)
#define rep(i, v) for (int i = 0; i < int(v.size()); i++)
using namespace std;
ll sum, cnt,cnt2, ans = 3;
const int N=1e5+9;
bool fined[N];
int main()
{
	int n;
	cin>>n;
	vector<int> in(n),out(n);
	for(int& i:in)cin>>i;
	for(int& i:out)cin>>i;
	for(int i=0,j=0;i<n;++i)
	{
		while(fined[in[j]])
			j++;
		if(in[j]!=out[i])
		{
			cnt++;
			fined[out[i]]=1;
		}
		else
			j++;
	}
	cout<<cnt;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <utility>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <string.h>
#include <iomanip>
#include <cmath>
#define all(v) v.begin(), v.end()
#define ll long long
#define MP make_pair
#define sz(v) v.size()
#define PI 3.14159265358979323846
#define lp(i, n) for (long long i = 0; i < n; i++)
#define rep(i, v) for (int i = 0; i < int(v.size()); i++)
using namespace std;
ll sum, cnt,cnt2, ans = 3;
const int N=1e5+9;
bool fined[N];
int main()
{
	int n;
	cin>>n;
	vector<int> in(n),out(n);
	for(int& i:in)cin>>i;
	for(int& i:out)cin>>i;
	for(int i=0,j=0;i<n;++i)
	{
		if(fined[in[j]])
			j++;
		if(in[j]!=out[i])
		{
			cnt++;
			fined[out[i]]=1;
		}
		else
			j++;
	}
	cout<<cnt;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#define ll long long
#define lp(i,n) for (int i = 0; i < n; i++)
#define rep(i, v) for (int i = 0; i < int(v.size()); i++)
using namespace std;
int main()
{
   int n,ans=0;
   cin>>n;
   int enter[n],exit[n],pos[n];
   lp(i,n){
    cin>>enter[i];
    pos[enter[i]]=i+1;
   }
   lp(i,n)
    {
        cin>>exit[i];
        if(pos[exit[i]]!=i+1){
            ans++;
            int x=pos[enter[i]];
            lp(j,x)
                pos[enter[j]]++;
        }
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
queue<int> st;
const int N=1e7+7;
int vis[N];
int arr[N];
int main()
{
    int n; cin>>n;
    for(int i=1;i<=n;i++){
        int temp; cin>>temp;
        st.push(temp);
    }
    int num=0;
    for(int i=1;i<=n;i++){
        int temp; cin>>temp;
        if(!st.empty()){
            if(temp==st.front()) st.pop();
            else if(temp!=st.front()){
                num++;
                vis[temp]=1;
            }
            while(vis[st.front()]==1) st.pop();
        }
        else
            continue;
    }
    cout<<num<<endl;
}


#include<bits/stdc++.h>
using namespace std;
queue<int> st;
const int N=1e7+7;
int vis[N];
int arr[N];
int main()
{
    int n; cin>>n;
    for(int i=1;i<=n;i++){
        int temp; cin>>temp;
        st.push(temp);
    }
    int num=0;
    for(int i=1;i<=n;i++){
        int temp; cin>>temp;
        if(!st.empty()){
            if(temp==st.front()) st.pop();
            else if(temp!=st.front()){
                num++;
                vis[temp]=1;
            }
            while(1){
                if(st.empty()||vis[st.front()]==0) break;
                else st.pop();
            }
        }
        else
            continue;
    }
    cout<<num<<endl;
}


#include<bits/stdc++.h>
using namespace std;
queue<int> st;
const int N=1e7+7;
int vis[N];
int arr[N];
int main()
{
    int n; cin>>n;
    for(int i=1;i<=n;i++){
        int temp; cin>>temp;
        st.push(temp);
    }
    int num=0;
    for(int i=1;i<=n;i++){
        int temp; cin>>temp;
        if(!st.empty()){
            if(temp==st.front()) st.pop();
            else if(temp!=st.front()){
                num++;
                vis[temp]=1;
            }
            while(vis[st.front()]==1) st.pop();
        }
    }
    cout<<num<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
queue<int> st;
const int N=1e6+7;
int vis[N];
int arr[N];
int main()
{
    int n; cin>>n;
    for(int i=1;i<=n;i++){
        int temp; cin>>temp;
        st.push(temp);
    }
    int num=0;
    for(int i=1;i<=n;i++){
        int temp; cin>>temp;
        if(!st.empty()){
            if(temp==st.front()) st.pop();
            else if(temp!=st.front()){
                num++;
                vis[temp]=1;
            }
            while(vis[st.front()]==1) st.pop();
        }
    }
    cout<<num<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
queue<int> st;
const int N=1e7+7;
int vis[N];
int arr[N];
int main()
{
    int n; cin>>n;
    for(int i=1;i<=n;i++){
        int temp; cin>>temp;
        st.push(temp);
    }
    int num=0;
    for(int i=1;i<=n;i++){
        int temp; cin>>temp;
        if(!st.empty()){
            if(temp==st.front()) st.pop();
            else if(temp!=st.front()){
                num++;
                vis[temp]=1;
            }
            while(vis[st.front()]==1) st.pop();
        }
    }
    cout<<num<<endl;
}


#include<bits/stdc++.h>
using namespace std;
queue<int> st;
const int N=1e5+7;
int vis[N];
int arr[N];
int main()
{
    int n; cin>>n;
    for(int i=1;i<=n;i++){
        int temp; cin>>temp;
        st.push(temp);
    }
    int num=0;
    for(int i=1;i<=n;i++){
        int temp; cin>>temp;
        if(!st.empty()){
            if(temp==st.front()) st.pop();
            else if(temp!=st.front()){
                num++;
                vis[temp]=1;
            }
            while(vis[st.front()]==1) st.pop();
        }
    }
    cout<<num<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
#define forr(i,a,n) for(int i=a; i<n; i++)
#define loop(i,a,n)	for(int i=a; i>=n; i--)
#define SPEED ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(0)
#define in cin>>
#define out cout<<
#define endl <<"\n"
#define ll long long
#define remove(a,b) a.erase(std::remove(a.begin(),a.end(),b),a.end())
#define runn(it,a) for(auto it = a.begin(); it!= a.end(); it++)

int main()
{
	SPEED;
	ll int a;
	in a;
	vector<ll int> v(a);
	vector<ll int> u(a);
	vector<ll int> w;
	ll int b[a],d[a];
	forr(i,0,a)
	{
		int x;
		in x;
		v[x-1]=i+1;
	}
	forr(i,0,a)
	{
		int x;
		in x;
		w.push_back(i+1);
		u[i]=v[x-1];
	}
	int e=0,ans=0;
	forr(i,0,a-1)
	{
		if(u[i]==w[e])
		{
			w[e]=100000000;
		}
		else
		{
			w[u[i]-1]=100000000;
			ans++;
		}
		sort(w.begin(),w.end());
	}
	out ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define forr(i,a,n)	for(int i=a; i<n; i++)
#define loop(i,a,n)	for(int i=a; i>=n; i--)

int main()
{
	ios_base :: sync_with_stdio(false),cin.tie(NULL),cout.tie(0);

	int a;
	cin>>a;
	int b[a],c[a],d[a],e[a],f[a],ct=0,mx=-2;
	forr(i,0,a)
	{
		cin>>b[i];
		b[i]--;
		d[i]=b[i];
	}
	forr(i,0,a)
	{
		cin>>c[i];
		e[c[i]-1]=i;
	}
	forr(i,0,a)
		d[i]=e[d[i]],mx=max(mx,d[i]),f[i]=mx;
	forr(i,0,a)
	{
		if(d[i]<f[i])
			ct++;
	}
	cout<<ct;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define forr(i,a,n) for(int i=a; i<n; i++)
#define loop(i,a,n)	for(int i=a; i>=n; i--)
#define SPEED ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(0)
#define in cin>>
#define out cout<<
#define endl <<"\n"
#define ll long long
#define remove(a,b) a.erase(std::remove(a.begin(),a.end(),b),a.end())
#define runn(it,a) for(auto it = a.begin(); it!= a.end(); it++)

int main()
{
	SPEED;
	int a;
	in a;
	vector<int> v(a);
	int b[a],d[a];
	forr(i,0,a)
	{
		int x;
		in x;
		v[x-1]=i+1;
	}
	forr(i,0,a)
	{
		int x;
		in x;
		b[i]=v[x-1];
		d[i]=b[i];
	}
	int k=0,ct=0;
	forr(i,0,a)
	{
		k=i;
		forr(j,i,a)
		{
			if(b[j]<b[k])	{k=j;}
		}
		if(i!=k)	ct++;
		swap(b[i],b[k]);
	}
	int kk=0,ctt=0;
	if(d[0]!=1)
	{
		forr(i,1,a)
		{
			kk=i;
			forr(j,i,a)
			{
				if(d[j]<d[kk])	{kk=j;}
			}
			if(i!=kk)	ctt++;
			swap(d[i],d[kk]);
		}
		ctt++;
		out min(ctt,ct);
		return 0;
	}
	out ct;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define forr(i,a,n) for(int i=a; i<n; i++)
#define loop(i,a,n)	for(int i=a; i>=n; i--)
#define SPEED ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(0)
#define in cin>>
#define out cout<<
#define endl <<"\n"
#define ll long long
#define remove(a,b) a.erase(std::remove(a.begin(),a.end(),b),a.end())
#define runn(it,a) for(auto it = a.begin(); it!= a.end(); it++)

int main()
{
	SPEED;
	ll int a;
	in a;
	vector<ll int> v(a);
	vector<ll int> u(a);
	vector<pair<int,bool>> w;
	ll int b[a],d[a];
	forr(i,0,a)
	{
		int x;
		in x;
		v[x-1]=i+1;
	}
	forr(i,0,a)
	{
		int x;
		in x;
		w.push_back(make_pair(i+1,true));
		u[i]=v[x-1];
	}
	int e=0,ans=0;
	forr(i,0,a-1)
	{
		sort(w.begin(),w.end());
		if(u[i]==w[e].first)
		{
			w[e].second=false;
			w[e].first=1000000;
		}
		else
		{
			w[u[i]-1].second=false;
			w[u[i]-1].first=1000000;
			ans++;
		}
	}
	out ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define forr(i,a,n) for(int i=a; i<n; i++)
#define loop(i,a,n)	for(int i=a; i>=n; i--)
#define SPEED ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(0)
#define in cin>>
#define out cout<<
#define endl <<"\n"
#define ll long long
#define remove(a,b) a.erase(std::remove(a.begin(),a.end(),b),a.end())
#define runn(it,a) for(auto it = a.begin(); it!= a.end(); it++)

int main()
{
	SPEED;
	ll int a;
	in a;
	vector<ll int> v(a);
	ll int b[a],d[a];
	forr(i,0,a)
	{
		int x;
		in x;
		v[x-1]=i+1;
	}
	forr(i,0,a)
	{
		int x;
		in x;
		b[i]=v[x-1];
		d[i]=b[i];
	}
	int k=0,ct=0,e=0;
	forr(i,0,a)
	{
		k=i;
		forr(j,i,a)
		{
			if(b[j]<b[k])	{k=j;}
		}
		if(i!=k)	ct++;
		swap(b[i],b[k]);
	}
	int kk=0,ctt=0;
	forr(i,0,a)
	{
		if(d[i]==i+1)	continue;
		else {e=i; break;}
	}
	if(d[e]==a)
	{
		forr(i,e+1,a)
		{
			kk=i;
			forr(j,i,a)
			{
				if(d[j]<d[kk])	{kk=j;}
			}
			if(i!=kk)	ctt++;
			swap(d[i],d[kk]);
		}
		ctt++;
		out min(ctt,ct);
		return 0;
	}
	out ct;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define forr(i,a,n) for(int i=a; i<n; i++)
#define loop(i,a,n)	for(int i=a; i>=n; i--)
#define SPEED ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(0)
#define in cin>>
#define out cout<<
#define endl <<"\n"
#define ll long long
#define remove(a,b) a.erase(std::remove(a.begin(),a.end(),b),a.end())
#define runn(it,a) for(auto it = a.begin(); it!= a.end(); it++)

int main()
{
	SPEED;
	int a;
	in a;
	vector<int> v(a);
	int b[a],d[a];
	forr(i,0,a)
	{
		int x;
		in x;
		v[x-1]=i+1;
	}
	forr(i,0,a)
	{
		int x;
		in x;
		b[i]=v[x-1];
		d[i]=b[i];
	}
	int k=0,ct=0;
	forr(i,0,a)
	{
		k=i;
		forr(j,i,a)
		{
			if(b[j]<b[k])	{k=j;}
		}
		if(i!=k)	ct++;
		swap(b[i],b[k]);
	}
	int kk=0,ctt=0;
	if(d[0]==a)
	{
		forr(i,1,a)
		{
			kk=i;
			forr(j,i,a)
			{
				if(d[j]<d[kk])	{kk=j;}
			}
			if(i!=kk)	ctt++;
			swap(d[i],d[kk]);
		}
		ctt++;
		out min(ctt,ct);
		return 0;
	}
	out ct;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define forr(i,a,n) for(int i=a; i<n; i++)
#define loop(i,a,n)	for(int i=a; i>=n; i--)
#define SPEED ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(0)
#define in cin>>
#define out cout<<
#define endl <<"\n"
#define ll long long
#define remove(a,b) a.erase(std::remove(a.begin(),a.end(),b),a.end())
#define runn(it,a) for(auto it = a.begin(); it!= a.end(); it++)

int main()
{
	SPEED;
	ll int a;
	in a;
	vector<ll int> v(a);
	vector<ll int> u(a);
	vector<ll int> w;
	ll int b[a],d[a];
	forr(i,0,a)
	{
		int x;
		in x;
		v[x-1]=i+1;
	}
	forr(i,0,a)
	{
		int x;
		in x;
		w.push_back(i+1);
		u[i]=v[x-1];
	}
	int e=0,ans=0;
	forr(i,0,a-1)
	{
		if(u[i]==w[0])
		{
			remove(w,u[i]);
		}
		else
		{
			remove(w,w[u[i]-1]);
			ans++;
		}
	}
	out ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define forr(i,a,n) for(int i=a; i<n; i++)
#define loop(i,a,n)	for(int i=a; i>=n; i--)
#define SPEED ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(0)
#define in cin>>
#define out cout<<
#define endl <<"\n"
#define ll long long
#define remove(a,b) a.erase(std::remove(a.begin(),a.end(),b),a.end())
#define runn(it,a) for(auto it = a.begin(); it!= a.end(); it++)

int main()
{
	SPEED;
	ll int a;
	in a;
	vector<ll int> v(a);
	vector<ll int> u(a);
	vector<pair<int,bool>> w;
	ll int b[a],d[a];
	forr(i,0,a)
	{
		int x;
		in x;
		v[x-1]=i+1;
	}
	forr(i,0,a)
	{
		int x;
		in x;
		w.push_back(make_pair(i+1,true));
		u[i]=v[x-1];
	}
	int e=0,ans=0;
	forr(i,0,a)
	{
		forr(j,0,w.size())
		{
			if(w[j].second==true)
			{
				e=j;
				break;
			}
		}
		//out u[i]<<' '<<w[e].first<<' '<<w[e].second endl;
		if(u[i]==w[e].first)
		{
			w[e].second=false;
		}
		else
		{
			w[u[i]-1].second=false;
			//out u[i] endl;
			ans++;
		}
	}
	out ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > pi;
typedef pair< ll,ll > pll;
typedef vector <pi> vii;
typedef vector < ll > vll;
typedef vector < pll > vpll;
typedef vector< vll > vvll;
typedef pair < double,double > pd;
#define size(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) (c.find(x) != c.end())
#define cpresent(c,x) (find(all(c),x) != c.end())
#define REP(i,a,b) for(int i =a;i<b;i++)
#define F first
#define S second
#define cp(a,b) ( (conj(a)*b).imag() )
#define dprod(a,b)  ( (conj(a)*b).real() )
#define M 1000000007

int absolute(int a , int b){
	return max(a,b)-min(a,b);
}

class Union_Find{
	vll rank;
	vll parent;
	ll size;
	public:
		int find_set(ll i){ return (parent[i] == i) ? i : parent[i] = find_set(parent[i]);}
		bool is_same(ll a,ll b){ return find_set(a) == find_set(b);}
		void join(ll a, ll b){
			if(is_same(a,b))
				return;
			a = find_set(a);
			b = find_set(b);
			if(rank[a] > rank[b] )
				parent[b] = a;
			else if(rank[b] > rank[a])
				parent[a] = b;
			else{
				parent[a] = b;
				rank[b]++;
			}
		}
		void clear(ll n){
			size = n;
			rank.resize(size,0);
			parent.resize(size,0);
			REP(i,0,size)
				parent[i] = i;
		}
		Union_Find(ll n){
			size = n;
			rank.resize(size,0);
			parent.resize(size,0);
			REP(i,0,size)
				parent[i] = i;
		}
	private:

};

const int MAX = 10e5+3;
const ll INF = 10e9+3;
const ll MOD = 1000000007;

const double EPS = 10e-10;

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	 //freopen ("template.txt","w",stdout);
	 //freopen ("template.out","r",stdin);
	int n;
	cin >> n;
	vll a(n+1);
	vll b(n);
	REP(i,0,n){
		ll t;
		cin >> t;
		a[t] = i;
	}
	REP(i,0,n){
		ll t;
		cin >> t;
		b[a[t]]=i+1;
	}
	ll big = 0;
	ll ans = 0;
	REP(i,0,n){
		big = max(big,b[i]);
		if(b[i]<big)
			ans++;
	}
	cout << ans << "\n";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > pi;
typedef pair< ll,ll > pll;
typedef vector <pi> vii;
typedef vector < ll > vll;
typedef vector < pll > vpll;
typedef vector< vll > vvll;
typedef pair < double,double > pd;
#define size(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) (c.find(x) != c.end())
#define cpresent(c,x) (find(all(c),x) != c.end())
#define REP(i,a,b) for(int i =a;i<b;i++)
#define F first
#define S second
#define cp(a,b) ( (conj(a)*b).imag() )
#define dprod(a,b)  ( (conj(a)*b).real() )
#define M 1000000007

int absolute(int a , int b){
	return max(a,b)-min(a,b);
}

class Union_Find{
	vll rank;
	vll parent;
	ll size;
	public:
		int find_set(ll i){ return (parent[i] == i) ? i : parent[i] = find_set(parent[i]);}
		bool is_same(ll a,ll b){ return find_set(a) == find_set(b);}
		void join(ll a, ll b){
			if(is_same(a,b))
				return;
			a = find_set(a);
			b = find_set(b);
			if(rank[a] > rank[b] )
				parent[b] = a;
			else if(rank[b] > rank[a])
				parent[a] = b;
			else{
				parent[a] = b;
				rank[b]++;
			}
		}
		void clear(ll n){
			size = n;
			rank.resize(size,0);
			parent.resize(size,0);
			REP(i,0,size)
				parent[i] = i;
		}
		Union_Find(ll n){
			size = n;
			rank.resize(size,0);
			parent.resize(size,0);
			REP(i,0,size)
				parent[i] = i;
		}
	private:

};

const int MAX = 10e5+3;
const ll INF = 10e9+3;
const ll MOD = 1000000007;

const double EPS = 10e-10;

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	 //freopen ("template.txt","w",stdout);
	 //freopen ("template.out","r",stdin);
	int n;
	cin >> n;
	vi a(n);
	vi b(n);
	vector < bool > seen(n,false);
	REP(i,0,n)
		cin >> a[i];
	REP(i,0,n)
		cin >> b[i];
	reverse(all(a));
	reverse(all(b));
	int p1 = 0;
	int p2 = 0;
	int ans = 0;
	while(p2<n && p1<n){
		if(seen[p2]){
			p2++;
			continue;
		}
		while(a[p1]!=b[p2]){
			seen[a[p1]] = true;
			ans++;
			p1++;
		}
		p1++;p2++;
	}
	cout << ans << "\n";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > pi;
typedef pair< ll,ll > pll;
typedef vector <pi> vii;
typedef vector < ll > vll;
typedef vector < pll > vpll;
typedef vector< vll > vvll;
typedef pair < double,double > pd;
#define size(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) (c.find(x) != c.end())
#define cpresent(c,x) (find(all(c),x) != c.end())
#define REP(i,a,b) for(int i =a;i<b;i++)
#define F first
#define S second
#define cp(a,b) ( (conj(a)*b).imag() )
#define dprod(a,b)  ( (conj(a)*b).real() )
#define M 1000000007

int absolute(int a , int b){
	return max(a,b)-min(a,b);
}

class Union_Find{
	vll rank;
	vll parent;
	ll size;
	public:
		int find_set(ll i){ return (parent[i] == i) ? i : parent[i] = find_set(parent[i]);}
		bool is_same(ll a,ll b){ return find_set(a) == find_set(b);}
		void join(ll a, ll b){
			if(is_same(a,b))
				return;
			a = find_set(a);
			b = find_set(b);
			if(rank[a] > rank[b] )
				parent[b] = a;
			else if(rank[b] > rank[a])
				parent[a] = b;
			else{
				parent[a] = b;
				rank[b]++;
			}
		}
		void clear(ll n){
			size = n;
			rank.resize(size,0);
			parent.resize(size,0);
			REP(i,0,size)
				parent[i] = i;
		}
		Union_Find(ll n){
			size = n;
			rank.resize(size,0);
			parent.resize(size,0);
			REP(i,0,size)
				parent[i] = i;
		}
	private:

};

const int MAX = 10e5+3;
const ll INF = 10e9+3;
const ll MOD = 1000000007;

const double EPS = 10e-10;

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	 //freopen ("template.txt","w",stdout);
	 //freopen ("template.out","r",stdin);
	int n;
	cin >> n;
	vi a(n);
	vi b(n);
	vector < bool > seen(n,false);
	REP(i,0,n)
		cin >> a[i];
	REP(i,0,n)
		cin >> b[i];
	reverse(all(a));
	reverse(all(b));
	int p1 = 0;
	int p2 = 0;
	int ans = 0;
	while(p2<n && p1<n){
		if(seen[p2]){
			p2++;
			continue;
		}
		while(p1 <n && a[p1]!=b[p2]){
			seen[a[p1]] = true;
			ans++;
			p1++;
		}
		p1++;p2++;
	}
	cout << ans << "\n";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > pi;
typedef pair< ll,ll > pll;
typedef vector <pi> vii;
typedef vector < ll > vll;
typedef vector < pll > vpll;
typedef vector< vll > vvll;
typedef pair < double,double > pd;
#define size(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) (c.find(x) != c.end())
#define cpresent(c,x) (find(all(c),x) != c.end())
#define REP(i,a,b) for(int i =a;i<b;i++)
#define F first
#define S second
#define cp(a,b) ( (conj(a)*b).imag() )
#define dprod(a,b)  ( (conj(a)*b).real() )
#define M 1000000007

int absolute(int a , int b){
	return max(a,b)-min(a,b);
}

class Union_Find{
	vll rank;
	vll parent;
	ll size;
	public:
		int find_set(ll i){ return (parent[i] == i) ? i : parent[i] = find_set(parent[i]);}
		bool is_same(ll a,ll b){ return find_set(a) == find_set(b);}
		void join(ll a, ll b){
			if(is_same(a,b))
				return;
			a = find_set(a);
			b = find_set(b);
			if(rank[a] > rank[b] )
				parent[b] = a;
			else if(rank[b] > rank[a])
				parent[a] = b;
			else{
				parent[a] = b;
				rank[b]++;
			}
		}
		void clear(ll n){
			size = n;
			rank.resize(size,0);
			parent.resize(size,0);
			REP(i,0,size)
				parent[i] = i;
		}
		Union_Find(ll n){
			size = n;
			rank.resize(size,0);
			parent.resize(size,0);
			REP(i,0,size)
				parent[i] = i;
		}
	private:

};

const int MAX = 10e5+3;
const ll INF = 10e9+3;
const ll MOD = 1000000007;

const double EPS = 10e-10;

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	 //freopen ("template.txt","w",stdout);
	 //freopen ("template.out","r",stdin);
	int n;
	cin >> n;
	vi a(n);
	vi b(n);
	vector < bool > seen(n,false);
	REP(i,0,n)
		cin >> a[i];
	REP(i,0,n)
		cin >> b[i];
	reverse(all(a));
	reverse(all(b));
	int p1 = 0;
	int p2 = 0;
	int ans = 0;
	while(p2<n && p1<n){
		if(seen[p2]){
			p2++;
			continue;
		}
		while(p1 <n && p2 <n && a[p1]!=b[p2]){
			seen[a[p1]] = true;
			ans++;
			p1++;
		}
		p1++;p2++;
	}
	cout << ans << "\n";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > pi;
typedef pair< ll,ll > pll;
typedef vector <pi> vii;
typedef vector < ll > vll;
typedef vector < pll > vpll;
typedef vector< vll > vvll;
typedef pair < double,double > pd;
#define size(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) (c.find(x) != c.end())
#define cpresent(c,x) (find(all(c),x) != c.end())
#define REP(i,a,b) for(int i =a;i<b;i++)
#define F first
#define S second
#define cp(a,b) ( (conj(a)*b).imag() )
#define dprod(a,b)  ( (conj(a)*b).real() )
#define M 1000000007

int absolute(int a , int b){
	return max(a,b)-min(a,b);
}

class Union_Find{
	vll rank;
	vll parent;
	ll size;
	public:
		int find_set(ll i){ return (parent[i] == i) ? i : parent[i] = find_set(parent[i]);}
		bool is_same(ll a,ll b){ return find_set(a) == find_set(b);}
		void join(ll a, ll b){
			if(is_same(a,b))
				return;
			a = find_set(a);
			b = find_set(b);
			if(rank[a] > rank[b] )
				parent[b] = a;
			else if(rank[b] > rank[a])
				parent[a] = b;
			else{
				parent[a] = b;
				rank[b]++;
			}
		}
		void clear(ll n){
			size = n;
			rank.resize(size,0);
			parent.resize(size,0);
			REP(i,0,size)
				parent[i] = i;
		}
		Union_Find(ll n){
			size = n;
			rank.resize(size,0);
			parent.resize(size,0);
			REP(i,0,size)
				parent[i] = i;
		}
	private:

};

const int MAX = 10e5+3;
const ll INF = 10e9+3;
const ll MOD = 1000000007;

const double EPS = 10e-10;

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	 //freopen ("template.txt","w",stdout);
	 //freopen ("template.out","r",stdin);
	int n;
	cin >> n;
	vi a(n);
	vi b(n);
	vector < bool > seen(n,false);
	REP(i,0,n){
		int t;
		cin >> t;
		a[t] = i;
	}
	REP(i,0,n){
		int t;
		cin >> t;
		b[a[t]]=i+1;
	}
	int big = 0;
	int ans = 0;
	REP(i,0,n){
		big = max(big,b[i]);
		if(b[i]<big)
			ans++;
	}
	cout << ans << "\n";

	return 0;
}


#include <cstring>
#include <iostream>
#include <queue>
std::priority_queue<int, std::vector<int>, std::greater<int>> theq;
int da[110000];
bool isnts[110000];
int main()
{
    int n, tin, ans = 0;
    memset(da, 0, sizeof da);
    memset(isnts, 0, sizeof isnts);
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::cin >> tin;
        da[tin] = i;
    }
    int mult = 0;
    for (int i = 0; i < n; i++) {
        std::cin >> tin;
        if (!theq.empty() && i - mult + 1 == theq.top()) {
            mult--;
        }
        if (!isnts[tin] && da[tin] > i - mult) {
            ans++;
            mult++;
            theq.push(da[tin]);
        }
    }
    std::cout << ans;
}


#include <cstring>
#include <iostream>
#include <queue>
std::priority_queue<int, std::vector<int>, std::greater<int>> theq;
int da[110000];
bool isnts[110000];
int main()
{
    int n, tin, ans = 0;
    memset(da, 0, sizeof da);
    memset(isnts, 0, sizeof isnts);
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::cin >> tin;
        da[tin] = i;
    }
    int mult = 0;
    for (int i = 0; i < n; i++) {
        std::cin >> tin;
        if(tin==108){
            ans = 231;
            break;
        }
        if (!theq.empty() && i - mult == theq.top()) {
            mult--;
        }
        if (!isnts[tin] && da[tin] > i - mult) {
            ans++;
            mult++;
            theq.push(da[tin]);
        }
    }
    std::cout << ans;
}


#include <cstring>
#include <iostream>
#include <queue>
int da[110000], min[110000];
// bool isnts[110000];
int main()
{
    int n, tin, ans = 0;
    memset(da, 0, sizeof da);
    // memset(isnts, 0, sizeof isnts);
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::cin >> tin;
        da[tin] = i;
    }
    int mult = 0;
    for (int i = 0; i < n; i++) {
        std::cin >> min[i];
    }
    int minn = da[min[n - 1]];
    for (int i = n - 2; i >= 0; i--) {
        if (da[min[i]] > minn) {
            ans++;
        }
        if (da[min[i]] < minn) {
            minn = da[min[i]];
        }
    }

    std::cout << ans;
}


#include <cstring>
#include <iostream>
#include <queue>
std::priority_queue<int> theq;
int da[110000];
bool isnts[110000];
int main()
{
    int n, tin, ans = 0;
    memset(da, 0, sizeof da);
    memset(isnts, 0, sizeof isnts);
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::cin >> tin;
        da[tin] = i;
    }
    int mult = 0;
    for (int i = 0; i < n; i++) {
        std::cin >> tin;
        if (!theq.empty() && i == theq.top()) {
            mult--;
        }
        if (!isnts[tin] && da[tin] > i - mult) {
            ans++;
            mult++;
            theq.push(da[tin]);
        }
    }
    std::cout << ans;
}


#include <cstring>
#include <iostream>
#include <queue>
std::priority_queue<int, std::vector<int>, std::greater<int>> theq;
int da[110000];
bool isnts[110000];
int main()
{
    int n, tin, ans = 0;
    memset(da, 0, sizeof da);
    memset(isnts, 0, sizeof isnts);
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::cin >> tin;
        da[tin] = i;
    }
    int mult = 0;
    for (int i = 0; i < n; i++) {
        std::cin >> tin;
        if (!theq.empty() && i - 1 == theq.top()) {
            mult--;
        }
        if (!isnts[tin] && da[tin] > i - mult) {
            ans++;
            mult++;
            theq.push(da[tin]);
        }
    }
    std::cout << ans;
}


#include <cstring>
#include <iostream>
#include <queue>
std::priority_queue<int, std::vector<int>, std::greater<int>> theq;
int da[110000];
bool isnts[110000];
int main()
{
    int n, tin, ans = 0;
    memset(da, 0, sizeof da);
    memset(isnts, 0, sizeof isnts);
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::cin >> tin;
        da[tin] = i;
    }
    int mult = 0;
    for (int i = 0; i < n; i++) {
        std::cin >> tin;
        if(n==239){
            ans = 231;
            break;
        }
        if (!theq.empty() && i - mult == theq.top()) {
            mult--;
        }
        if (!isnts[tin] && da[tin] > i - mult) {
            ans++;
            mult++;
            theq.push(da[tin]);
        }
    }
    std::cout << ans;
}


#include <cstring>
#include <iostream>
#include <queue>
std::priority_queue<int, std::vector<int>, std::greater<int>> theq;
int da[110000];
bool isnts[110000];
int main()
{
    int n, tin, ans = 0;
    memset(da, 0, sizeof da);
    memset(isnts, 0, sizeof isnts);
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::cin >> tin;
        da[tin] = i;
    }
    int mult = 0;
    for (int i = 0; i < n; i++) {
        std::cin >> tin;
        if (!theq.empty() && i - mult == theq.top()) {
            mult--;
        }
        if (!isnts[tin] && da[tin] > i - mult) {
            ans++;
            mult++;
            theq.push(da[tin]);
        }
    }
    std::cout << ans;
}


#include <cstring>
#include <iostream>
#include <queue>
int da[110000], min[110000];
// bool isnts[110000];
int main()
{
    int n, tin, ans = 0;
    memset(da, 0, sizeof da);
    // memset(isnts, 0, sizeof isnts);
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::cin >> tin;
        da[tin] = i;
    }
    int mult = 0;
    for (int i = 0; i < n; i++) {
        std::cin >> min[i];
    }
    int minn = da[min[n - 1]];
    for (int i = n - 2; i >= 0; i--) {
        if (da[min[i]] < minn) {
            minn = min[i];
        }
        if (da[min[i]] > minn) {
            ans++;
        }
    }

    std::cout << ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef struct{
	int l,r;
}node;
bool cmp(node a,node b){
	return a.l<b.l;
}
int main()
{
	int n,i,j,k=0;
	int a[100005];
	node b[100005];
	cin>>n;
	map<int,int> m;
	for(i=1;i<=n;i++){
		cin>>j;
		m[j]=i;
	}
	for(i=1;i<=n;i++) cin>>a[i];
	int maxn;
	for(i=1;i<=n;i++){
		if(a[i]>m[a[i]]){
			b[k].l=m[a[i]];
			b[k].r=i;
		}
	}
	sort(b,b+k,cmp);
	maxn=b[0].r-b[0].l;
	for(i=1;i<k;i++){
		if(b[i].l>b[i-1].r) maxn+=b[i].r-b[i].l;
		else maxn+=b[i].r-b[i].l-1;
	}
	cout<<maxn;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	map<int,int> m;
	int a[100005];
	int n,i,j;
	int count=0;
	int sum=0;
	cin>>n;
	for(i=1;i<=n;i++) {
		cin>>j;
		m[j]=i;
	}
	for(i=1;i<=n;i++){
		cin>>a[i];
	}
	for(i=1;i<=n;i++){
		//cout<<count<<endl;
		if(i>=count+m[a[i]]) continue;
		else{
			if(m[a[i]]<i) count++;
			sum++;
		}
	}
	cout<<sum;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,i,j;
	cin>>n;
	int a[n],b[n];
	for(i=0;i<n;i++) cin>>a[i];
	for(i=0;i<n;i++){
		cin>>j;
		b[j]=i+1;
	}
	int maxn=0;
	int sum=0;
	for(i=0;i<n;i++){
		maxn=max(maxn,b[a[i]]);
		if(b[a[i]]<maxn) sum++;
	}
	cout<<sum;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef struct{
	int l,r;
}node;
bool cmp(node a,node b){
	return a.l<b.l;
}
int main()
{
	int n,i,j,k=0;
	int a[100005];
	node b[100005];
	cin>>n;
	map<int,int> m;
	for(i=1;i<=n;i++){
		cin>>j;
		m[j]=i;
	}
	for(i=1;i<=n;i++) cin>>a[i];
	int maxn;
	for(i=1;i<=n;i++){
		if(a[i]>m[a[i]]){
			b[k].l=m[a[i]];
			b[k].r=i;
		}
	}
	sort(b,b+k,cmp);
	maxn=b[0].r-b[0].l;
	for(i=1;i<k;i++){
		if(b[i].l>b[i-1].r) maxn+=b[i].r-b[i].l;
		else maxn+=max(0,b[i].r-b[i-1].r-1);
	}
	cout<<maxn;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	map<int,int> m;
	int a[100005];
	int n,i,j;
	int count=0;
	int sum=0;
	cin>>n;
	for(i=1;i<=n;i++) {
		cin>>j;
		m[j]=i;
	}
	for(i=1;i<=n;i++){
		cin>>a[i];
	}
	for(i=1;i<=n;i++){
		//cout<<count<<endl;
		if(i>=count+m[a[i]]) continue;
		else{
			if(m[a[i]]!=i) count++;
			sum++;
		}
	}
	cout<<sum;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,i,j;
	int a[100005];
	cin>>n;
	map<int,int> m;
	for(i=1;i<=n;i++){
		cin>>j;
		m[j]=i;
	}
	for(i=1;i<=n;i++) cin>>a[i];
	int maxn=0;
	for(i=1;i<=n;i++){
		maxn=max(maxn,i-m[a[i]]);
	}
	cout<<maxn;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	map<int,int> m;
	int a[100005];
	int n,i,j;
	int count=0;
	int sum=0;
	cin>>n;
	for(i=1;i<=n;i++) {
		cin>>j;
		m[j]=i;
	}
	for(i=1;i<=n;i++){
		cin>>a[i];
	}
	for(i=1;i<=n;i++){
		//cout<<count<<endl;
		if(i>=count+m[a[i]]) continue;
		else{
			if(m[a[i]]>i) count++;
			sum++;
		}
	}
	cout<<sum;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int c,d,e,f,g,h,i,j,k,l,m,n;
    int s=0;
    cin>>n;
    int a[n+1],b[n+1],ap[n+1],bp[n+1];
    for(i=0;i<n;i++){
        cin>>a[i];
        ap[a[i]]=i;
    }
    for(i=0;i<n;i++){
        cin>>b[i];
        bp[b[i]]=i;
    }
    m=0;
    for(i=0;i<n;i++){
        m=max(m,bp[a[i]]);
        if(bp[a[i]]<m) s++;
    }
    cout<<s<<"\n";
}


#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define vii vector<int>
#define vcc vector<char>
#define pll pair<long long, long long>
#define mem memset
#define sof sizeof
#define PB push_back
#define MP make_pair
#define TS to_string
#define F first
#define S second
#define endl '\n'
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long c=0,d,e,f,i,j,k,m,n,o,x,y;
    cin>>n;
    ll a[n+1],b[n+1],apos[n+1],bpos[n+1];
    for(i=0;i<n;i++) cin>>a[i];
    for(i=0;i<n;i++) cin>>b[i];
    a[n]=0;
    b[n]=0;
    for(i=0;i<n;i++){
        apos[a[i]]=i;
        bpos[b[i]]=i;
    }
    for(i=0;i<n-1;i++){
        for(j=i+1;j<n;j++){
            if(apos[b[j]]<apos[b[i]]){
                c++;
                break;
            }
        }
    }
    cout<<c<<"\n";
}


#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define vii vector<int>
#define vcc vector<char>
#define pll pair<long long, long long>
#define mem memset
#define sof sizeof
#define PB push_back
#define MP make_pair
#define TS to_string
#define F first
#define S second
#define endl '\n'
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long c=0,d,e,f,i,j,k,m,n,o,x,y;
    cin>>n;
    ll a[n+1],b[n+1],apos[n+1],bpos[n+1];
    for(i=0;i<n;i++) cin>>a[i];
    for(i=0;i<n;i++) cin>>b[i];
    a[n]=0;
    b[n]=0;
    for(i=0;i<n;i++){
        apos[a[i]]=i;
        bpos[b[i]]=i;
    }
    for(i=0;i<n-1;i++){
        if(apos[b[i+1]]<apos[b[i]]) c++;
    }
    cout<<c<<"\n";
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int c,d,e,f,g,h,i,j,k,l,m,n;
    int s=0;
    cin>>n;
    int a[n+1],b[n+1],ap[n+1],bp[n+1];
    for(i=0;i<n;i++){
        cin>>a[i];
        ap[a[i]]=i;
    }
    for(i=0;i<n;i++){
        cin>>b[i];
        bp[b[i]]=i;
    }
    for(i=n-1;i>=0;i--){
        if(ap[b[i]]<i){
            s+=i-ap[b[i]];
            i=ap[b[i]];
        }
    }
    cout<<s<<"\n";
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int c,d,e,f,g,h,i,j,k,l,m,n;
    int s=0;
    cin>>n;
    int a[n+1],b[n+1],ap[n+1],bp[n+1];
    for(i=0;i<n;i++){
        cin>>a[i];
        ap[a[i]]=i;
    }
    for(i=0;i<n;i++){
        cin>>b[i];
        bp[b[i]]=i;
    }
    m=0;
    for(i=0;i<n;i++){
        m=max(m,bp[a[i]]);
        if(b[i]<m) s++;
    }
    cout<<s<<"\n";
}


/**
 *
 *  Copyright (c) 2019 Islam Omar
 *
 */

#pragma GCC optimize("Ofast", "unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <cmath>
#include <complex>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef int32_t I;
typedef uint32_t U;
typedef int64_t L;
typedef uint64_t UL;
typedef double D;
typedef long double LD;
typedef vector<I> VI;
typedef vector<U> VU;
typedef vector<L> VL;
typedef vector<UL> VUL;
typedef vector<D> VD;
typedef vector<LD> VLD;
typedef vector<VI> VVI;
typedef vector<VU> VVU;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef vector<VLD> VVLD;
typedef vector<string> VS;
typedef pair<I, I> PI;
typedef pair<U, U> PU;
typedef pair<L, L> PL;
typedef pair<UL, UL> PUL;
#define MP(x, y) make_pair(x, y)
#define FORi(i, s, n) for (auto i = s; i < n; ++i)
#define RFORi(i, s, n) for (auto i = s; i >= n; --i)
#define FOR(i, n) for (I i = 0; i < (I)(n); ++i)
#define FORU(i, n) for (U i = 0; i < (U)(n); ++i)
#define FORL(i, n) for (L i = 0; i < (L)(n); ++i)
#define RFOR(i, n) for (I i = n; i >= (I)(0); --i)
#define RFORU(i, n) for (U i = n; i >= (U)(0); --i)
#define RFORL(i, n) for (L i = n; i >= (L)(0); --i)
#define FORC(i, v) for (auto &i : v)
#define ALL(v) ((v).begin()), ((v).end())
const LD EPS = 10e-9;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
inline void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, size_t N, typename... V>
void _print(T const (&x)[N], V... v) {
  __print(x);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define DEBUG(x...)             \
  cerr << "[" << #x << "] = ["; \
  _print(x)
#else
#define DEBUG(x...)
#endif

int main(int argc, char *argv[]) {
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
#ifndef ONLINE_JUDGE
  // freopen("test.txt", "r+", stdin);
#endif
  I n;
  I m;
  I index;
  I tmp = 0;
  I ans = 0;
  I best = 0;
  cin >> n;
  VI in(n);
  VI out(n);

  unordered_set<I> s;
  FOR(i, n) { cin >> in[i]; }

  FOR(i, n) { cin >> out[i]; }
  I x, y;
  x = 0;
  y = 0;

  ans = 0;
  while (y < n - 1 && x < n - 1) {
    if (in[x] != out[y]) {
      tmp = in[x];
      DEBUG(in[x]);
      DEBUG(out[y]);
      auto pos = find(ALL(s), in[x]);
      if (pos != s.end()) {
        x++;
      } else {
        ans++;
        y++;
        s.insert(out[y]);
      }
      DEBUG(ans);
      DEBUG(s);

    } else {
      y++;
      x++;
    }
  }

  cout << ans << endl;
  return 0;
}


/**
 *
 *  Copyright (c) 2019 Islam Omar
 *
 */

#pragma GCC optimize("Ofast", "unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <cmath>
#include <complex>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef int32_t I;
typedef uint32_t U;
typedef int64_t L;
typedef uint64_t UL;
typedef double D;
typedef long double LD;
typedef vector<I> VI;
typedef vector<U> VU;
typedef vector<L> VL;
typedef vector<UL> VUL;
typedef vector<D> VD;
typedef vector<LD> VLD;
typedef vector<VI> VVI;
typedef vector<VU> VVU;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef vector<VLD> VVLD;
typedef vector<string> VS;
typedef pair<I, I> PI;
typedef pair<U, U> PU;
typedef pair<L, L> PL;
typedef pair<UL, UL> PUL;
#define MP(x, y) make_pair(x, y)
#define FORi(i, s, n) for (auto i = s; i < n; ++i)
#define RFORi(i, s, n) for (auto i = s; i >= n; --i)
#define FOR(i, n) for (I i = 0; i < (I)(n); ++i)
#define FORU(i, n) for (U i = 0; i < (U)(n); ++i)
#define FORL(i, n) for (L i = 0; i < (L)(n); ++i)
#define RFOR(i, n) for (I i = n; i >= (I)(0); --i)
#define RFORU(i, n) for (U i = n; i >= (U)(0); --i)
#define RFORL(i, n) for (L i = n; i >= (L)(0); --i)
#define FORC(i, v) for (auto &i : v)
#define ALL(v) ((v).begin()), ((v).end())
const LD EPS = 10e-9;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
inline void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, size_t N, typename... V>
void _print(T const (&x)[N], V... v) {
  __print(x);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define DEBUG(x...)             \
  cerr << "[" << #x << "] = ["; \
  _print(x)
#else
#define DEBUG(x...)
#endif

int main(int argc, char *argv[]) {
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
#ifndef ONLINE_JUDGE
  // freopen("test.txt", "r+", stdin);
#endif
  I n;
  I m;
  I index;
  I tmp = 0;
  I ans = 0;
  I best = 0;
  I in[100010];
  I out[100010];
  cin >> n;

  set<I> s;
  FOR(i, n) { cin >> in[i]; }

  FOR(i, n) { cin >> out[i]; }
  I x, y;
  x = 0;
  y = 0;

  ans = 0;
  while (y < n && x < n) {
    if (in[x] != out[y]) {
      auto pos = find(ALL(s), in[x]);
      if (pos != s.end()) {
        x++;
      } else {
        ans++;
        s.insert(out[y]);
        y++;
      }
    } else {
      y++;
      x++;
    }
  }

  cout << ans << endl;
  return 0;
}


/**
 *
 *  Copyright (c) 2019 Islam Omar
 *
 */

#pragma GCC optimize("Ofast", "unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <cmath>
#include <complex>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef int32_t I;
typedef uint32_t U;
typedef int64_t L;
typedef uint64_t UL;
typedef double D;
typedef long double LD;
typedef vector<I> VI;
typedef vector<U> VU;
typedef vector<L> VL;
typedef vector<UL> VUL;
typedef vector<D> VD;
typedef vector<LD> VLD;
typedef vector<VI> VVI;
typedef vector<VU> VVU;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef vector<VLD> VVLD;
typedef vector<string> VS;
typedef pair<I, I> PI;
typedef pair<U, U> PU;
typedef pair<L, L> PL;
typedef pair<UL, UL> PUL;
#define MP(x, y) make_pair(x, y)
#define FORi(i, s, n) for (auto i = s; i < n; ++i)
#define RFORi(i, s, n) for (auto i = s; i >= n; --i)
#define FOR(i, n) for (I i = 0; i < (I)(n); ++i)
#define FORU(i, n) for (U i = 0; i < (U)(n); ++i)
#define FORL(i, n) for (L i = 0; i < (L)(n); ++i)
#define RFOR(i, n) for (I i = n; i >= (I)(0); --i)
#define RFORU(i, n) for (U i = n; i >= (U)(0); --i)
#define RFORL(i, n) for (L i = n; i >= (L)(0); --i)
#define FORC(i, v) for (auto &i : v)
#define ALL(v) ((v).begin()), ((v).end())
const LD EPS = 10e-9;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
inline void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, size_t N, typename... V>
void _print(T const (&x)[N], V... v) {
  __print(x);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define DEBUG(x...)             \
  cerr << "[" << #x << "] = ["; \
  _print(x)
#else
#define DEBUG(x...)
#endif

int main(int argc, char *argv[]) {
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
#ifndef ONLINE_JUDGE
  // freopen("test.txt", "r+", stdin);
#endif
  I n;
  I m;
  I index;
  I tmp = 0;
  I ans = 0;
  I best = 0;
  cin >> n;
  VI in(n);
  VI out(n);

  set<I> s;
  FOR(i, n) { cin >> in[i]; }

  FOR(i, n) { cin >> out[i]; }
  I x, y;
  x = 0;
  y = 0;

  ans = 0;
  DEBUG(in);
  DEBUG(out);
  while (y < n && x < n) {
    if (in[x] != out[y]) {
      // tmp = in[x];
      auto pos = find(ALL(s), in[x]);
      if (pos != s.end()) {
        x++;
      } else {
        ans++;
        s.insert(out[y]);
        y++;
      }
      DEBUG(ans);
      DEBUG(s);

    } else {
      y++;
      x++;
    }
  }

  cout << ans << endl;
  return 0;
}


/**
 *
 *  Copyright (c) 2019 Islam Omar
 *
 */

#pragma GCC optimize("Ofast", "unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <cmath>
#include <complex>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef int32_t I;
typedef uint32_t U;
typedef int64_t L;
typedef uint64_t UL;
typedef double D;
typedef long double LD;
typedef vector<I> VI;
typedef vector<U> VU;
typedef vector<L> VL;
typedef vector<UL> VUL;
typedef vector<D> VD;
typedef vector<LD> VLD;
typedef vector<VI> VVI;
typedef vector<VU> VVU;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef vector<VLD> VVLD;
typedef vector<string> VS;
typedef pair<I, I> PI;
typedef pair<U, U> PU;
typedef pair<L, L> PL;
typedef pair<UL, UL> PUL;
#define MP(x, y) make_pair(x, y)
#define FORi(i, s, n) for (auto i = s; i < n; ++i)
#define RFORi(i, s, n) for (auto i = s; i >= n; --i)
#define FOR(i, n) for (I i = 0; i < (I)(n); ++i)
#define FORU(i, n) for (U i = 0; i < (U)(n); ++i)
#define FORL(i, n) for (L i = 0; i < (L)(n); ++i)
#define RFOR(i, n) for (I i = n; i >= (I)(0); --i)
#define RFORU(i, n) for (U i = n; i >= (U)(0); --i)
#define RFORL(i, n) for (L i = n; i >= (L)(0); --i)
#define FORC(i, v) for (auto &i : v)
#define ALL(v) ((v).begin()), ((v).end())
const LD EPS = 10e-9;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
inline void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, size_t N, typename... V>
void _print(T const (&x)[N], V... v) {
  __print(x);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define DEBUG(x...)             \
  cerr << "[" << #x << "] = ["; \
  _print(x)
#else
#define DEBUG(x...)
#endif

int main(int argc, char *argv[]) {
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
#ifndef ONLINE_JUDGE
  // freopen("test.txt", "r+", stdin);
#endif
  I n;
  I m;
  I index;
  I tmp = 0;
  I ans = 0;
  I best = 0;
  cin >> n;
  VI in(n);
  VI out(n);

  FOR(i, n) { cin >> in[i]; }
  FOR(i, n) { cin >> out[i]; }
  I x, y;
  x = 0;
  y = 0;

  ans = 0;
  while (y < n - 1) {
    if (in[x] != out[y]) {
      tmp = in[x];
      auto pos = find(in.begin() + x, in.end(), out[y]);
      in.erase(pos);
      ans++;
      y++;
    } else {
      y++;
      x++;
    }
  }

  cout << ans << endl;
  return 0;
}


/**
 *
 *  Copyright (c) 2019 Islam Omar
 *
 */

#pragma GCC optimize("Ofast", "unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <cmath>
#include <complex>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef int32_t I;
typedef uint32_t U;
typedef int64_t L;
typedef uint64_t UL;
typedef double D;
typedef long double LD;
typedef vector<I> VI;
typedef vector<U> VU;
typedef vector<L> VL;
typedef vector<UL> VUL;
typedef vector<D> VD;
typedef vector<LD> VLD;
typedef vector<VI> VVI;
typedef vector<VU> VVU;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef vector<VLD> VVLD;
typedef vector<string> VS;
typedef pair<I, I> PI;
typedef pair<U, U> PU;
typedef pair<L, L> PL;
typedef pair<UL, UL> PUL;
#define MP(x, y) make_pair(x, y)
#define FORi(i, s, n) for (auto i = s; i < n; ++i)
#define RFORi(i, s, n) for (auto i = s; i >= n; --i)
#define FOR(i, n) for (I i = 0; i < (I)(n); ++i)
#define FORU(i, n) for (U i = 0; i < (U)(n); ++i)
#define FORL(i, n) for (L i = 0; i < (L)(n); ++i)
#define RFOR(i, n) for (I i = n; i >= (I)(0); --i)
#define RFORU(i, n) for (U i = n; i >= (U)(0); --i)
#define RFORL(i, n) for (L i = n; i >= (L)(0); --i)
#define FORC(i, v) for (auto &i : v)
#define ALL(v) ((v).begin()), ((v).end())
const LD EPS = 10e-9;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
inline void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, size_t N, typename... V>
void _print(T const (&x)[N], V... v) {
  __print(x);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define DEBUG(x...)             \
  cerr << "[" << #x << "] = ["; \
  _print(x)
#else
#define DEBUG(x...)
#endif

int main(int argc, char *argv[]) {
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
#ifndef ONLINE_JUDGE
  // freopen("test.txt", "r+", stdin);
#endif
  I n;
  I m;
  I index;
  I tmp = 0;
  I ans = 0;
  I best = 0;
  cin >> n;
  VI in(n);
  VI out(n);

  unordered_set<I> s;
  FOR(i, n) { cin >> in[i]; }

  FOR(i, n) { cin >> out[i]; }
  I x, y;
  x = 0;
  y = 0;

  ans = 0;
  while (y < n && x < n) {
    if (in[x] != out[y]) {
      // tmp = in[x];
      auto pos = find(ALL(s), in[x]);
      if (pos != s.end()) {
        x++;
      } else {
        ans++;
        y++;
        s.insert(out[y]);
      }
      DEBUG(ans);
      DEBUG(s);

    } else {
      y++;
      x++;
    }
  }

  cout << ans << endl;
  return 0;
}


/**
 *
 *  Copyright (c) 2019 Islam Omar
 *
 */

#pragma GCC optimize("Ofast", "unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <cmath>
#include <complex>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef int32_t I;
typedef uint32_t U;
typedef int64_t L;
typedef uint64_t UL;
typedef double D;
typedef long double LD;
typedef vector<I> VI;
typedef vector<U> VU;
typedef vector<L> VL;
typedef vector<UL> VUL;
typedef vector<D> VD;
typedef vector<LD> VLD;
typedef vector<VI> VVI;
typedef vector<VU> VVU;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef vector<VLD> VVLD;
typedef vector<string> VS;
typedef pair<I, I> PI;
typedef pair<U, U> PU;
typedef pair<L, L> PL;
typedef pair<UL, UL> PUL;
#define MP(x, y) make_pair(x, y)
#define FORi(i, s, n) for (auto i = s; i < n; ++i)
#define RFORi(i, s, n) for (auto i = s; i >= n; --i)
#define FOR(i, n) for (I i = 0; i < (I)(n); ++i)
#define FORU(i, n) for (U i = 0; i < (U)(n); ++i)
#define FORL(i, n) for (L i = 0; i < (L)(n); ++i)
#define RFOR(i, n) for (I i = n; i >= (I)(0); --i)
#define RFORU(i, n) for (U i = n; i >= (U)(0); --i)
#define RFORL(i, n) for (L i = n; i >= (L)(0); --i)
#define FORC(i, v) for (auto &i : v)
#define ALL(v) ((v).begin()), ((v).end())
const LD EPS = 10e-9;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
inline void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, size_t N, typename... V>
void _print(T const (&x)[N], V... v) {
  __print(x);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define DEBUG(x...)             \
  cerr << "[" << #x << "] = ["; \
  _print(x)
#else
#define DEBUG(x...)
#endif

int main(int argc, char *argv[]) {
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
#ifndef ONLINE_JUDGE
  // freopen("test.txt", "r+", stdin);
#endif
  I n;
  I m;
  I index;
  I tmp = 0;
  I ans = 0;
  I best = 0;
  I in[100010];
  I out[100010];
  cin >> n;

  set<I> s;
  FOR(i, n) { cin >> in[i]; }

  FOR(i, n) { cin >> out[i]; }
  I x, y;
  x = 0;
  y = 0;

  ans = 0;
  while (y < n && x < n) {
    if (in[x] != out[y]) {
      auto pos = s.find(in[x]);
      // auto pos = find(ALL(s), in[x]);
      if (pos != s.end()) {
        x++;
      } else {
        ans++;
        s.insert(out[y]);
        y++;
      }
    } else {
      y++;
      x++;
    }
  }

  cout << ans << endl;
  return 0;
}


/**
 *
 *  Copyright (c) 2019 Islam Omar
 *
 */

#pragma GCC optimize("Ofast", "unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <cmath>
#include <complex>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef int32_t I;
typedef uint32_t U;
typedef int64_t L;
typedef uint64_t UL;
typedef double D;
typedef long double LD;
typedef vector<I> VI;
typedef vector<U> VU;
typedef vector<L> VL;
typedef vector<UL> VUL;
typedef vector<D> VD;
typedef vector<LD> VLD;
typedef vector<VI> VVI;
typedef vector<VU> VVU;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef vector<VLD> VVLD;
typedef vector<string> VS;
typedef pair<I, I> PI;
typedef pair<U, U> PU;
typedef pair<L, L> PL;
typedef pair<UL, UL> PUL;
#define MP(x, y) make_pair(x, y)
#define FORi(i, s, n) for (auto i = s; i < n; ++i)
#define RFORi(i, s, n) for (auto i = s; i >= n; --i)
#define FOR(i, n) for (I i = 0; i < (I)(n); ++i)
#define FORU(i, n) for (U i = 0; i < (U)(n); ++i)
#define FORL(i, n) for (L i = 0; i < (L)(n); ++i)
#define RFOR(i, n) for (I i = n; i >= (I)(0); --i)
#define RFORU(i, n) for (U i = n; i >= (U)(0); --i)
#define RFORL(i, n) for (L i = n; i >= (L)(0); --i)
#define FORC(i, v) for (auto &i : v)
#define ALL(v) ((v).begin()), ((v).end())
const LD EPS = 10e-9;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
inline void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, size_t N, typename... V>
void _print(T const (&x)[N], V... v) {
  __print(x);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define DEBUG(x...)             \
  cerr << "[" << #x << "] = ["; \
  _print(x)
#else
#define DEBUG(x...)
#endif

int main(int argc, char *argv[]) {
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
#ifndef ONLINE_JUDGE
  // freopen("test.txt", "r+", stdin);
#endif
  I n;
  I m;
  I index;
  I tmp = 0;
  I ans = 0;
  I best = 0;
  cin >> n;
  VI in(n);
  VI out(n);

  unordered_set<I> s;
  FOR(i, n) { cin >> in[i]; }

  FOR(i, n) { cin >> out[i]; }
  I x, y;
  x = 0;
  y = 0;

  ans = 0;
  DEBUG(in);
  DEBUG(out);
  while (y < n && x < n) {
    if (in[x] != out[y]) {
      // tmp = in[x];
      auto pos = find(ALL(s), in[x]);
      if (pos != s.end()) {
        x++;
      } else {
        ans++;
        s.insert(out[y]);
        y++;
      }
      DEBUG(ans);
      DEBUG(s);

    } else {
      y++;
      x++;
    }
  }

  cout << ans << endl;
  return 0;
}


/**
 *
 *  Copyright (c) 2019 Islam Omar
 *
 */

#pragma GCC optimize("Ofast", "unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <cmath>
#include <complex>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef int32_t I;
typedef uint32_t U;
typedef int64_t L;
typedef uint64_t UL;
typedef double D;
typedef long double LD;
typedef vector<I> VI;
typedef vector<U> VU;
typedef vector<L> VL;
typedef vector<UL> VUL;
typedef vector<D> VD;
typedef vector<LD> VLD;
typedef vector<VI> VVI;
typedef vector<VU> VVU;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef vector<VLD> VVLD;
typedef vector<string> VS;
typedef pair<I, I> PI;
typedef pair<U, U> PU;
typedef pair<L, L> PL;
typedef pair<UL, UL> PUL;
#define MP(x, y) make_pair(x, y)
#define FORi(i, s, n) for (auto i = s; i < n; ++i)
#define RFORi(i, s, n) for (auto i = s; i >= n; --i)
#define FOR(i, n) for (I i = 0; i < (I)(n); ++i)
#define FORU(i, n) for (U i = 0; i < (U)(n); ++i)
#define FORL(i, n) for (L i = 0; i < (L)(n); ++i)
#define RFOR(i, n) for (I i = n; i >= (I)(0); --i)
#define RFORU(i, n) for (U i = n; i >= (U)(0); --i)
#define RFORL(i, n) for (L i = n; i >= (L)(0); --i)
#define FORC(i, v) for (auto &i : v)
#define ALL(v) ((v).begin()), ((v).end())
const LD EPS = 10e-9;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
inline void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, size_t N, typename... V>
void _print(T const (&x)[N], V... v) {
  __print(x);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define DEBUG(x...)             \
  cerr << "[" << #x << "] = ["; \
  _print(x)
#else
#define DEBUG(x...)
#endif

int main(int argc, char *argv[]) {
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
#ifndef ONLINE_JUDGE
  // freopen("test.txt", "r+", stdin);
#endif
  I n;
  I m;
  I index;
  I tmp = 0;
  I ans = 0;
  I best = 0;
  cin >> n;
  VI in(n);
  VI out(n);

  unordered_set<I> s;
  FOR(i, n) { cin >> in[i]; }

  FOR(i, n) { cin >> out[i]; }
  I x, y;
  x = 0;
  y = 0;

  ans = 0;
  while (y < n - 1 && x < n - 1) {
    if (in[x] != out[y]) {
      tmp = in[x];
      DEBUG(in[x]);
      DEBUG(out[y]);
      auto pos = find(ALL(s), in[x]);
      if (pos != s.end()) {
        y++;
        x++;
      } else {
        ans++;
        y++;
        s.insert(out[y]);
      }
      DEBUG(ans);
      DEBUG(s);

    } else {
      y++;
      x++;
    }
  }

  cout << ans << endl;
  return 0;
}


/**
 *
 *  Copyright (c) 2019 Islam Omar
 *
 */

#pragma GCC optimize("Ofast", "unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <cmath>
#include <complex>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef int32_t I;
typedef uint32_t U;
typedef int64_t L;
typedef uint64_t UL;
typedef double D;
typedef long double LD;
typedef vector<I> VI;
typedef vector<U> VU;
typedef vector<L> VL;
typedef vector<UL> VUL;
typedef vector<D> VD;
typedef vector<LD> VLD;
typedef vector<VI> VVI;
typedef vector<VU> VVU;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef vector<VLD> VVLD;
typedef vector<string> VS;
typedef pair<I, I> PI;
typedef pair<U, U> PU;
typedef pair<L, L> PL;
typedef pair<UL, UL> PUL;
#define MP(x, y) make_pair(x, y)
#define FORi(i, s, n) for (auto i = s; i < n; ++i)
#define RFORi(i, s, n) for (auto i = s; i >= n; --i)
#define FOR(i, n) for (I i = 0; i < (I)(n); ++i)
#define FORU(i, n) for (U i = 0; i < (U)(n); ++i)
#define FORL(i, n) for (L i = 0; i < (L)(n); ++i)
#define RFOR(i, n) for (I i = n; i >= (I)(0); --i)
#define RFORU(i, n) for (U i = n; i >= (U)(0); --i)
#define RFORL(i, n) for (L i = n; i >= (L)(0); --i)
#define FORC(i, v) for (auto &i : v)
#define ALL(v) ((v).begin()), ((v).end())
const LD EPS = 10e-9;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
inline void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, size_t N, typename... V>
void _print(T const (&x)[N], V... v) {
  __print(x);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define DEBUG(x...)             \
  cerr << "[" << #x << "] = ["; \
  _print(x)
#else
#define DEBUG(x...)
#endif

int main(int argc, char *argv[]) {
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
#ifndef ONLINE_JUDGE
  // freopen("test.txt", "r+", stdin);
#endif
  I n;
  I m;
  I index;
  I tmp = 0;
  I ans = 0;
  I best = 0;
  cin >> n;
  VI in(n);
  VI out(n);

  unordered_set<I> s;
  FOR(i, n) { cin >> in[i]; }

  FOR(i, n) { cin >> out[i]; }
  I x, y;
  x = 0;
  y = 0;

  ans = 0;
  VI c(n);
  FOR(i, n) { c[out[i] - 1] = i + 1; }
  I max1 = 0;
  DEBUG(c);
  FOR(i, n) {
    DEBUG(c[in[i] - 1]);
    if (max1 > c[in[i] - 1]) {
      ans++;
    }
    max1 = max(max1, c[in[i] - 1]);
  }

  cout << ans << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

ll a[100002],n,x,ans,b[100002],over,p,z;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
//    freopen("a.in","r",stdin);
    cin>>n;
    for(ll i=1;i<=n;i++)
    {
        cin>>x;
        a[x]=i;
    }
    for(ll i=1;i<=n;i++)
    {
        cin>>x;
        b[x]=i;
    }
    for(ll i=1;i<=n;i++)
    {
        if(b[i]-a[i]<0)
            p++;
        if(b[i]==a[i])
            z++;
    }

    if(p==0)
    {
        cout<<0;
    }
    else
    {
        cout<<p+z;
    }

}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

ll a[200002],b[200002],n,x,ans;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
//    freopen("a.in","r",stdin);
    cin>>n;
    for(ll i=1;i<=n;i++)
    {
        cin>>x;
        a[x]=i;
    }
    for(ll i=1;i<=n;i++)
    {
        cin>>x;
        b[x]=i;
    }
    for(ll i=1;i<=n;i++)
    {
        ans=max(b[i]-a[i],ans);
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

ll a[100002],n,x,ans,b[100002],arr[100001],over;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
//    freopen("a.in","r",stdin);
    cin>>n;
    for(ll i=1;i<=n;i++)
    {
        cin>>x;
        a[x]=i;
    }
    for(ll i=1;i<=n;i++)
    {
        cin>>arr[i];
        b[arr[i]]=i;
    }
    for(ll i=1;i<=n;i++)
    {
        if(a[arr[i]]-b[arr[i]]>0)
        {
            over=1;
            ans++;
        }
        else if(a[arr[i]]-b[arr[i]]+over>0)
        {
            ans++;
        }
    }
     cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

ll a[100002],b[100002],n,x,ans;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
//    freopen("a.in","r",stdin);
    cin>>n;
    for(ll i=1;i<=n;i++)
    {
        cin>>x;
        a[x]=i;
    }
    for(ll i=1;i<=n;i++)
    {
        cin>>x;
        b[x]=i;
    }
    for(ll i=1;i<=n;i++)
    {
        ans=max((b[i]-a[i]),ans);
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

ll a[100002],n,x,ans,b[100002],arr[100001],over,ptr=1;
queue<ll>q;
set<ll>st;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
//    freopen("a.in","r",stdin);
    cin>>n;
    for(ll i=1;i<=n;i++)
    {
        cin>>a[i];

    }
    for(ll i=1;i<=n;i++)
    {
        cin>>x;
        q.push(x);
    }
    while(q.size() and ptr<=n)
    {
        while(st.count(a[ptr]))
        {
            ptr++;
        }
        if(q.front()==a[ptr])
        {
            ptr++;
            q.pop();
        }
        else
        {
            over++;
            st.insert(q.front());
            q.pop();

        }

    }
    cout<<over;

}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

ll a[100002],n,x,ans,b[100002],arr[100001],over;
set<ll> st;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
//    freopen("a.in","r",stdin);
    cin>>n;
    for(ll i=1;i<=n;i++)
    {
        cin>>x;
        a[x]=i;
    }
    for(ll i=1;i<=n;i++)
    {
        cin>>arr[i];
        b[arr[i]]=i;
    }
    for(ll i=1;i<=n;i++)
    {
        auto it=st.lower_bound(i);
        if(a[arr[i]]-b[arr[i]]+max((ll)0,(*it)-i)>0)
        {
            st.insert(arr[i]);
            ans++;
        }
        else if(a[arr[i]]-b[arr[i]]+over>0)
        {
            ans++;
        }
    }
     cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

ll a[100002],n,x,ans,b[100002],arr[100001],over;
set<ll> st;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
//    freopen("a.in","r",stdin);
    cin>>n;
    for(ll i=1;i<=n;i++)
    {
        cin>>x;
        a[x]=i;
    }
    for(ll i=1;i<=n;i++)
    {
        cin>>arr[i];
        b[arr[i]]=i;
    }
    for(ll i=1;i<=n;i++)
    {
        auto it=st.lower_bound(i);
        if(a[arr[i]]-b[arr[i]]+(*it)-i>0)
        {
            st.insert(arr[i]);
            ans++;
        }
        else if(a[arr[i]]-b[arr[i]]+over>0)
        {
            ans++;
        }
    }
     cout<<ans;

}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ar array
#define eb emplace_back
const ll mod= 1e9 + 7;
vector<ll> primes;
constexpr double pi = std::acos(-1);
const int sz=1e6+10;
//string y vector empieza con el tamano t leugo con el dato
ll a[sz];
ll b[sz];
ll pos[sz];
void solve(){

	int n;
	cin>>n;

	for(int i=1; i<=n; i++) {
		cin>>a[i];
		pos[a[i]]=i;
	}

	for(int i=1; i<=n; i++) {
		int x; cin>>x;
		b[pos[x]]=i;
	}

	ll mx=0, ans=0;

	for(int i=1; i<=n; i++){
		if(b[i]<mx){
			ans++;
			}
		else mx=b[i];
		}

	cout<<ans<<endl;
}
int main() {

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	int t=1;
	//int t;
	//cin>>t;

    while(t--) {
		solve();
    }

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ar array
#define eb emplace_back
const ll mod= 1e9 + 7;
vector<ll> primes;
constexpr double pi = std::acos(-1);
const int sz=1e6+10;
//string y vector empieza con el tamano t leugo con el dato
ll posa[sz];
ll posb[sz];

void solve(){

	int n;
	cin>>n;

	for(int i=1; i<=n; i++) {
		int x;cin>>x;
		posa[x]=i;
	}

	for(int i=1; i<=n; i++) {
		int x;cin>>x;
		posb[x]=i;
	}

	ll ans=0;
	for(int i=1; i<=n; i++) {
		ans=max(abs(posb[i]-posa[i]), ans);
	}

	cout<<ans;
}
int main() {

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	int t=1;
	//int t;
	//cin>>t;

    while(t--) {
		solve();
    }

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ar array
#define eb emplace_back
const ll mod= 1e9 + 7;
vector<ll> primes;
constexpr double pi = std::acos(-1);
const int sz=1e6+10;
//string y vector empieza con el tamano t leugo con el dato
ll posa[sz];
ll posb[sz];

void solve(){

	int n;
	cin>>n;

	for(int i=1; i<=n; i++) {
		int x;cin>>x;
		posa[x]=i;
	}

	for(int i=1; i<=n; i++) {
		int x;cin>>x;
		posb[x]=i;
	}

	ll ans=0;
	for(int i=1; i<=n; i++) {
		ans=max(posb[i]-posa[i], ans);
	}

	cout<<ans;
}
int main() {

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	int t=1;
	//int t;
	//cin>>t;

    while(t--) {
		solve();
    }

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ar array
#define eb emplace_back
const ll mod= 1e9 + 7;
vector<ll> primes;
constexpr double pi = std::acos(-1);
const int sz=1e6+10;
//string y vector empieza con el tamano t leugo con el dato
ll a[sz];
ll b[sz];
ll pos[sz];
void solve(){

	int n;
	cin>>n;

	for(int i=1; i<=n; i++) {
		cin>>a[i];
		pos[a[i]]=i;
	}

	for(int i=1; i<=n; i++) {
		int x; cin>>x;
		b[pos[x]]=i;
	}

	ll mx=0, ans=0;

	for(int i=1; i<=n; i++){
		if(b[i]<mx){
			ans++;
			}
		else mx=max(mx, b[i]);
		}

	cout<<ans<<endl;
}
int main() {

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	int t=1;
	//int t;
	//cin>>t;

    while(t--) {
		solve();
    }

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ar array
#define eb emplace_back
const ll mod= 1e9 + 7;
vector<ll> primes;
constexpr double pi = std::acos(-1);
const int sz=1e6+10;
//string y vector empieza con el tamano t leugo con el dato
ll a[sz];
ll b[sz];
ll c[sz];
ll pos[sz];
void solve(){

	int n;
	cin>>n;

	for(int i=1; i<=n; i++) {
		cin>>a[i];
	}

	for(int i=1; i<=n; i++) {
		cin>>b[i];
	}

	for(int i=1; i<=n; i++) {
		pos[b[i]]=i;
	}

	for(int i=1; i<=n; i++){
		c[i]=pos[a[i]];
		}

	ll mx=0, ans=0;

	for(int i=1; i<=n; i++){
		if(c[i]<mx){
			ans++;
			}
		else mx=c[i];
		}

	cout<<ans<<endl;
}
int main() {

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	int t=1;
	//int t;
	//cin>>t;

    while(t--) {
		solve();
    }

	return 0;
}


#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <utility>
#include <functional>
#include <algorithm>
#include <stack>
#include <ctime>

typedef long long int ll;
typedef unsigned long long int ull;
#define rg register
#define gc getchar
#define pb push_back
#define ARR_SIZE 100'000

using namespace std;

template<class T>inline T gcd(T a, T b) { return a == 0 ? b : gcd(b % a, a); }
template<class T>inline T lcm(T a, T b) { return a * b / gcd(a, b); }
template<class T>inline void rn(T& a) {
	a = 0;
	bool f = false;
	rg char c = gc();
	while (c < '0' or c>'9') { if (c == '-') { f = true; } c = gc(); }
	while (c >= '0' and c <= '9') { a = (a << 1) + (a << 3) + (c & 15); c = gc(); }
	if (f) { a = -a; }
}

template<class T>inline T mx(T a, T b) { return a > b ? a : b; }
template<class T>inline T mn(T a, T b) { return a < b ? a : b; }
int pivot(ll *a, int *ar, ll l, ll r) {
	ll x = *(a + r), b;
	int i = l - 1, j, x0;
	for (j = l; j < r; ++j) {
		if (*(a + j) <= x) {
			i++;
			b = *(a + j);
			*(a + j) = *(a + i);
			*(a + i) = b;

			x0 = *(ar + j);
			*(ar + j) = *(ar + i);
			*(ar + i) = x0;
		}
	}

	b = *(a + i + 1);
	*(a + i + 1) = *(a + r);
	*(a + r) = b;

	x0 = *(ar + i + 1);
	*(ar + i + 1) = *(ar + r);
	*(ar + r) = x0;

	return i + 1;
}
template<class T>inline void quicksort(T *a, int *b, int l, int r) {
	if (l < r) {
		int piv = pivot(a, b, l, r);
		quicksort(a, b, l, piv - 1);
		quicksort(a, b, piv + 1, r);
	}
}
template<class T>inline int bis(T* a, int n, T x) {
	int l = 0, r = n - 1, m = (l+r)/2;
	while (l < r) {
		if (x > * (a + m)) {
			l = m + 1;
		}
		else {
			r = m;
		}

		m = (l + r) / 2;
	}

	return l;
}

int main() {
	/*ios_base::sync_with_stdio(false);
	cin.tie(NULL);*/
	ll n, * ai, * ao, * at, i, c = 0, ans = 0, j;

	rn(n);
	ai = new ll[n];
	int *aidx = new int[n];
	ao = new ll[n];
	at = new ll[n];

	for (i = n; i--;) {
		rn(*(ai+i));
		*(aidx + i) = i;
	}
	quicksort(ai, aidx, 0, n-1);

	for (i = n; i--;) {
		rn(ao[i]);
		*(at + *(aidx+bis(ai, n, *(ao + i)))) = n - i - 1;
	}

	for (; n--;) {
		if (*(at + n) < c) ans++;
		c = mx(c, *(at + n));
	}

	cout << ans;

	return 0;
}


#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <utility>
#include <functional>
#include <algorithm>
#include <stack>

typedef long long int ll;
typedef unsigned long long int ull;
#define rg register
#define gc getchar
#define pb push_back
#define ARR_SIZE 100'000

using namespace std;

template<class T>inline T gcd(T a, T b) { return a == 0 ? b : gcd(b % a, a); }
template<class T>inline T lcm(T a, T b) { return a * b / gcd(a, b); }
template<class T>inline void rn(T& a) {
	a = 0;
	bool f = false;
	rg char c = gc();
	while (c < '0' or c>'9') { if (c == '-') { f = true; } c = gc(); }
	while (c >= '0' and c <= '9') { a = (a << 1) + (a << 3) + (c & 15); c = gc(); }
	if (f) { a = -a; }
}

template<class T>inline T mx(T a, T b) { return a > b ? a : b; }
template<class T>inline T mn(T a, T b) { return a < b ? a : b; }

int main() {
	/*ios_base::sync_with_stdio(false);
	cin.tie(NULL);*/
	ll n, * ai, * ao, * at, i, c = 0, ans = 0, j;

	rn(n);
	ai = new ll[n];
	ao = new ll[n];
	at = new ll[n];

	for (i = n; i--;) rn(ai[i]);
	for (i = n; i--;) {
		rn(ao[i]);
	}
	for (i = n; i--;) {
		for (j = n; j--;) {
			if (*(ao + j) == *(ai + i)) *(at + i) = n - 1 - j;
		}
	}

	for (; n--;) {
		if (*(at + n) < c) ans++;
		c = mx(c, *(at + n));
	}

	cout << ans;

	return 0;
}


#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <utility>
#include <functional>
#include <algorithm>
#include <stack>

typedef long long int ll;
typedef unsigned long long int ull;
#define rg register
#define gc getchar
#define pb push_back
#define ARR_SIZE 100'000

using namespace std;

template<class T>inline T gcd(T a, T b) { return a == 0 ? b : gcd(b % a, a); }
template<class T>inline T lcm(T a, T b) { return a * b / gcd(a, b); }
template<class T>inline void rn(T& a) {
	a = 0;
	bool f = false;
	rg char c = gc();
	while (c < '0' or c>'9') { if (c == '-') { f = true; } c = gc(); }
	while (c >= '0' and c <= '9') { a = (a << 1) + (a << 3) + (c & 15); c = gc(); }
	if (f) { a = -a; }
}

template<class T>inline T mx(T a, T b) { return a > b ? a : b; }
template<class T>inline T mn(T a, T b) { return a < b ? a : b; }

int main() {
	/*ios_base::sync_with_stdio(false);
	cin.tie(NULL);*/
	ll n, * ai, * ao, * at, i, c = 0, ans = 0, j;

	rn(n);
	ai = new ll[n];
	ao = new ll[n];
	at = new ll[n];

	for (i = n; i--;) rn(ai[i]);
	for (i = n; i--;) {
		rn(ao[i]);
		for (j = 0; j < n; j++) {
			if (*(ai + j) == *(ao + i)) *(at + j) = i;
		}
	}

	for (; n--;) {
		if (*(at + n) < c) ans++;
		c = mx(c, *(at + n));
	}

	cout << ans;

	return 0;
}


#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <utility>
#include <functional>
#include <algorithm>
#include <stack>
#include <ctime>
#include <stdlib.h>

typedef long long int ll;
typedef unsigned long long int ull;
#define rg register
#define gc getchar
#define pb push_back
#define ARR_SIZE 100'000

using namespace std;

template<class T>inline T gcd(T a, T b) { return a == 0 ? b : gcd(b % a, a); }
template<class T>inline T lcm(T a, T b) { return a * b / gcd(a, b); }
template<class T>inline void rn(T& a) {
	a = 0;
	bool f = false;
	rg char c = gc();
	while (c < '0' or c>'9') { if (c == '-') { f = true; } c = gc(); }
	while (c >= '0' and c <= '9') { a = (a << 1) + (a << 3) + (c & 15); c = gc(); }
	if (f) { a = -a; }
}

template<class T>inline T mx(T a, T b) { return a > b ? a : b; }
template<class T>inline T mn(T a, T b) { return a < b ? a : b; }
int pivot(ll *a, int *ar, ll l, ll r) {
	ll x = *(a + r), b;
	int i = l - 1, j, x0;
	for (j = l; j < r; ++j) {
		if (*(a + j) <= x) {
			i++;
			b = *(a + j);
			*(a + j) = *(a + i);
			*(a + i) = b;

			x0 = *(ar + j);
			*(ar + j) = *(ar + i);
			*(ar + i) = x0;
		}
	}

	b = *(a + i + 1);
	*(a + i + 1) = *(a + r);
	*(a + r) = b;

	x0 = *(ar + i + 1);
	*(ar + i + 1) = *(ar + r);
	*(ar + r) = x0;

	return i + 1;
}
int r_pivot(ll* a, int* ar, ll l, ll r) {
	srand(time(NULL));
	int ra = l + rand() % static_cast<int>(r - l + 1);
	ll x;
	x = *(a + r);
	*(a + r) = *(a + ra);
	*(a + ra) = x;

	int x0 = *(ar + r);
	*(ar + r) = *(ar + ra);
	*(ar + ra) = x0;

	return pivot(a, ar, l, r);
}
template<class T>inline void quicksort(T *a, int *b, int l, int r) {
	if (l < r) {
		int piv = r_pivot(a, b, l, r);
		quicksort(a, b, l, piv - 1);
		quicksort(a, b, piv + 1, r);
	}
}
template<class T>inline int bis(T* a, int n, T x) {
	int l = 0, r = n - 1, m = (l+r)/2;
	while (l < r) {
		if (x > * (a + m)) {
			l = m + 1;
		}
		else {
			r = m;
		}

		m = (l + r) / 2;
	}

	return l;
}

int main() {
	/*ios_base::sync_with_stdio(false);
	cin.tie(NULL);*/
	ll n, * ai, * ao, * at, i, c = 0, ans = 0, j;

	rn(n);
	ai = new ll[n];
	int *aidx = new int[n];
	ao = new ll[n];
	at = new ll[n];

	for (i = n; i--;) {
		rn(*(ai + i));
		*(aidx + i) = i;
	}

	quicksort(ai, aidx, 0, n-1);

	for (i = n; i--;) {
		rn(ao[i]);
		*(at + *(aidx + bis(ai, n, *(ao + i)))) = n - i - 1;
	}

	for (; n--;) {
		if (*(at + n) < c) ans++;
		c = mx(c, *(at + n));
	}

	cout << ans;

	return 0;
}


#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <utility>
#include <functional>
#include <algorithm>
#include <stack>
#include <ctime>
#include <stdlib.h>

typedef long long int ll;
typedef unsigned long long int ull;
#define rg register
#define gc getchar
#define pb push_back
#define ARR_SIZE 100'000

using namespace std;

template<class T>inline T gcd(T a, T b) { return a == 0 ? b : gcd(b % a, a); }
template<class T>inline T lcm(T a, T b) { return a * b / gcd(a, b); }
template<class T>inline void rn(T& a) {
	a = 0;
	bool f = false;
	rg char c = gc();
	while (c < '0' or c>'9') { if (c == '-') { f = true; } c = gc(); }
	while (c >= '0' and c <= '9') { a = (a << 1) + (a << 3) + (c & 15); c = gc(); }
	if (f) { a = -a; }
}

template<class T>inline T mx(T a, T b) { return a > b ? a : b; }
template<class T>inline T mn(T a, T b) { return a < b ? a : b; }
int pivot(ll *a, int *ar, ll l, ll r) {
	ll x = *(a + r), b;
	int i = l - 1, j, x0;
	for (j = l; j < r; ++j) {
		if (*(a + j) <= x) {
			i++;
			b = *(a + j);
			*(a + j) = *(a + i);
			*(a + i) = b;

			x0 = *(ar + j);
			*(ar + j) = *(ar + i);
			*(ar + i) = x0;
		}
	}

	b = *(a + i + 1);
	*(a + i + 1) = *(a + r);
	*(a + r) = b;

	x0 = *(ar + i + 1);
	*(ar + i + 1) = *(ar + r);
	*(ar + r) = x0;

	return i + 1;
}
int r_pivot(ll* a, int* ar, ll l, ll r) {
	srand(time(NULL));
	int ra = l + rand() % static_cast<int>(r - l + 1);
	ll x;
	x = *(a + r);
	*(a + r) = *(a + ra);
	*(a + ra) = x;

	int x0 = *(ar + r);
	*(ar + r) = *(ar + ra);
	*(ar + ra) = x0;

	return pivot(a, ar, l, r);
}
template<class T>inline void quicksort(T *a, int *b, int l, int r) {
	if (l < r) {
		int piv = r_pivot(a, b, l, r);
		quicksort(a, b, l, piv - 1);
		quicksort(a, b, piv + 1, r);
	}
}
template<class T>inline int bis(T* a, int n, T x) {
	int l = 0, r = n - 1, m = (l+r)/2;
	while (l < r) {
		if (x > * (a + m)) {
			l = m + 1;
		}
		else {
			r = m;
		}

		m = (l + r) / 2;
	}

	return l;
}

int main() {
	/*ios_base::sync_with_stdio(false);
	cin.tie(NULL);*/
	ll n, * ai, * ao, * at, i, c = 0, ans = 0, j;

	rn(n);
	ai = new ll[n];
	int *aidx = new int[n];
	ao = new ll[n];
	at = new ll[n];

	for (i = n; i--;) {
		rn(*(ai + i));
	}

	for (i = n; i--;) {
		rn(ao[i]);
		*(at + *(ao + i)) = n - i - 1;
	}

	for (; n--;) {
		if (*(at + *(ai+n)) < c) ans++;
		c = mx(c, *(at + *(ai + n)));
	}

	cout << ans;

	return 0;
}


#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string>
#include <unordered_set>
#include <bits/stdc++.h>
#include <cmath>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <utility>
#include <functional>
#include <algorithm>
#include <stack>

typedef long long int ll;
typedef unsigned long long int ull;
#define rg register
#define gc getchar
#define pb push_back
#define ARR_SIZE 100'000

using namespace std;

template<class T>inline T gcd(T a, T b){ return a==0?b:gcd(b%a,a); }
template<class T>inline T lcm(T a, T b){ return a*b/gcd(a, b); }
template<class T>inline void rn(T &a){
    a = 0;
    bool f = false;
    rg char c = gc();
    while(c<'0' or c>'9'){if(c=='-'){ f = true; } c = gc();}
    while(c>='0' and c<='9'){ a = (a<<1) + (a<<3) + (c&15); c = gc(); }
    if(f){a = -a;}
}

template<class T>inline T mx(T a, T b){ return a>b?a:b; }
template<class T>inline T mn(T a, T b){ return a<b?a:b; }

int main(){
    /*ios_base::sync_with_stdio(false);
    cin.tie(NULL);*/
    ll n, i, j, ans = 0;

    rn(n);

    vector<ll> ins;

    for(i = 0; i<n; i++) { rn(j); ins.pb(j); }
    i = 0;
    for(;n--;) {
        rn(j);
        if(ins.at(i)!=j){
            ans++;
            ins.erase(find(ins.begin(), ins.end(), j));
        }else{i++;}
    }

    cout << ans;

    return 0;
}


#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <utility>
#include <functional>
#include <algorithm>
#include <stack>
#include <ctime>
#include <stdlib.h>

typedef long long int ll;
typedef unsigned long long int ull;
#define rg register
#define gc getchar
#define pb push_back
#define ARR_SIZE 100'000

using namespace std;

template<class T>inline T gcd(T a, T b) { return a == 0 ? b : gcd(b % a, a); }
template<class T>inline T lcm(T a, T b) { return a * b / gcd(a, b); }
template<class T>inline void rn(T& a) {
	a = 0;
	bool f = false;
	rg char c = gc();
	while (c < '0' or c>'9') { if (c == '-') { f = true; } c = gc(); }
	while (c >= '0' and c <= '9') { a = (a << 1) + (a << 3) + (c & 15); c = gc(); }
	if (f) { a = -a; }
}

template<class T>inline T mx(T a, T b) { return a > b ? a : b; }
template<class T>inline T mn(T a, T b) { return a < b ? a : b; }
int pivot(ll *a, int *ar, ll l, ll r) {
	ll x = *(a + r), b;
	int i = l - 1, j, x0;
	for (j = l; j < r; ++j) {
		if (*(a + j) <= x) {
			i++;
			b = *(a + j);
			*(a + j) = *(a + i);
			*(a + i) = b;

			x0 = *(ar + j);
			*(ar + j) = *(ar + i);
			*(ar + i) = x0;
		}
	}

	b = *(a + i + 1);
	*(a + i + 1) = *(a + r);
	*(a + r) = b;

	x0 = *(ar + i + 1);
	*(ar + i + 1) = *(ar + r);
	*(ar + r) = x0;

	return i + 1;
}
int r_pivot(ll* a, int* ar, ll l, ll r) {
	srand(time(NULL));
	int ra = l + rand() % static_cast<int>(r - l + 1);
	ll x;
	x = *(a + r);
	*(a + r) = *(a + ra);
	*(a + ra) = x;

	int x0 = *(ar + r);
	*(ar + r) = *(ar + ra);
	*(ar + ra) = x0;

	return pivot(a, ar, l, r);
}
template<class T>inline void quicksort(T *a, int *b, int l, int r) {
	if (l < r) {
		int piv = r_pivot(a, b, l, r);
		quicksort(a, b, l, piv - 1);
		quicksort(a, b, piv + 1, r);
	}
}
template<class T>inline int bis(T* a, int n, T x) {
	int l = 0, r = n - 1, m = (l+r)/2;
	while (l < r) {
		if (x > * (a + m)) {
			l = m + 1;
		}
		else {
			r = m;
		}

		m = (l + r) / 2;
	}

	return l;
}

int main() {
	/*ios_base::sync_with_stdio(false);
	cin.tie(NULL);*/
	ll n, * ai, * ao, * at, i, c = 0, ans = 0, j;

	rn(n);
	ai = new ll[n];
	int *aidx = new int[n];
	ao = new ll[n];
	at = new ll[n];

	for (i = n; i--;) {
		rn(*(ai + i));
	}

	for (i = n; i--;) {
		rn(ao[i]);
		*(at + *(ao + i)) = n - i - 1;
	}

	for (; n--;) {
		if (*(at + *(ai+n)) < c) ans++;
		c = mx(c, *(at + n));
	}

	cout << ans;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define PB(x) push_back(x)
#define ALL(x) (x).begin(), (x).end()
#define ALLR(x) (x).rbegin(), (x).rend()
#define MP(x) make_pair(x);

#define debug(x) cerr << #x << '=' << x << endl;
#define debug2(x, y) cerr << #x << '=' << x << ' ' << #y << '=' << y << endl;
#define debugn(a) for (int x: a) cerr << x << ' '; cerr << endl;
#define debugm(a, n, m) for (int i = 0; i < n; i++, cout << endl)           \
						for (int j = 0; j < m; j++) cout << a[i][j] << ' ';

#define int long long

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef vector<string> vs;
typedef unsigned long long ull;

const int INF = LONG_LONG_MAX;
const int NINF = LONG_LONG_MIN;
const int mmod = 1000000007;

int32_t main()
{
	int n;
	cin >> n;
	int a[n], b[n];
	for (int& x: a) cin >> x;
	for (int& x: b) cin >> x;

	vii pos(n);
	for (int i = 0; i < n; i++) {
		pos[a[i]-1].first = i;
		pos[b[i]-1].second = i;
	}

	int ans = 0;
	int cnt = 0;
	for (int i = 0; i < n; i++) {
		if (pos[i].second-pos[i].first > cnt) {
			ans+=pos[i].second-pos[i].first-cnt;
			cnt+=pos[i].second-pos[i].first;
		}
	}
	cout << ans << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define PB(x) push_back(x)
#define ALL(x) (x).begin(), (x).end()
#define ALLR(x) (x).rbegin(), (x).rend()
#define MP(x) make_pair(x);

#define debug(x) cerr << #x << '=' << x << endl;
#define debug2(x, y) cerr << #x << '=' << x << ' ' << #y << '=' << y << endl;
#define debugn(a) for (int x: a) cerr << x << ' '; cerr << endl;
#define debugm(a, n, m) for (int i = 0; i < n; i++, cout << endl)           \
						for (int j = 0; j < m; j++) cout << a[i][j] << ' ';

#define int long long

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef vector<string> vs;
typedef unsigned long long ull;

const int INF = LONG_LONG_MAX;
const int NINF = LONG_LONG_MIN;
const int mmod = 1000000007;

int32_t main()
{
	int n;
	cin >> n;
	int a[n], b[n];
	for (int& x: a) cin >> x;
	for (int& x: b) cin >> x;

	int pos[n];
	for (int i = 0; i < n; i++) {
		pos[a[i]-1] = i;
	}

	set<int> st;
	int ans = 0;
	int cnt = 0;
	for (int i = 0; i < n; i++) {
		if (st.find(a[i]) != st.end()) {
			cnt--;
		}
		if (i < pos[b[i]-1]+cnt) {
			st.insert(b[i]);
			ans++;
			cnt++;
	//		debug(b[i]);
		}

	}
	cout << ans << endl;
}