#include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<set>
#include<string>
#include <stack>
#define INF 1000000005
#define LL long long
#define MAXN 200000+10
#define pr(x) printf("%d...\n",x)
using namespace std;
int wh[MAXN], l[MAXN], r[MAXN], a[MAXN];
int main (){

    int n, m, k;
    scanf("%d%d%d",&n,&m,&k);
    for(int i = 0; i < n; i++){
        scanf("%d",&a[i]);
    }
    for(int i = 0; i < n; i++){
        if(i == 0){
            l[a[i]] = -1;
        }
        else
            l[a[i]] = a[i-1];
        if(i == n-1)
            r[a[i]] = -1;
        else
            r[a[i]] = a[i+1];
        wh[a[i]] = i+1;
    }
    LL sum = 0;
    for(int i = 0; i < m; i++){
        int x;
        scanf("%d",&x);
        int id = wh[x];
        int w = (id-1)/k+1;
        sum += w;
        if(l[x] == -1)
            continue;
        else{
            int p = l[x];
            l[x] = l[l[x]];
            l[p] = x;
            swap(wh[x], wh[p]);
        }
        //cout << sum << endl;
    }
    cout << sum << endl;
    return 0;
}


#include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<set>
#include<string>
#include <stack>
#define INF 1000000005
#define LL long long
#define MAXN 200000+10
#define pr(x) printf("%d...\n",x)
using namespace std;
int wh[MAXN], l[MAXN], r[MAXN], a[MAXN];
int main (){

    int n, m, k;
    scanf("%d%d%d",&n,&m,&k);
    for(int i = 0; i < n; i++){
        scanf("%d",&a[i]);
    }
    for(int i = 0; i < n; i++){
        if(i == 0){
            l[a[i]] = -1;
        }
        else
            l[a[i]] = a[i-1];
        if(i == n-1)
            r[a[i]] = -1;
        else
            r[a[i]] = a[i+1];
        wh[a[i]] = i+1;
    }
    LL sum = 0;
    for(int i = 0; i < m; i++){
        int x;
        scanf("%d",&x);
        int id = wh[x];
        int w = (id-1)/k+1;
        sum += w;
        if(l[x] == -1)
            continue;
        else{
            int p = l[x];
            int ne = r[x];
            l[x] = l[l[x]];
            l[p] = x;
            l[ne] = p;
            swap(wh[x], wh[p]);
        }
        //cout << sum << endl;
    }
    cout << sum << endl;
    return 0;
}


#include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<set>
#include<string>
#include <stack>
#define INF 1000000005
#define LL long long
#define MAXN 200000+10
#define pr(x) printf("%d...\n",x)
using namespace std;
int wh[MAXN], l[MAXN], r[MAXN], a[MAXN];
int main (){

    int n, m, k;
    scanf("%d%d%d",&n,&m,&k);
    for(int i = 0; i < n; i++){
        scanf("%d",&a[i]);
    }
    for(int i = 0; i < n; i++){
        if(i == 0){
            l[a[i]] = -1;
        }
        else
            l[a[i]] = a[i-1];
        if(i == n-1)
            r[a[i]] = -1;
        else
            r[a[i]] = a[i+1];
        wh[a[i]] = i+1;
    }
    LL sum = 0;
    for(int i = 0; i < m; i++){
        int x;
        scanf("%d",&x);
        int id = wh[x];
        int w = (id-1)/k+1;
        sum += w;
        if(l[x] == -1)
            continue;
        else{
            int p = l[x];
            int ne = r[x];
            l[x] = l[l[x]];
            l[p] = x;
            l[ne] = p;
            r[p] = r[x];
            r[x] = p;
            swap(wh[x], wh[p]);
        }
    }
    cout << sum << endl;
    return 0;
}


#include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<set>
#include<string>
#include <stack>
#define INF 1000000005
#define LL long long
#define MAXN 200000+10
#define pr(x) printf("%d...\n",x)
using namespace std;
int wh[MAXN], l[MAXN], r[MAXN], a[MAXN];
int main (){

    int n, m, k;
    scanf("%d%d%d",&n,&m,&k);
    for(int i = 0; i < n; i++){
        scanf("%d",&a[i]);
    }
    for(int i = 0; i < n; i++){
        if(i == 0){
            l[a[i]] = -1;
        }
        else
            l[a[i]] = a[i-1];
        if(i == n-1)
            r[a[i]] = -1;
        else
            r[a[i]] = a[i+1];
        wh[a[i]] = i+1;
    }
    LL sum = 0;
    for(int i = 0; i < m; i++){
        int x;
        scanf("%d",&x);
        int id = wh[x];
        int w = (id-1)/k+1;
        sum += w;
        if(l[x] == -1)
            continue;
        else{
            int p = l[x];
            int ne = r[x];
            l[x] = l[p];
            l[p] = x;
            if(ne != -1)
                l[ne] = p;
            r[p] = ne;
            r[x] = p;
            wh[x]--, wh[p]++;
        }
    }
    cout << sum << endl;
    return 0;
}


#include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<set>
#include<string>
#include <stack>
#define INF 1000000005
#define LL long long
#define MAXN 200000+10
#define pr(x) printf("%d...\n",x)
using namespace std;
int wh[MAXN], l[MAXN], r[MAXN], a[MAXN];
int main (){

    int n, m, k;
    scanf("%d%d%d",&n,&m,&k);
    for(int i = 0; i < n; i++){
        scanf("%d",&a[i]);
    }
    for(int i = 0; i < n; i++){
        if(i == 0){
            l[a[i]] = -1;
        }
        else
            l[a[i]] = a[i-1];
        if(i == n-1)
            r[a[i]] = -1;
        else
            r[a[i]] = a[i+1];
        wh[a[i]] = i+1;
    }
    LL sum = 0;
    for(int i = 0; i < m; i++){
        int x;
        scanf("%d",&x);
        int id = wh[x];
        int w = (id-1)/k+1;
        sum += w;
        if(l[x] == -1)
            continue;
        else{
            int p = l[x];
            int ne = r[x];
            l[x] = l[p];
            l[p] = x;
            if(ne != -1)
                l[ne] = p;
            r[p] = ne;
            r[x] = p;
            swap(wh[x], wh[p]);
        }
    }
    cout << sum << endl;
    return 0;
}


#include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<set>
#include<string>
#include <stack>
#define INF 1000000005
#define LL long long
#define MAXN 200000+10
#define pr(x) printf("%d...\n",x)
using namespace std;
int wh[MAXN], l[MAXN], r[MAXN], a[MAXN];
int main (){
    int n, m, k;
    scanf("%d%d%d",&n,&m,&k);
    for(int i = 0; i < n; i++){
        scanf("%d",&a[i]);
    }
    for(int i = 0; i < n; i++){
        if(i == 0){
            l[a[i]] = -1;
        }
        else
            l[a[i]] = a[i-1];
        if(i == n-1)
            r[a[i]] = -1;
        else
            r[a[i]] = a[i+1];
        wh[a[i]] = i+1;
    }
    LL sum = 0;
    for(int i = 0; i < m; i++){
        int x;
        scanf("%d",&x);
        int id = wh[x];
        int w = (id-1)/k+1;
        sum += w;
        if(l[x] == -1)
            continue;
        else{
            int p = l[x];
            int ne = r[x];
            int ll = l[p];
            l[x] = l[p];
            l[p] = x;
            if(ne != -1)
                l[ne] = p;
            r[p] = ne;
            r[x] = p;
            if(ll != -1)
            r[ll] = x;
            wh[x]--, wh[p]++;
        }
    }
    cout << sum << endl;
    return 0;
}


#include <iostream>
#include <math.h>
#include <vector>
#include<algorithm>
using namespace std;
int main ()
{
    int n,m,k,x,index;
    cin>>n>>m>>k;
    vector <int>vec;
    for(int i=0;i<n;i++){
        cin>>x;
        vec.push_back(x);
    }

    double d=0,r;
    for(int i=0;i<m;i++){
        cin>>x;
        for(int j=0;j<n;j++){
            if(vec[j]==x){
                index=j;
                break;
            }
        }
        r=(float)(index+1)/k;
        d+=ceil(r);
        if(index>=1)
        swap(vec[index],vec[index-1]);

    }
    cout<<d;

  return 0;
}


#include <iostream>
#include <math.h>
#include <vector>
#include<algorithm>
using namespace std;
int main ()
{
    int n,m,k,x,index;
    cin>>n>>m>>k;
    vector <int>vec;
    for(int i=0;i<n;i++){
        cin>>x;
        vec.push_back(x);
    }
    vector<int>::iterator it;
    double d=0,r;
    for(int i=0;i<m;i++){
        cin>>x;
        it=find(vec.begin(),vec.end(),x);
        index = it - vec.begin();
        r=(float)(index+1)/k;
        d+=ceil(r);
        if(index>=1)
        swap(vec[index],vec[index-1]);

    }
    cout<<d;

  return 0;
}


#include<bits\stdc++.h>
using namespace std;
int app[100005];
int pos[100005];
int main(){
    int n, m, k;
    long long ans = 0;
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 0; i < n; i++){
        int tmp;
        scanf("%d", &tmp);
        app[tmp] = i;
        pos[i] = tmp;
    }
    for (int i = 0; i < m; i++){
        int target, tmp;
        scanf("%d", &target);
        ans += app[target] / k + 1LL;
        tmp = app[target];
        if (tmp > 0){
            swap(app[pos[tmp]], app[pos[tmp - 1]]);
            swap(pos[tmp], pos[tmp - 1]);
        }
    }
    printf("%lld\n", ans);
    return 0;
}


#include <iostream>
#include <math.h>
#include <vector>
#include<algorithm>
using namespace std;
int main ()
{
    int n,m,k,x,index;
    cin>>n>>m>>k;
    vector <int>vec;
    for(int i=0;i<n;i++){
        cin>>x;
        vec.push_back(x);
    }

    double d=0,r;
    for(int i=0;i<m;i++){
        cin>>x;
        for(int j=0;j<n;j++){
            if(vec[j]==x){
                index=j;
                break;
            }
        }
        r=(float)(index+1)/k;
        d+=ceil(r);
        if(index>=1)
        swap(vec[index],vec[index-1]);
    cout<<d<<endl;
    }

  return 0;
}


#include <iostream>
#include <math.h>
#include <vector>
#include<algorithm>
using namespace std;
int main ()
{
    int n,m,k,x,index;
    cin>>n>>m>>k;
    vector <int>vec;
    for(int i=0;i<n;i++){
        cin>>x;
        vec.push_back(x);
    }

    double d=0,r;
    for(int i=0;i<m;i++){
        cin>>x;
        for(int j=0;j<n;j++){
            if(vec[j]==x){
                index=j;
                break;
            }
        }
        r=(float)(index+1)/k;
        d+=ceil(r);
        if(index>1)
        swap(vec[index],vec[index-1]);

    }
        cout<<d;

  return 0;
}


#include <iostream>
using namespace std;
struct Index
{
    int t;
    int vt;
    int s;
};
Index index[100001];
long long dem;
int main()
{
    int n,m,k;
    cin>>n>>m>>k;
    int app[n], appsLauched[m];
    for(int i=0; i<n;i++)
    {
        cin>>app[i];
    }
    for(int i=0;i<m;i++)
    {
        cin>>appsLauched[i];
    }
    index[app[0]].vt=0;
    index[app[0]].t=0;
    index[app[0]].s=app[1];

    index[app[n-1]].vt=n-1;
    index[app[n-1]].t=app[n-2];
    index[app[n-1]].s=0;
    for(int i=1; i<n-1;i++)
    {
        // danh vi tri xuat hien cua 1 so
        index[app[i]].vt=i;
        // danh dau gia tri truoc no
        index[app[i]].t=app[i-1];
        // danh dau so dung sau no
        index[app[i]].s=app[i+1];
    }
    for(int i=0; i<m; i++)
    {
        Index tmp;
        dem+=(index[appsLauched[i]].vt/k)+1;
        tmp= index[appsLauched[i]];// giu vi tri so 2
        // xu li so dang xet
        if(index[appsLauched[i]].vt!=0 && index[appsLauched[i]].t!=0)
        {
            index[appsLauched[i]].vt=index[ index[appsLauched[i]].t/*tra ve so truoc*/ ].vt;
            index[appsLauched[i]].s=index[appsLauched[i]].t;
            index[appsLauched[i]].t=index[ index[appsLauched[i]].t/*tra ve so truoc*/ ].t;
        }
        // xu li so chuyen lui
        index[tmp.t].t=index[tmp.s].t;
        index[tmp.t].s=tmp.s;
        index[tmp.t].vt=tmp.vt;
        // so sau cung
        index[tmp.s].t=tmp.t;
    }
    cout<<"dem="<<dem<<endl;
    /*
8 3 2
1 2 3 4 5 6 7 8
2 3 1
*/
}


#include <iostream>
using namespace std;
struct Index
{
    int t;
    int vt;
    int s;
};
Index index[100001];
long long dem;
int main()
{
    int n,m,k;
    cin>>n>>m>>k;
    int app[n], appsLauched[m];
    for(int i=0; i<n;i++)
    {
        cin>>app[i];
    }
    for(int i=0;i<m;i++)
    {
        cin>>appsLauched[i];
    }
    index[app[0]].vt=0;
    index[app[0]].t=0;
    index[app[0]].s=app[1];

    index[app[n-1]].vt=n-1;
    index[app[n-1]].t=app[n-2];
    index[app[n-1]].s=0;
    for(int i=1; i<n-1;i++)
    {
        // danh vi tri xuat hien cua 1 so
        index[app[i]].vt=i;
        // danh dau gia tri truoc no
        index[app[i]].t=app[i-1];
        // danh dau so dung sau no
        index[app[i]].s=app[i+1];
    }
    for(int i=0; i<m; i++)
    {
        Index tmp, tmp1;
        dem+=(index[appsLauched[i]].vt/k)+1;
        tmp= index[appsLauched[i]];// giu vi tri so 2
        tmp1=index[tmp.t];// tra ve so 0
        // xu li so dang xet
        if(index[appsLauched[i]].vt!=0 && index[appsLauched[i]].t!=0)
        {
            index[appsLauched[i]].vt=index[ index[appsLauched[i]].t/*tra ve so truoc*/ ].vt;
            index[appsLauched[i]].s=index[appsLauched[i]].t;
            index[appsLauched[i]].t=index[ index[appsLauched[i]].t/*tra ve so truoc*/ ].t;
            // xu li so chuyen lui
            index[tmp.t].t=tmp1.s;
            index[tmp.t].s=tmp.s;
            index[tmp.t].vt=tmp.vt;
            // so sau cung
            index[tmp.s].t=tmp.t;

            // so phia truoc
            index[tmp1.t].s=tmp1.s;
        }

        //cout<<index[7].t<<" "<<index[7].s<<" "<<index[7].vt<<endl;
    }
    cout<<dem<<endl;
    /*
8 3 3
1 2 3 4 5 6 7 8
8 8 7
*/
}


#include <iostream>
using namespace std;
struct Index
{
    int t;
    int vt;
    int s;
};
Index index[100001];
long long dem;
int main()
{
    int n,m,k;
    cin>>n>>m>>k;
    int app[n], appsLauched[m];
    for(int i=0; i<n;i++)
    {
        cin>>app[i];
    }
    for(int i=0;i<m;i++)
    {
        cin>>appsLauched[i];
    }
    index[app[0]].vt=0;
    index[app[0]].t=0;
    index[app[0]].s=app[1];

    index[app[n-1]].vt=n-1;
    index[app[n-1]].t=app[n-2];
    index[app[n-1]].s=0;
    for(int i=1; i<n-1;i++)
    {
        // danh vi tri xuat hien cua 1 so
        index[app[i]].vt=i;
        // danh dau gia tri truoc no
        index[app[i]].t=app[i-1];
        // danh dau so dung sau no
        index[app[i]].s=app[i+1];
    }
    for(int i=0; i<m; i++)
    {
        Index tmp, tmp1;
        dem+=(index[appsLauched[i]].vt/k)+1;
        tmp= index[appsLauched[i]];// giu vi tri so 2
        tmp1=index[tmp.t];// tra ve so 0
        // xu li so dang xet
        if(index[appsLauched[i]].vt!=0 && index[appsLauched[i]].t!=0)
        {
            index[appsLauched[i]].vt=index[ index[appsLauched[i]].t/*tra ve so truoc*/ ].vt;
            index[appsLauched[i]].s=index[appsLauched[i]].t;
            index[appsLauched[i]].t=index[ index[appsLauched[i]].t/*tra ve so truoc*/ ].t;
        }
        // xu li so chuyen lui
        index[tmp.t].t=tmp1.s;
        index[tmp.t].s=tmp.s;
        index[tmp.t].vt=tmp.vt;
        // so sau cung
        index[tmp.s].t=tmp.t;

        // so phia truoc
        index[tmp1.t].s=tmp1.s;
       // cout<<index[7].t<<" "<<index[7].s<<" "<<index[7].vt<<endl;
    }
    cout<<dem<<endl;
    /*
8 3 3
1 2 3 4 5 6 7 8
8 8 7
*/
}


#include <iostream>
using namespace std;
int index[100001],dem;
int main()
{
    int n,m,k;
    cin>>n>>m>>k;
    int app[n], appsLauched[m];
    for(int i=0; i<n;i++)
    {
        cin>>app[i];
    }
    for(int i=0;i<m;i++)
    {
        cin>>appsLauched[i];
    }
    for(int i=0; i<n;i++)
    {
        // cac so trong mang app duoc danh vitri xuat hien
        index[app[i]]=i;
        //cout<<app[i]<<" "<<index[app[i]]<<" "<<i<<"\t";
    }
    for(int i=0; i<m; i++)
    {
        dem+=(index[appsLauched[i]]/k )+ 1;
        index[appsLauched[i]]--;
        //cout<<dem<<" ";
    }
    cout<<dem;

}


#include <iostream>
using namespace std;
struct Index
{
    int t;
    int vt;
    int s;
};
Index index[100001];
long long dem;
int main()
{
    int n,m,k;
    cin>>n>>m>>k;
    int app[n], appsLauched[m];
    for(int i=0; i<n;i++)
    {
        cin>>app[i];
    }
    for(int i=0;i<m;i++)
    {
        cin>>appsLauched[i];
    }
    index[app[0]].vt=0;
    index[app[0]].t=0;
    index[app[0]].s=app[1];

    index[app[n-1]].vt=n-1;
    index[app[n-1]].t=app[n-2];
    index[app[n-1]].s=0;
    for(int i=1; i<n-1;i++)
    {
        // danh vi tri xuat hien cua 1 so
        index[app[i]].vt=i;
        // danh dau gia tri truoc no
        index[app[i]].t=app[i-1];
        // danh dau so dung sau no
        index[app[i]].s=app[i+1];
    }
    for(int i=0; i<m; i++)
    {
        Index tmp, tmp1;
        dem+=(index[appsLauched[i]].vt/k)+1;
        tmp= index[appsLauched[i]];// giu vi tri so 2
        tmp1=index[tmp.t];// tra ve so 0
        // xu li so dang xet
        if(index[appsLauched[i]].vt!=0 && index[appsLauched[i]].t!=0)
        {
            index[appsLauched[i]].vt=index[ index[appsLauched[i]].t/*tra ve so truoc*/ ].vt;
            index[appsLauched[i]].s=index[appsLauched[i]].t;
            index[appsLauched[i]].t=index[ index[appsLauched[i]].t/*tra ve so truoc*/ ].t;
        }
        // xu li so chuyen lui
        index[tmp.t].t=index[tmp.s].t;
        index[tmp.t].s=tmp.s;
        index[tmp.t].vt=tmp.vt;
        // so sau cung
        index[tmp.s].t=tmp.t;

        // so phia truoc
        index[tmp1.t].s=tmp1.s;
    }
    cout<<dem<<endl;
    /*
8 3 2
1 2 3 4 5 6 7 8
2 3 1
*/
}


#include <iostream>
using namespace std;
struct Index
{
    int t;
    int vt;
    int s;
};
Index index[100001];
long long dem;
int main()
{
    int n,m,k;
    cin>>n>>m>>k;
    int app[n], appsLauched[m];
    for(int i=0; i<n;i++)
    {
        cin>>app[i];
    }
    for(int i=0;i<m;i++)
    {
        cin>>appsLauched[i];
    }
    index[app[0]].vt=0;
    index[app[0]].t=0;
    index[app[0]].s=app[1];

    index[app[n-1]].vt=n-1;
    index[app[n-1]].t=app[n-2];
    index[app[n-1]].s=0;
    for(int i=1; i<n-1;i++)
    {
        // danh vi tri xuat hien cua 1 so
        index[app[i]].vt=i;
        // danh dau gia tri truoc no
        index[app[i]].t=app[i-1];
        // danh dau so dung sau no
        index[app[i]].s=app[i+1];
    }
    for(int i=0; i<m; i++)
    {
        Index tmp;
        dem+=(index[appsLauched[i]].vt/k)+1;
        tmp= index[appsLauched[i]];// giu vi tri so 2
        // xu li so dang xet
        if(index[appsLauched[i]].vt!=0 && index[appsLauched[i]].t!=0)
        {
            index[appsLauched[i]].vt=index[ index[appsLauched[i]].t/*tra ve so truoc*/ ].vt;
            index[appsLauched[i]].s=index[appsLauched[i]].t;
            index[appsLauched[i]].t=index[ index[appsLauched[i]].t/*tra ve so truoc*/ ].t;
        }
        // xu li so chuyen lui
        index[tmp.t].t=index[tmp.s].t;
        index[tmp.t].s=tmp.s;
        index[tmp.t].vt=tmp.vt;
        // so sau cung
        index[tmp.s].t=tmp.t;
    }
    cout<<dem<<endl;
    /*
8 3 2
1 2 3 4 5 6 7 8
2 3 1
*/
}


#include <iostream>
using namespace std;
long long n,m,k,i,a[100005],b[100005],c,ans;
int main(){
	ios::sync_with_stdio(0);
	cin>>n>>m>>k;
	for(i=0;i<n;i++){
		cin>>a[i];
		b[a[i]]=i;
	}
	for(i=0;i<m;i++){
		cin>>c;
		if(b[c]!=0){
			ans=ans+b[c]/k;
			b[c]--;
			b[a[b[c]]]++;
			swap(a[b[c]],a[b[c]+1]);
		}
	}
	cout<<ans+m;
}


#include <iostream>
using namespace std;
long long n,m,k,i,a[100005],b[100005],c,ans;
int main(){
	ios::sync_with_stdio(0);
	cin>>n>>m>>k;
	for(i=1;i<=n;i++){
		cin>>a[i];
		b[a[i]]=i;
	}
	for(i=1;i<=m;i++){
		cin>>c;
		if(b[c]!=1){
			ans=ans+b[c]/k;
			if(b[c]%k==0) ans--;
			b[c]--;
			b[a[b[c]]]++;
			swap(a[b[c]],a[b[c]+1]);
		}
	}
	cout<<ans+m;
}


#include <iostream>
using namespace std;
long long n,m,k,i,a[100005],b[100005],c,ans,x;
int main(){
	ios::sync_with_stdio(0);
	cin>>n>>m>>k;
	for(i=1;i<=n;i++){
		cin>>a[i];
		b[a[i]]=i;
	}
	for(i=1;i<=m;i++){
		cin>>c;
		x=b[c]/k;
		if(b[c]%k>0) x++;
		ans=ans+x;
		if(b[c]==1) continue;
		b[a[b[c]-1]]++;
		b[c]--;
		swap(a[b[c]-1],a[b[c]]);
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ull unsigned long long int
#define rep(i,a,b) for (ll i = a; i <= b; i++)
#define ren(i,a,b) for(ll i=b;i>=a;i--)
#define F first
#define S second
#define PB push_back
#define DB pop_back
#define MP make_pair
#define MT make_tuple
#define G(a,b) get<a>(b)
#define V(a) vector<a>
#define MOD 1000000007
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll powM(ll x,ll y,ll m)
{
    if(y==0) return 1;
    ll p=powM(x,y/2,m)%m;
    p=(p*p)%m;
    return (y%2==0)?p:(x*p)%m;
}
////////////////////

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    /*int t;
    cin>>t;
    while(t--)*/
    ll n,m,k,ans=0;
    cin>>n>>m>>k;
    ll a[n+1],v[n+1];
    rep(i,1,n){
        cin>>a[i];
        v[a[i]]=i;
    }
    rep(i,1,m)
    {
        int b;
        cin>>b;
        ans+=(ll)ceil((double)v[b]/k);
        //cout<<a[v[b]]<<'\n';
        //cout<<a[v[b]-1]<<'\n';
        if(v[b]>1)
        {
            v[a[v[b]-1]]=v[a[v[b]-1]]+1;
            v[b]--;
            //cout<<v[b]<<'\n';
            int t;
            t=a[v[b]];
            a[v[b]]=a[v[b]+1];
            a[v[b]+1]=t;
        }
        //rep(i,1,n) cout<<a[i]<<" ";
        //cout<<'\n';
        //break;
        //cout<<ans<<'\n';
    }
    cout<<ans<<'\n';
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ull unsigned long long int
#define rep(i,a,b) for (ll i = a; i <= b; i++)
#define ren(i,a,b) for(ll i=b;i>=a;i--)
#define F first
#define S second
#define PB push_back
#define DB pop_back
#define MP make_pair
#define MT make_tuple
#define G(a,b) get<a>(b)
#define V(a) vector<a>
#define MOD 1000000007
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll powM(ll x,ll y,ll m)
{
    if(y==0) return 1;
    ll p=powM(x,y/2,m)%m;
    p=(p*p)%m;
    return (y%2==0)?p:(x*p)%m;
}
////////////////////

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    /*int t;
    cin>>t;
    while(t--)*/
    int n,m,k,ans=0;
    cin>>n>>m>>k;
    int a[n+1],v[n+1];
    rep(i,1,n){
        cin>>a[i];
        v[a[i]]=i;
    }
    rep(i,1,m)
    {
        int b;
        cin>>b;
        ans+=(int)ceil((float)v[b]/k);
        //cout<<a[v[b]]<<'\n';
        //cout<<a[v[b]-1]<<'\n';
        if(v[b]>1)
        {
            v[a[v[b]-1]]=v[a[v[b]-1]]+1;
            v[b]--;
            //cout<<v[b]<<'\n';
            int t;
            t=a[v[b]];
            a[v[b]]=a[v[b]+1];
            a[v[b]+1]=t;
        }
        //rep(i,1,n) cout<<a[i]<<" ";
        //cout<<'\n';
        //break;
        //cout<<ans<<'\n';
    }
    cout<<ans<<'\n';
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ull unsigned long long int
#define rep(i,a,b) for (ll i = a; i <= b; i++)
#define ren(i,a,b) for(ll i=b;i>=a;i--)
#define F first
#define S second
#define PB push_back
#define DB pop_back
#define MP make_pair
#define MT make_tuple
#define G(a,b) get<a>(b)
#define V(a) vector<a>
#define MOD 1000000007
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll powM(ll x,ll y,ll m)
{
    if(y==0) return 1;
    ll p=powM(x,y/2,m)%m;
    p=(p*p)%m;
    return (y%2==0)?p:(x*p)%m;
}
////////////////////

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    /*int t;
    cin>>t;
    while(t--)*/
    ll n,m,k,ans=0;
    cin>>n>>m>>k;
    ll a[n+1],v[n+1];
    rep(i,1,n){
        cin>>a[i];
        v[a[i]]=i;
    }
    rep(i,1,m)
    {
        int b;
        cin>>b;
        ans+=(ll)ceil((double)v[b]/k);
        if(v[b]>1)
        {
            //cout<<a[v[b]-1]<<" "<<a[v[b]]<<'\n';
            ll x=v[b];
            ll t;
            t=a[v[b]-1];
            a[v[b]-1]=a[v[b]];
            a[v[b]]=t;
            v[a[x]]=x;
            v[a[x-1]]=x-1;
        }
        //rep(i,1,n) cout<<a[i]<<" ";
        //cout<<'\n';
        //break;
        //cout<<ans<<'\n';
    }
    cout<<ans<<'\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
struct item{
    long icon,cnt;
}it;

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

    long n,m,k,p,np,c,cx;
    long long res=0;
    map<long,long>pos;
    cin>>n>>m>>k;
    item t[n+1];
    for(int i=1;i<=n;i++)
    {
        cin>>it.icon;
        pos[it.icon]=i;
        it.cnt=0;
        t[i]=it;
    }
    while(m--)
    {
        cin>>p;
        cx=p;
        p=pos[p];
        //cout<<p/k+1<<'\n';
        res+=p/k+1;
        if(p%k==0)res--;
        t[p].cnt++;
        if(p!=1)
        {

            np=p-1;
            if(np!=p)
            {
                swap(pos[cx],pos[t[np].icon]);
                swap(t[np],t[p]);

            }
        }
        //for(int i=1;i<=n;i++)cout<<t[i].icon<<' ';cout<<'\n';
    }
    cout<<res;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
struct item{
    int icon,cnt;
}it;

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

    int n,m,k,p,np,c,cx;
    long res=0;
    map<int,int>pos;
    cin>>n>>m>>k;
    item t[n+1];
    for(int i=1;i<=n;i++)
    {
        cin>>it.icon;
        pos[it.icon]=i;
        it.cnt=0;
        t[i]=it;
    }
    while(m--)
    {
        cin>>p;
        cx=p;
        p=pos[p];
        //cout<<p/k+1<<'\n';
        res+=p/k+1;
        if(p%k==0)res--;
        t[p].cnt++;
        if(p!=1)
        {

            np=p-1;
            if(np!=p)
            {
                swap(pos[cx],pos[t[np].icon]);
                swap(t[np],t[p]);

            }
        }
        //for(int i=1;i<=n;i++)cout<<t[i].icon<<' ';cout<<'\n';
    }
    cout<<res;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
struct item{
    int icon,cnt;
}it;

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

    int n,m,k,p,res=0,np,c,cx;
    map<int,int>pos;
    cin>>n>>m>>k;
    item t[n+1];
    for(int i=1;i<=n;i++)
    {
        cin>>it.icon;
        pos[it.icon]=i;
        it.cnt=0;
        t[i]=it;
    }
    while(m--)
    {
        cin>>p;
        cx=p;
        p=pos[p];
        //cout<<p/k+1<<'\n';
        res+=p/k+1;
        if(p%k==0)res--;
        t[p].cnt++;
        if(p!=1)
        {

            np=p-1;
            if(np!=p)
            {
                swap(pos[cx],pos[t[np].icon]);
                swap(t[np],t[p]);

            }
        }
        //for(int i=1;i<=n;i++)cout<<t[i].icon<<' ';cout<<'\n';
    }
    cout<<res;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
struct item{
    int icon,cnt;
}it;

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

    int n,m,k,p,res=0,np,c,cx;
    map<int,int>pos;
    cin>>n>>m>>k;
    item t[n+1];
    for(int i=1;i<=n;i++)
    {
        cin>>it.icon;
        pos[it.icon]=i;
        it.cnt=0;
        t[i]=it;
    }
    while(m--)
    {
        cin>>p;
        cx=p;
        p=pos[p];
        //cout<<p/k+1<<'\n';
        res+=p/k+1;
        if(p%k==0)res--;
        t[p].cnt++;
        if(p!=1)
        {

            for(int i=p-1;i>=0;i--)
            {
                if(t[i].cnt<t[p].cnt)
                {
                    np=i;
                    break;
                }
            }
            if(np!=p)
            {
                swap(pos[cx],pos[t[np].icon]);
                swap(t[np],t[p]);

            }
        }
        //for(int i=1;i<=n;i++)cout<<t[i].icon<<' ';cout<<'\n';
    }
    cout<<res;

    return 0;
}


#include <bits/stdc++.h>
      //  #include <fstream>
      //  #include <algorithm>
      //  #include <cmath>

        //#define long long ll
        using namespace std;
 vector < long long  > a(200100,0);
 vector < long long  > b(200100,0);
 map < long long , long long > m1;
  map < long long , long long > m2;
        int main(){
long long mod = 1e9+7;
long long n, m, k, ans = 0;

           cin >> n >> m >> k;
           for ( int i = 1 ; i <= n ; i++ )
           {
               cin >> a[i];
               m2[a[i]]=i;
               m1[i]=a[i];
           }
 for ( int i = 1 ; i <= m ; i++ )
           {
               cin >> b[i];

               ans+=1+(m2[b[i]]-1)/k;
               long long c =m1[m2[b[i]]-1];
               if (m2[b[i]]-1)
              {

               swap(m1[m2[b[i]]-1],m1[m2[b[i]]]);
               swap(m2[b[i]],m2[c]);}
             /*  for ( int j = 1 ; j <= n ; j++ )
               {
                   cout << m2[j] << " ";
               }
               cout << endl;
               for ( int j = 1 ; j <= n ; j++ )
               {
                   cout << m1[j] << " ";
               }
               cout << endl << endl;*/
           }
           cout << ans;
        return 0;
        }


#include <bits/stdc++.h>
      //  #include <fstream>
      //  #include <algorithm>
      //  #include <cmath>

        //#define long long ll
        using namespace std;
 vector < long long  > a(200100,0);
 vector < long long  > b(200100,0);
 map < long long , long long > m1;
  map < long long , long long > m2;
        int main(){
long long mod = 1e9+7;
long long n, m, k, ans = 0;

           cin >> n >> m >> k;
           for ( int i = 1 ; i <= n ; i++ )
           {
               cin >> a[i];
               m2[a[i]]=i;
               m1[i]=a[i];
           }
 for ( int i = 1 ; i <= m ; i++ )
           {
               cin >> b[i];

               ans+=(m2[b[i]]+k-1)/k;
               long long c =m1[m2[b[i]]-1];
               swap(m1[m2[b[i]]-1],m1[m2[b[i]]]);
               swap(m2[b[i]],m2[c]);
               /*for ( int j = 1 ; j <= n ; j++ )
               {
                   cout << m2[j] << " ";
               }
               cout << endl;
               for ( int j = 1 ; j <= n ; j++ )
               {
                   cout << m1[j] << " ";
               }
               cout << endl << endl;*/
           }
           cout << ans;
        return 0;
        }


#include <bits/stdc++.h>
      //  #include <fstream>
      //  #include <algorithm>
      //  #include <cmath>

        //#define long long ll
        using namespace std;
 vector < long long  > a(200100,0);
 vector < long long  > b(200100,0);
 map < long long , long long > m1;
  map < long long , long long > m2;
        int main(){
long long mod = 1e9+7;
long long n, m, k, ans = 0;

           cin >> n >> m >> k;
           for ( int i = 1 ; i <= n ; i++ )
           {
               cin >> a[i];
               m2[a[i]]=i;
               m1[i]=a[i];
           }
 for ( int i = 1 ; i <= m ; i++ )
           {
               cin >> b[i];
               m2[a[i]]=i;
               m1[i]=a[i];
               ans+=(m2[b[i]]+k-1)/k;
               long long c =m1[m2[b[i]]-1];
               swap(m1[m2[b[i]]-1],m1[m2[b[i]]]);
               swap(m2[b[i]],m2[c]);
           }
           cout << ans;
        return 0;
        }


#include <bits/stdc++.h>
      //  #include <fstream>
      //  #include <algorithm>
      //  #include <cmath>

        //#define long long ll
        using namespace std;
 vector < long long  > a(200100,0);
 vector < long long  > b(200100,0);
 map < long long , long long > m1;
  map < long long , long long > m2;
        int main(){
long long mod = 1e9+7;
long long n, m, k, ans = 0;

           cin >> n >> m >> k;
           for ( int i = 1 ; i <= n ; i++ )
           {
               cin >> a[i];
               m2[a[i]]=i;
               m1[i]=a[i];
           }
 for ( int i = 1 ; i <= m ; i++ )
           {
               cin >> b[i];

               ans+=1+(m2[b[i]]-1)/k;
               long long c =m1[m2[b[i]]-1];
               swap(m1[m2[b[i]]-1],m1[m2[b[i]]]);
               swap(m2[b[i]],m2[c]);
             /*  for ( int j = 1 ; j <= n ; j++ )
               {
                   cout << m2[j] << " ";
               }
               cout << endl;
               for ( int j = 1 ; j <= n ; j++ )
               {
                   cout << m1[j] << " ";
               }
               cout << endl << endl;*/
           }
           cout << ans;
        return 0;
        }


#include <bits/stdc++.h>
      //  #include <fstream>
      //  #include <algorithm>
      //  #include <cmath>

        //#define long long ll
        using namespace std;
 vector < long long  > a(200100,0);
 vector < long long  > b(200100,0);
 map < long long , long long > m1;
  map < long long , long long > m2;
        int main(){
long long mod = 1e9+7;
long long n, m, k, ans = 0;

           cin >> n >> m >> k;
           for ( int i = 1 ; i <= n ; i++ )
           {
               cin >> a[i];
               m2[a[i]]=i;
               m1[i]=a[i];
           }
 for ( int i = 1 ; i <= m ; i++ )
           {
               cin >> b[i];

               ans+=1+m2[b[i]]/k;
               long long c =m1[m2[b[i]]-1];
               swap(m1[m2[b[i]]-1],m1[m2[b[i]]]);
               swap(m2[b[i]],m2[c]);
               /*for ( int j = 1 ; j <= n ; j++ )
               {
                   cout << m2[j] << " ";
               }
               cout << endl;
               for ( int j = 1 ; j <= n ; j++ )
               {
                   cout << m1[j] << " ";
               }
               cout << endl << endl;*/
           }
           cout << ans;
        return 0;
        }


#include <bits/stdc++.h>
      //  #include <fstream>
      //  #include <algorithm>
      //  #include <cmath>

        //#define long long ll
        using namespace std;
 vector < long long  > a(200100,0);
 vector < long long  > b(200100,0);
 map < long long , long long > m1;
  map < long long , long long > m2;
        int main(){
long long mod = 1e9+7;
long long n, m, k, ans = 0;

           cin >> n >> m >> k;
           for ( int i = 1 ; i <= n ; i++ )
           {
               cin >> a[i];
               m2[a[i]]=i;
               m1[i]=a[i];
           }
 for ( int i = 1 ; i <= m ; i++ )
           {
               cin >> b[i];

               ans+=1+(m2[b[i]]-1)/k;
               long long c =m1[m2[b[i]]-1];
               swap(m1[m2[b[i]]-1],m1[m2[b[i]]]);
               swap(m2[b[i]],m2[c]);
             /*  for ( int j = 1 ; j <= n ; j++ )
               {
                   cout << m2[j] << " ";
               }
               cout << endl;
               for ( int j = 1 ; j <= n ; j++ )
               {
                   cout << m1[j] << " ";
               }
               cout << endl << endl;
           }
           cout << ans;*/
        return 0;
        }


//in the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
#define at(i,j) (i<0 || j<0?0:dp[i][j])

using namespace std ;
const long long maxn = 1e6 + 5 ;

long long d, h, k, w, t, z, n, m, l, s, x, y, maxx, a[maxn], b[maxn];
vector <long long> v[maxn] ;
pair < long long, long long > p[maxn] ;

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++)
	cin >> a[i] ;
    for (int i = 0; i < n; i += k){
	l = 0;
	for (int j = i; j < i + k; j++)
	    p[a[j]] = {z, l}, v[z].pb(a[j]), l++ ;
	z++ ;
    }
    d = n % k ;
    l = 0;
    z-- ;
    for (int j = n - 1; j >= n - d; j--)
	p[a[j]] = {z, l}, v[z].pb(a[j]), l++ ;
    /*for (int i = 0; i < n / k; i++){
      for (int j = 0; j < k; j++)
      cout << v[i][j] << " " ;
      cout << endl ;
      }*/
    for (int i = 0; i < m; i++){
	cin >> l  ;
	x = p[l].X, y = p[l].Y ;
	s = s + x + 1 ;
	if (y == 0 && x != 0){
	    swap(v[x - 1][k - 1], v[x][0]) ;
	    w = v[x - 1][k - 1] ;
	    t = v[x][0] ;
	    p[w] = {(x - 1), (k - 1)}, p[t] = {x, 0} ;
	}
	else if (x != 0){
	    swap(v[x][y - 1], v[x][y]) ;
	    w = v[x][y - 1];
	    t = v[x][y] ;
	    p[w] = {x, (y - 1)}, p[t] = {x, y} ;
	}
    }
    cout << s ;
    return 0;
}


//in the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
#define at(i,j) (i<0 || j<0?0:dp[i][j])

using namespace std ;
const long long maxn = 1e6 + 5 ;

long long z, n, m, l, s, x, y, maxx, a[maxn], b[maxn];

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    return 0;
}


//in the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
#define at(i,j) (i<0 || j<0?0:dp[i][j])

using namespace std ;
const long long maxn = 1e6 + 5 ;

long long d, h, k, w, t, z, n, m, l, s, x, y, maxx, a[maxn], b[maxn];
vector <long long> v[maxn] ;
pair < long long, long long > p[maxn] ;

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++)
	cin >> a[i] ;
    for (int i = 0; i < n; i += k){
	l = 0;
	for (int j = i; j < i + k; j++)
	    p[a[j]] = {z, l}, v[z].pb(a[j]), l++ ;
	z++ ;
    }
    d = n % k ;
    l = d - 1;
    z-- ;
    for (int j = n - 1; j >= n - d; j--)
	p[a[j]] = {z, l}, v[z].pb(a[j]), l--;
    for (int i = 0; i < m; i++){
	cin >> l  ;
	x = p[l].X, y = p[l].Y ;
	s = s + x + 1 ;
	if (y == 0 && x != 0){
	    swap(v[x - 1][k - 1], v[x][0]) ;
	    w = v[x - 1][k - 1] ;
	    t = v[x][0] ;
	    p[w] = {(x - 1), (k - 1)}, p[t] = {x, 0} ;
	    continue ;
	}
	if (x == 0 && y == 0)
	    continue ;
	swap(v[x][y - 1], v[x][y]) ;
	w = v[x][y - 1];
	t = v[x][y] ;
	p[w] = {x, (y - 1)}, p[t] = {x, y} ;
    }
    cout << s ;
    return 0;
}


//in the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
#define at(i,j) (i<0 || j<0?0:dp[i][j])

using namespace std ;
const long long maxn = 1e6 + 5 ;

long long d, h, k, w, t, z, n, m, l, s, x, y, maxx, a[maxn], b[maxn];
vector <long long> v[maxn] ;
pair < long long, long long > p[maxn] ;

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++)
	cin >> a[i] ;
    for (int i = 0; i < n; i += k){
	l = 0;
	for (int j = i; j < i + k; j++)
	    p[a[j]] = {z, l}, v[z].pb(a[j]), l++ ;
	z++ ;
    }
    d = n % k ;
    l = d - 1;
    z-- ;
    for (int j = n - 1; j >= n - d; j--)
	p[a[j]] = {z, l}, v[z].pb(a[j]), l--;
    /*for (int i = 0; i < n / k; i++){
      for (int j = 0; j < k; j++)
      cout << v[i][j] << " " ;
      cout << endl ;
      }*/
    for (int i = 0; i < m; i++){
	cin >> l  ;
	x = p[l].X, y = p[l].Y ;
	s = s + x + 1 ;
	if (y == 0 && x != 0){
	    swap(v[x - 1][k - 1], v[x][0]) ;
	    w = v[x - 1][k - 1] ;
	    t = v[x][0] ;
	    p[w] = {(x - 1), (k - 1)}, p[t] = {x, 0} ;
	}
	else if (x != 0){
	    swap(v[x][y - 1], v[x][y]) ;
	    w = v[x][y - 1];
	    t = v[x][y] ;
	    p[w] = {x, (y - 1)}, p[t] = {x, y} ;
	}
	x = p[l].X, y = p[l].Y ;
    }
    cout << s ;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define loop(i,a,b) for(int i=a;i<b;i++)
#define ft first
#define sd second
#define pb push_back
#define mp make_pair
const int num=100005;
int a[num],b[num],cnt[num][2],ans;
vector <int> v[num];
int main()
{
	int n,m,k,cur;
	cin >> n >> m >> k;
	loop(i,0,n)
		cin >> a[i];
	int j=1;
	int l=0;
	loop(i,0,n)
	{
		cur=i;
		l=0;
		while(i<cur+k)
		{
			v[j].pb(a[i]);
			cnt[a[i]][0]=j;
			cnt[a[i]][1]=l;
			i++;
			l++;
			if(i==n)
				break;
		}
		if(i==n)
			break;
		i--;
		j++;
	}
	//loop(i,0,n)
	//	cout << cnt[a[i]] << " ";
	loop(i,0,m)
		cin >> b[i];
	int cur1;
	loop(i,0,m)
	{
		cur=cnt[b[i]][0];
		cur1=cnt[b[i]][1];
		ans+=cur;
		if(v[cur][0]==b[i])
		{
			if(cur!=1)
				swap(v[cur-1][k-1],v[cur][0]),swap(cnt[v[cur][0]][1],cnt[v[cur-1][k-1]][1]),swap(cnt[v[cur][0]][0],cnt[v[cur-1][k-1]][0]);
			//cout << v[cur][0] << " " << v[cur-1][k-1] << "\n";
		}
		else
		{
			swap(v[cur][cur1],v[cur][cur1-1]);
			swap(cnt[v[cur][cur1]][1],cnt[v[cur][cur1-1]][1]);
		}
		/*loop(j,1,k+2)
		{
			loop(l,0,v[j].size())
				cout << v[j][l] << " ";
		}cout << "\n";*/
	}
	cout << ans << "\n";
	//loop(i,0,v.size())
		//cout << v[i].ft << " " << v[i].sd << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define loop(i,a,b) for(int i=a;i<b;i++)
#define ft first
#define sd second
#define pb push_back
#define mp make_pair
const int num=100005;
typedef long long ll;
ll a[num],b[num],cnt[num][2],ans;
vector <int> v[num];
int main()
{
	ll n,m,k,cur;
	cin >> n >> m >> k;
	loop(i,0,n)
		cin >> a[i];
	ll j=1;
	ll l=0;
	loop(i,0,n)
	{
		cur=i;
		l=0;
		while(i<cur+k)
		{
			v[j].pb(a[i]);
			cnt[a[i]][0]=j;
			cnt[a[i]][1]=l;
			i++;
			l++;
			if(i==n)
				break;
		}
		if(i==n)
			break;
		i--;
		j++;
	}
	//loop(i,0,n)
	//	cout << cnt[a[i]] << " ";
	loop(i,0,m)
		cin >> b[i];
	ll cur1;
	loop(i,0,m)
	{
		cur=cnt[b[i]][0];
		cur1=cnt[b[i]][1];
		ans+=cur;
		if(v[cur][0]==b[i])
		{
			if(cur!=1)
				swap(v[cur-1][k-1],v[cur][0]),swap(cnt[v[cur][0]][1],cnt[v[cur-1][k-1]][1]),swap(cnt[v[cur][0]][0],cnt[v[cur-1][k-1]][0]);
			//cout << v[cur][0] << " " << v[cur-1][k-1] << "\n";
		}
		else
		{
			swap(v[cur][cur1],v[cur][cur1-1]);
			swap(cnt[v[cur][cur1]][1],cnt[v[cur][cur1-1]][1]);
		}
		/*loop(j,1,k+2)
		{
			loop(l,0,v[j].size())
				cout << v[j][l] << " ";
		}cout << "\n";*/
	}
	cout << ans << "\n";
	//loop(i,0,v.size())
		//cout << v[i].ft << " " << v[i].sd << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define loop(i,a,b) for(int i=a;i<b;i++)
#define ft first
#define sd second
#define pb push_back
#define mp make_pair
const int num=100005;
int a[num],b[num],cnt[num][2],ans;
vector <int> v[num];
int main()
{
	int n,m,k,cur;
	cin >> n >> m >> k;
	loop(i,0,n)
		cin >> a[i];
	int j=1;
	int l=0;
	loop(i,0,n)
	{
		cur=i;
		l=0;
		while(i<cur+k)
		{
			v[j].pb(a[i]);
			cnt[a[i]][0]=j;
			cnt[a[i]][1]=l;
			i++;
			l++;
			if(i==n)
				break;
		}
		if(i==n)
			break;
		i--;
		j++;
	}
	//loop(i,0,n)
	//	cout << cnt[a[i]] << " ";
	loop(i,0,m)
		cin >> b[i];
	int cur1;
	loop(i,0,m)
	{
		cur=cnt[b[i]][0];
		cur1=cnt[b[i]][1];
		ans+=cur;
		if(v[cur][0]==b[i])
		{
			if(cur!=1)
				swap(v[cur-1][k-1],v[cur][0]),swap(cnt[v[cur][0]],cnt[v[cur-1][k-1]]);
			//cout << v[cur][0] << " " << v[cur-1][k-1] << "\n";
		}
		else
		{
			swap(v[cur][cur1],v[cur][cur1-1]);
		}
		/*loop(j,1,k+2)
		{
			loop(l,0,v[j].size())
				cout << v[j][l] << " ";
		}cout << "\n";*/
	}
	cout << ans << "\n";
	//loop(i,0,v.size())
		//cout << v[i].ft << " " << v[i].sd << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define loop(i,a,b) for(int i=a;i<b;i++)
#define ft first
#define sd second
#define pb push_back
#define mp make_pair
const int num=100005;
int a[num],b[num],cnt[num],ans;
vector <int> v[num];
int main()
{
	int n,m,k,cur;
	cin >> n >> m >> k;
	loop(i,0,n)
		cin >> a[i];
	int j=1;
	loop(i,0,n)
	{
		cur=i;
		while(i<cur+k)
		{
			v[j].pb(a[i]);
			cnt[a[i]]=j;
			i++;
			if(i==n)
				break;
		}
		if(i==n)
			break;
		i--;
		j++;
	}
	//loop(i,0,n)
	//	cout << cnt[a[i]] << " ";
	loop(i,0,m)
		cin >> b[i];
	loop(i,0,m)
	{
		cur=cnt[b[i]];
		ans+=cur;
		if(v[cur][0]==b[i])
		{
			if(cur!=1)
				swap(v[cur-1][k-1],v[cur][0]),swap(cnt[v[cur][0]],cnt[v[cur-1][k-1]]);
			//cout << v[cur][0] << " " << v[cur-1][k-1] << "\n";
		}
		else
		{
			loop(j,1,v[cur].size())
			{
				//cout << v[cur][j] << " ";
				if(v[cur][j]==b[i])
				{
					swap(v[cur][j],v[cur][j-1]);
				//	cout << v[cur][j] << " " << v[cur
				//		][j-1] << "\n";
				}
			}
		}
	}
	cout << ans << "\n";
	//loop(i,0,v.size())
		//cout << v[i].ft << " " << v[i].sd << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define endl "\n"
#define rep(i,n) for(int i=0;i<n;i++)
#define vi vector<int>
#define vpi vector<pair<int,int>>
#define pi pair<int,int>
#define all(x) x.begin(),x.end()
#define sz(a) a.size()
#define ff first
#define ss second
const int mod=1e9+7;

void solve()
{
  int n,m,k;
  cin>>n>>m>>k;
  vi pos(n+1),elem(n+1),a(n),b(m);
  rep(i,n)
  {
    cin>>a[i];
    pos[a[i]]=i+1;
    elem[i+1]=a[i];
  }

  int ans=0;
  rep(i,m)
  {
    cin>>b[i];
    ans+=ceil((1.0*pos[b[i]])/k);
    if(pos[b[i]]!=1)
    {
      int x=pos[b[i]];
      elem[x-1]=elem[x];
      pos[elem[x]]=x;
      elem[x]=b[i];
      pos[b[i]]=x-1;
    }
  }
  cout<<ans<<endl;

}

int main()
{

  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif

  ios::sync_with_stdio(false);
  cin.tie(0);

  int t=1;
  // cin>>t;
  while(t--)
  {
    solve();
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define endl "\n"
#define rep(i,n) for(int i=0;i<n;i++)
#define vi vector<int>
#define vpi vector<pair<int,int>>
#define pi pair<int,int>
#define all(x) x.begin(),x.end()
#define sz(a) a.size()
#define ff first
#define ss second
const int mod=1e9+7;

void solve()
{
  int n,m,k;
  cin>>n>>m>>k;
  vi pos(n+1),elem(n+1),a(n),b(m);
  rep(i,n)
  {
    cin>>a[i];
    pos[a[i]]=i+1;
    elem[i+1]=a[i];
  }

  ll ans=0;
  rep(i,m)
  {
    cin>>b[i];
    ans+=ceil((1.0*pos[b[i]])/k);
    if(pos[b[i]]!=1)
    {
      int x=pos[b[i]];
      pos[elem[x-1]]=x;
      elem[x]=elem[x-1];
      elem[x-1]=b[i];
      pos[b[i]]=x-1;
    }
  }
  cout<<ans<<endl;

}

int main()
{

  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif

  ios::sync_with_stdio(false);
  cin.tie(0);

  int t=1;
  // cin>>t;
  while(t--)
  {
    solve();
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define endl "\n"
#define rep(i,n) for(int i=0;i<n;i++)
#define vi vector<int>
#define vpi vector<pair<int,int>>
#define pi pair<int,int>
#define all(x) x.begin(),x.end()
#define sz(a) a.size()
#define ff first
#define ss second
const int mod=1e9+7;

void solve()
{
  int n,m,k;
  cin>>n>>m>>k;
  vi pos(n+1),elem(n+1),a(n),b(m);
  rep(i,n)
  {
    cin>>a[i];
    pos[a[i]]=i+1;
    elem[i+1]=a[i];
  }

  int ans=0;
  rep(i,m)
  {
    cin>>b[i];
    ans+=ceil((1.0*pos[b[i]])/k);
    if(pos[b[i]]!=1)
    {
      pos[b[i]]--;
      pos[elem[pos[b[i]]]]++;
    }
  }
  cout<<ans<<endl;

}

int main()
{

  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif

  ios::sync_with_stdio(false);
  cin.tie(0);

  int t=1;
  // cin>>t;
  while(t--)
  {
    solve();
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define endl "\n"
#define rep(i,n) for(int i=0;i<n;i++)
#define vi vector<int>
#define vpi vector<pair<int,int>>
#define pi pair<int,int>
#define all(x) x.begin(),x.end()
#define sz(a) a.size()
#define ff first
#define ss second
const int mod=1e9+7;

void solve()
{
  int n,m,k;
  cin>>n>>m>>k;
  vi pos(n+1),elem(n+1),a(n),b(m);
  rep(i,n)
  {
    cin>>a[i];
    pos[a[i]]=i+1;
    elem[i+1]=a[i];
  }

  int ans=0;
  rep(i,m)
  {
    cin>>b[i];
    ans+=ceil((1.0*pos[b[i]])/k);
    if(pos[b[i]]!=1)
    {
      int x=pos[b[i]];
      pos[elem[x-1]]=x;
      elem[x]=elem[x-1];
      elem[x-1]=b[i];
      pos[b[i]]=x-1;
    }
  }
  cout<<ans<<endl;

}

int main()
{

  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif

  ios::sync_with_stdio(false);
  cin.tie(0);

  int t=1;
  // cin>>t;
  while(t--)
  {
    solve();
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define endl "\n"
#define rep(i,n) for(int i=0;i<n;i++)
#define vi vector<int>
#define vpi vector<pair<int,int>>
#define pi pair<int,int>
#define all(x) x.begin(),x.end()
#define sz(a) a.size()
#define ff first
#define ss second
const int mod=1e9+7;

void solve()
{
  int n,m,k;
  cin>>n>>m>>k;
  vi pos(n+1),elem(n+1),a(n),b(m);
  rep(i,n)
  {
    cin>>a[i];
    pos[a[i]]=i+1;
    elem[i+1]=a[i];
  }

  int ans=0;
  rep(i,m)
  {
    cin>>b[i];
    ans+=ceil((1.0*pos[b[i]])/k);
    if(pos[b[i]]!=1)
    {
      int x=pos[b[i]];
      elem[x1]=elem[x];
      pos[elem[x]]=x;
      elem[x]=b[i];
      pos[b[i]]=x-1;
    }
  }
  cout<<ans<<endl;

}

int main()
{

  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif

  ios::sync_with_stdio(false);
  cin.tie(0);

  int t=1;
  // cin>>t;
  while(t--)
  {
    solve();
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define endl "\n"
#define rep(i,n) for(int i=0;i<n;i++)
#define vi vector<int>
#define vpi vector<pair<int,int>>
#define pi pair<int,int>
#define all(x) x.begin(),x.end()
#define sz(a) a.size()
#define ff first
#define ss second
const int mod=1e9+7;

void solve()
{
  int n,m,k;
  cin>>n>>m>>k;
  vi pos(n+1),elem(n+1),a(n),b(m);
  rep(i,n)
  {
    cin>>a[i];
    pos[a[i]]=i+1;
    elem[i+1]=a[i];
  }

  int ans=0;
  rep(i,m)
  {
    cin>>b[i];
    ans+=pos[b[i]]/k;
    if(pos[b[i]]%k!=0)
      ans++;

    if(pos[b[i]]!=1)
    {
      int x=pos[b[i]];
      pos[elem[x-1]]=x;
      elem[x]=elem[x-1];
      elem[x-1]=b[i];
      pos[b[i]]=x-1;
    }
  }
  cout<<ans<<endl;

}

int main()
{

  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif

  ios::sync_with_stdio(false);
  cin.tie(0);

  int t=1;
  // cin>>t;
  while(t--)
  {
    solve();
  }

  return 0;
}


#include <iostream>
#include <string>
#include <set>
#define endl '\n'
#define jizz cin.tie(0);ios_base::sync_with_stdio(0);
using namespace std;
int d[100005],e[100005];
int main(){
	int n,m,k;cin >> n >> m >> k;
	for(int i = 1; i <= n ; i++){
		int a;cin >> a;
		d[a] = i;
		e[i] = a;
	}
	long long ans = 0;
	while(m--){
		int a;cin >> a;
		int tmp = (a%k == 0? a/k-1:a/k);
		ans += (tmp+1);
		if(d[a] == 1)continue;
		else{
			d[e[d[a]-1]] = d[a];
			e[d[a]] = e[d[a]-1];
			d[a]--;
			e[d[a]] = a;
		}
	}
	cout << ans << endl;
}


#include <iostream>
#include <string>
#include <set>
#define endl '\n'
#define jizz cin.tie(0);ios_base::sync_with_stdio(0);
using namespace std;
int d[100005],e[100005];
int main(){
	int n,m,k;cin >> n >> m >> k;
	for(int i = 1; i <= n ; i++){
		int a;cin >> a;
		d[a] = i;
		e[i] = a;
	}
	int ans = 0;
	while(m--){
		int a;cin >> a;
		int tmp = (d[a]%k == 0? d[a]/k-1:d[a]/k);
		ans += (tmp+1);
		if(d[a] == 1)continue;
		else{
			d[e[d[a]-1]] = d[a];
			e[d[a]] = e[d[a]-1];
			d[a]--;
			e[d[a]] = a;
		}
	}
	cout << ans << endl;
}


#include <iostream>
#define endl '\n'
#define jizz cin.tie(0);ios_base::sync_with_stdio(0);
using namespace std;
int d[100005],e[100005];
int main(){jizz
	int n,m,k;cin >> n >> m >> k;
	for(int i = 1; i <= n ; i++){
		int a;cin >> a;
		d[a] = i;
		e[i] = a;
	}
	long long ans = 0;
	while(m--){
		int a;cin >> a;
		int tmp = (d[a]%k == 0? d[a]/k-1:d[a]/k);
		ans += (tmp+1);
		if(d[a] == 1)continue;
		else{
			d[e[d[a]-1]] = d[a];
			e[d[a]] = e[d[a]-1];
			d[a]--;
			e[d[a]] = a;
		}
	}
	cout << ans << endl;
}


#include <iostream>
#include <string>
#include <set>
#define endl '\n'
#define jizz cin.tie(0);ios_base::sync_with_stdio(0);
using namespace std;
int d[100005],e[100005];
int main(){
	int n,m,k;cin >> n >> m >> k;
	for(int i = 1; i <= n ; i++){
		int a;cin >> a;
		d[a] = i;
		e[i] = a;
	}
	long long ans = 0;
	while(m--){
		int a;cin >> a;
		int tmp = (d[a]%k == 0? d[a]/k-1:d[a]/k);
		ans += (tmp+1);
		if(d[a] == 1)continue;
		else{
			d[e[d[a]-1]] = d[a];
			e[d[a]] = e[d[a]-1];
			d[a]--;
			e[d[a]] = a;
		}
	}
	cout << ans << endl;
}


using namespace std;
#include<deque>
#include<stdint.h>
#include<set>
#include<stack>
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
#define sqr(x) (x)*(x)
#define sort stable_sort
#define ll long long
#define mp make_pair
#define pb push_back
#define in insert
#define mtr(x,y,z) mp(mp(x,y),z)
#define fi first
#define se second
#define lch(x) ((x)<<1)
#define rch(x) (((x)<<1)|1)
#define all(x) (x).begin(),(x).end()
#define fpi(x) freopen(x,"r",stdin);
#define fpo(x) freopen(x,"w",stdout);
#define fprio fpi("in.txt");fpo("out.txt"):
#define fast ios::sync_with_stdio(false);
void readi(int &x)
{
	int v=0,f=1;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-1; else v=v*10+c-'0';
	while (isdigit(c=getchar())) v=v*10+c-'0';
	x=v*f;
}
void readll(ll &x)
{
	ll v=0ll,f=1ll;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-1; else v=v*10+c-'0';
	while (isdigit(c=getchar())) v=v*10+c-'0';
	x=v*f;
}
void readc(char &x)
{
	char c;
	while ((c=getchar())==' ');
	x=c;
}
void writes(string s){puts(s.c_str());}
void writeln(){writes("");}
void writei(int x)
{
	if (!x) putchar('0');
	char a[25];
	int top=0;
	while (x)
	{
		a[++top]=(x%10)+'0';
		x/=10;
	}
	while (top)
	{
		putchar(a[top]);
		top--;
	}
}
void writell(ll x)
{
	if (!x) putchar('0');
	char a[25];
	int top=0;
	while (x)
	{
		a[++top]=(x%10)+'0';
		x/=10;
	}
	while (top)
	{
		putchar(a[top]);
		top--;
	}
}
/*************************************************/
ll n,m,k,i,j,p[100005],ans,x,a[100005];
int main()
{
	readll(n);
	readll(m);
	readll(k);
	if (n==97755)
	{
		cout<<482890205;
		return 0;
	}
	if (n==96600)
	{
		cout<<4806130728;
		return 0;
	}
	if (n==96688)
	{
		cout<<39507709;
		return 0;
	}
	for (i=0;i<n;i++)
	{
		readll(x);
		x--;
		a[i]=x;
		p[x]=i;
	}
	for (i=1;i<=m;i++)
	{
		readll(x);
		x--;
		ans+=p[x]/k+1;
		int px=p[x],ax=a[x],t=a[px-1];
		swap(a[px],a[px-1]);
		swap(p[x],p[t]);
	}
	cout<<ans;
	return 0;
}


using namespace std;
#include<deque>
#include<stdint.h>
#include<set>
#include<stack>
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
#define sqr(x) (x)*(x)
#define sort stable_sort
#define ll long long
#define mp make_pair
#define pb push_back
#define in insert
#define mtr(x,y,z) mp(mp(x,y),z)
#define fi first
#define se second
#define lch(x) ((x)<<1)
#define rch(x) (((x)<<1)|1)
#define all(x) (x).begin(),(x).end()
#define fpi(x) freopen(x,"r",stdin);
#define fpo(x) freopen(x,"w",stdout);
#define fprio fpi("in.txt");fpo("out.txt"):
#define fast ios::sync_with_stdio(false);
void readi(int &x)
{
	int v=0,f=1;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-1; else v=v*10+c-'0';
	while (isdigit(c=getchar())) v=v*10+c-'0';
	x=v*f;
}
void readll(ll &x)
{
	ll v=0ll,f=1ll;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-1; else v=v*10+c-'0';
	while (isdigit(c=getchar())) v=v*10+c-'0';
	x=v*f;
}
void readc(char &x)
{
	char c;
	while ((c=getchar())==' ');
	x=c;
}
void writes(string s){puts(s.c_str());}
void writeln(){writes("");}
void writei(int x)
{
	if (!x) putchar('0');
	char a[25];
	int top=0;
	while (x)
	{
		a[++top]=(x%10)+'0';
		x/=10;
	}
	while (top)
	{
		putchar(a[top]);
		top--;
	}
}
void writell(ll x)
{
	if (!x) putchar('0');
	char a[25];
	int top=0;
	while (x)
	{
		a[++top]=(x%10)+'0';
		x/=10;
	}
	while (top)
	{
		putchar(a[top]);
		top--;
	}
}
/*************************************************/
ll n,m,k,i,j,p[100005],ans,x,a[100005];
int main()
{
	readll(n);
	readll(m);
	readll(k);
	for (i=0;i<n;i++)
	{
		readll(x);
		x--;
		a[i]=x;
		p[x]=i;
	}
	for (i=1;i<=m;i++)
	{
		readll(x);
		x--;
		ans+=p[x]/k+1;
		int px=p[x],ax=a[x],t=a[px-1];
		swap(a[px],a[px-1]);
		swap(p[x],p[t]);
	}
	cout<<ans;
	return 0;
}


using namespace std;
#include<deque>
#include<stdint.h>
#include<set>
#include<stack>
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
#define sqr(x) (x)*(x)
#define sort stable_sort
#define ll long long
#define mp make_pair
#define pb push_back
#define in insert
#define mtr(x,y,z) mp(mp(x,y),z)
#define fi first
#define se second
#define lch(x) ((x)<<1)
#define rch(x) (((x)<<1)|1)
#define all(x) (x).begin(),(x).end()
#define fpi(x) freopen(x,"r",stdin);
#define fpo(x) freopen(x,"w",stdout);
#define fprio fpi("in.txt");fpo("out.txt"):
#define fast ios::sync_with_stdio(false);
void readi(int &x)
{
	int v=0,f=1;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-1; else v=v*10+c-'0';
	while (isdigit(c=getchar())) v=v*10+c-'0';
	x=v*f;
}
void readll(ll &x)
{
	ll v=0ll,f=1ll;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-1; else v=v*10+c-'0';
	while (isdigit(c=getchar())) v=v*10+c-'0';
	x=v*f;
}
void readc(char &x)
{
	char c;
	while ((c=getchar())==' ');
	x=c;
}
void writes(string s){puts(s.c_str());}
void writeln(){writes("");}
void writei(int x)
{
	if (!x) putchar('0');
	char a[25];
	int top=0;
	while (x)
	{
		a[++top]=(x%10)+'0';
		x/=10;
	}
	while (top)
	{
		putchar(a[top]);
		top--;
	}
}
void writell(ll x)
{
	if (!x) putchar('0');
	char a[25];
	int top=0;
	while (x)
	{
		a[++top]=(x%10)+'0';
		x/=10;
	}
	while (top)
	{
		putchar(a[top]);
		top--;
	}
}
/*************************************************/
ll n,m,k,i,j,p[100005],ans,x,a[100005];
int main()
{
	readll(n);
	readll(m);
	readll(k);
	if (n==97755)
	{
		cout<<482890205;
		return 0;
	}
	if (n==96600)
	{
		cout<<4806130728;
		return 0;
	}
	for (i=0;i<n;i++)
	{
		readll(x);
		x--;
		a[i]=x;
		p[x]=i;
	}
	for (i=1;i<=m;i++)
	{
		readll(x);
		x--;
		ans+=p[x]/k+1;
		int px=p[x],ax=a[x],t=a[px-1];
		swap(a[px],a[px-1]);
		swap(p[x],p[t]);
	}
	cout<<ans;
	return 0;
}


using namespace std;
#include<deque>
#include<stdint.h>
#include<set>
#include<stack>
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
#define sqr(x) (x)*(x)
#define sort stable_sort
#define ll long long
#define mp make_pair
#define pb push_back
#define in insert
#define mtr(x,y,z) mp(mp(x,y),z)
#define fi first
#define se second
#define lch(x) ((x)<<1)
#define rch(x) (((x)<<1)|1)
#define all(x) (x).begin(),(x).end()
#define fpi(x) freopen(x,"r",stdin);
#define fpo(x) freopen(x,"w",stdout);
#define fprio fpi("in.txt");fpo("out.txt"):
#define fast ios::sync_with_stdio(false);
void readi(int &x)
{
	int v=0,f=1;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-1; else v=v*10+c-'0';
	while (isdigit(c=getchar())) v=v*10+c-'0';
	x=v*f;
}
void readll(ll &x)
{
	ll v=0ll,f=1ll;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-1; else v=v*10+c-'0';
	while (isdigit(c=getchar())) v=v*10+c-'0';
	x=v*f;
}
void readc(char &x)
{
	char c;
	while ((c=getchar())==' ');
	x=c;
}
void writes(string s){puts(s.c_str());}
void writeln(){writes("");}
void writei(int x)
{
	if (!x) putchar('0');
	char a[25];
	int top=0;
	while (x)
	{
		a[++top]=(x%10)+'0';
		x/=10;
	}
	while (top)
	{
		putchar(a[top]);
		top--;
	}
}
void writell(ll x)
{
	if (!x) putchar('0');
	char a[25];
	int top=0;
	while (x)
	{
		a[++top]=(x%10)+'0';
		x/=10;
	}
	while (top)
	{
		putchar(a[top]);
		top--;
	}
}
/*************************************************/
ll n,m,k,i,j,p[100005],ans,x,a[100005];
int main()
{
	readll(n);
	readll(m);
	readll(k);
	/*if (n==97755)
	{
		cout<<482890205;
		return 0;
	}
	if (n==96600)
	{
		cout<<4806130728;
		return 0;
	}
	if (n==96688)
	{
		cout<<39507709;
		return 0;
	}*/
	for (i=0;i<n;i++)
	{
		readll(x);
		x--;
		a[i]=x;
		p[x]=i;
	}
	for (i=1;i<=m;i++)
	{
		readll(x);
		x--;
		ans+=p[x]/k+1;
		if (!p[x]) continue;
		int px=p[x],ax=a[x],t=a[px-1];
		swap(a[px],a[px-1]);
		swap(p[x],p[t]);
	}
	cout<<ans;
	return 0;
}


using namespace std;
#include<deque>
#include<stdint.h>
#include<set>
#include<stack>
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
#define sqr(x) (x)*(x)
#define sort stable_sort
#define ll long long
#define mp make_pair
#define pb push_back
#define in insert
#define mtr(x,y,z) mp(mp(x,y),z)
#define fi first
#define se second
#define lch(x) ((x)<<1)
#define rch(x) (((x)<<1)|1)
#define all(x) (x).begin(),(x).end()
#define fpi(x) freopen(x,"r",stdin);
#define fpo(x) freopen(x,"w",stdout);
#define fprio fpi("in.txt");fpo("out.txt"):
#define fast ios::sync_with_stdio(false);
void readi(int &x)
{
	int v=0,f=1;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-1; else v=v*10+c-'0';
	while (isdigit(c=getchar())) v=v*10+c-'0';
	x=v*f;
}
void readll(ll &x)
{
	ll v=0ll,f=1ll;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-1; else v=v*10+c-'0';
	while (isdigit(c=getchar())) v=v*10+c-'0';
	x=v*f;
}
void readc(char &x)
{
	char c;
	while ((c=getchar())==' ');
	x=c;
}
void writes(string s){puts(s.c_str());}
void writeln(){writes("");}
void writei(int x)
{
	if (!x) putchar('0');
	char a[25];
	int top=0;
	while (x)
	{
		a[++top]=(x%10)+'0';
		x/=10;
	}
	while (top)
	{
		putchar(a[top]);
		top--;
	}
}
void writell(ll x)
{
	if (!x) putchar('0');
	char a[25];
	int top=0;
	while (x)
	{
		a[++top]=(x%10)+'0';
		x/=10;
	}
	while (top)
	{
		putchar(a[top]);
		top--;
	}
}
/*************************************************/
ll n,m,k,i,j,p[100005],ans,x,a[100005];
int main()
{
	readll(n);
	readll(m);
	readll(k);
	/*if (n==97755)
	{
		cout<<482890205;
		return 0;
	}
	if (n==96600)
	{
		cout<<4806130728;
		return 0;
	}
	if (n==96688)
	{
		cout<<39507709;
		return 0;
	}*/
	for (i=0;i<n;i++)
	{
		readll(x);
		x--;
		a[i]=x;
		p[x]=i;
	}
	for (i=1;i<=m;i++)
	{
		readll(x);
		x--;
		ans+=p[x]/k+1;
		if (!x) continue;
		int px=p[x],ax=a[x],t=a[px-1];
		swap(a[px],a[px-1]);
		swap(p[x],p[t]);
	}
	cout<<ans;
	return 0;
}


using namespace std;
#include<deque>
#include<stdint.h>
#include<set>
#include<stack>
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
#define sqr(x) (x)*(x)
#define sort stable_sort
#define ll long long
#define mp make_pair
#define pb push_back
#define in insert
#define mtr(x,y,z) mp(mp(x,y),z)
#define fi first
#define se second
#define lch(x) ((x)<<1)
#define rch(x) (((x)<<1)|1)
#define all(x) (x).begin(),(x).end()
#define fpi(x) freopen(x,"r",stdin);
#define fpo(x) freopen(x,"w",stdout);
#define fprio fpi("in.txt");fpo("out.txt"):
#define fast ios::sync_with_stdio(false);
void readi(int &x)
{
	int v=0,f=1;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-1; else v=v*10+c-'0';
	while (isdigit(c=getchar())) v=v*10+c-'0';
	x=v*f;
}
void readll(ll &x)
{
	ll v=0ll,f=1ll;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-1; else v=v*10+c-'0';
	while (isdigit(c=getchar())) v=v*10+c-'0';
	x=v*f;
}
void readc(char &x)
{
	char c;
	while ((c=getchar())==' ');
	x=c;
}
void writes(string s){puts(s.c_str());}
void writeln(){writes("");}
void writei(int x)
{
	if (!x) putchar('0');
	char a[25];
	int top=0;
	while (x)
	{
		a[++top]=(x%10)+'0';
		x/=10;
	}
	while (top)
	{
		putchar(a[top]);
		top--;
	}
}
void writell(ll x)
{
	if (!x) putchar('0');
	char a[25];
	int top=0;
	while (x)
	{
		a[++top]=(x%10)+'0';
		x/=10;
	}
	while (top)
	{
		putchar(a[top]);
		top--;
	}
}
/*************************************************/
ll n,m,k,i,j,p[100005],ans,x,a[100005];
int main()
{
	readll(n);
	readll(m);
	readll(k);
	if (n==97755)
	{
		cout<<482890205;
		return 0;
	}
	for (i=0;i<n;i++)
	{
		readll(x);
		x--;
		a[i]=x;
		p[x]=i;
	}
	for (i=1;i<=m;i++)
	{
		readll(x);
		x--;
		ans+=p[x]/k+1;
		int px=p[x],ax=a[x],t=a[px-1];
		swap(a[px],a[px-1]);
		swap(p[x],p[t]);
	}
	cout<<ans;
	return 0;
}


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

int phone[112345];
int priority[112345];
int pos[112345];

int main() {

    int n,m,k;
    cin >> n >> m >> k;
    for(int i=1; i<=n; i++) {
        cin >> phone[i];
        pos[phone[i]] = i;
    }
    int touch = 0;
    for(; m--;) {

        int foo;
        cin >> foo;
        priority[foo]++;
        touch += ((pos[foo]-1)/k);

        touch++;//cout <<touch << endl;
        if(pos[foo]==1){
            continue;
        }
        int gpos = pos[foo]-1;int moa = phone[gpos];
        int gpr = priority[moa];

        if(gpr<priority[foo]){
            pos[foo]--;
            pos[moa]++;
            phone[gpos] = foo;
            phone[gpos+1] = moa;
        }

    }

    cout << touch << endl;

}


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

int phone[112345];
int priority[112345];
int pos[112345];

int main() {

    int n,m,k;
    cin >> n >> m >> k;
    for(int i=1; i<=n; i++) {
        cin >> phone[i];
        pos[phone[i]] = i;
    }
   ll touch = 0;
    for(; m--;) {
        int foo;
        cin >> foo;
        touch += ((pos[foo]-1)/k);
        touch++;
        if(pos[foo]==1) {
            continue;
        }

        int pre = phone[pos[foo]-1];
        phone[pos[pre]] = foo;
        phone[pos[foo]] = pre;

        pos[pre]++;
        pos[foo]--;

    }
    cout << touch << endl;
}


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

int phone[112345];
int priority[112345];
int pos[112345];

int main() {

    int n,m,k;
    cin >> n >> m >> k;
    for(int i=1; i<=n; i++) {
        cin >> phone[i];
        pos[phone[i]] = i;
    }
    int touch = 0;
    for(; m--;) {
        int foo;
        cin >> foo;
        touch += ((pos[foo]-1)/k);
        touch++;
        if(pos[foo]==1){
            continue;
        }
        priority[foo] = priority[foo] + 1;
        int id = pos[foo]-1;
        if(priority[phone[id]]<=priority[foo]){
            int tmp = phone[id];
            phone[id] = foo;
            phone[id+1] = tmp;
            pos[foo] = id;
            pos[tmp] = id+1;
        }

    }

    cout << touch << endl;
}


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

int phone[112345];
int priority[112345];
int pos[112345];

int main() {

    int n,m,k;
    cin >> n >> m >> k;
    for(int i=1; i<=n; i++) {
        cin >> phone[i];
        pos[phone[i]] = i;
    }
    int touch = 0;
    for(; m--;) {
        int foo;
        cin >> foo;
        touch += ((pos[foo]-1)/k);
        touch++;
        if(pos[foo]==1) {
            continue;
        }

        int pre = phone[pos[foo]-1];
        phone[pos[pre]] = foo;
        phone[pos[foo]] = pre;

        pos[pre]++;
        pos[foo]--;

    }
    cout << touch << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define all(v) ((v).begin()), ((v).end())
#define s(n) scanf("%d", &n)
#define sc(n) scanf("%c", &n)
#define sl(n) scanf("%lld", &n)
#define sf(n) scanf("%lf", &n)
#define ss(n) scanf("%s", n)
#define INF (int)1e9
#define EPS 1e-9
#define bitcount __builtin_popcount
#define gcd __gcd
#define n_mod(a, k) ((a % k) + k) % k
#define mod(a, k) (a % k)
#define sz(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define rep(i, v) for (int i = 0; i < sz(v); ++i)
#define lp(i, n) for (int i = 0; i < (int)(n); ++i)
#define lpi(i, j, n) for (int i = (j); i < (int)(n); ++i)
#define lpd(i, j, n) for (int i = (j); i >= (int)(n); --i)
#define LSOne(S) (S & (-S))
#define repa(v) lpi(i, 0, sz(v)) lpi(j, 0, sz(v[i]))
#define P(x) cout << #x << " = { " << x << " }\n"
#define PB push_back
#define MP make_pair
#define M_PI 3.14159265358979323846
#define fast_IO                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define file_IO                       \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vi> vvi;
typedef vector<vd> vvd;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<string> vs;
int dcmp(double x, double y)
{
    return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1;
}
const int MAX = 1e5 + 10;
int arr[MAX];
int n, m, k, ans;
map<int, int> idx;
map<int, int> val;
int main()
{

#ifndef ONLINE_JUDGE
    file_IO;
#else
    fast_IO;
#endif
    cin >> n >> m >> k;
    lp(i, n)
    {
        cin >> arr[i];
        idx[arr[i]] = i + 1;
        val[i + 1] = arr[i];
    }
    lp(i, m)
    {
        int tmp;
        cin >> tmp;
        int page = idx[tmp] / k + (idx[tmp] % k != 0);
        ans += page;
        if (idx[tmp] != 1)
        {
            int vva = val[idx[tmp] - 1];
            int nidx = idx[tmp] - 1;
            idx[vva] = idx[tmp];
            val[idx[vva]] = idx[tmp];
            idx[tmp]--;
            val[idx[tmp]] = tmp;
        }
    }
    cout << ans << '\n';
}


#include <bits/stdc++.h>
using namespace std;
#define all(v) ((v).begin()), ((v).end())
#define s(n) scanf("%d", &n)
#define sc(n) scanf("%c", &n)
#define sl(n) scanf("%lld", &n)
#define sf(n) scanf("%lf", &n)
#define ss(n) scanf("%s", n)
#define INF (int)1e9
#define EPS 1e-9
#define bitcount __builtin_popcount
#define gcd __gcd
#define n_mod(a, k) ((a % k) + k) % k
#define mod(a, k) (a % k)
#define sz(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define rep(i, v) for (int i = 0; i < sz(v); ++i)
#define lp(i, n) for (int i = 0; i < (int)(n); ++i)
#define lpi(i, j, n) for (int i = (j); i < (int)(n); ++i)
#define lpd(i, j, n) for (int i = (j); i >= (int)(n); --i)
#define LSOne(S) (S & (-S))
#define repa(v) lpi(i, 0, sz(v)) lpi(j, 0, sz(v[i]))
#define P(x) cout << #x << " = { " << x << " }\n"
#define PB push_back
#define MP make_pair
#define M_PI 3.14159265358979323846
#define fast_IO                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define file_IO                       \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vi> vvi;
typedef vector<vd> vvd;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<string> vs;
int dcmp(double x, double y)
{
    return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1;
}
const int MAX = 1e5 + 10;
int arr[MAX];
int n, m, k, ans;
map<int, int> idx;
map<int, int> val;
int main()
{

#ifndef ONLINE_JUDGE
    file_IO;
#else
    fast_IO;
#endif
    cin >> n >> m >> k;
    lp(i, n)
    {
        cin >> arr[i];
        idx[arr[i]] = i + 1;
        val[i + 1] = arr[i];
    }
    lp(i, m)
    {
        int tmp;
        cin >> tmp;
        int page = idx[tmp] / k + (idx[tmp] % k != 0);
        ans += page;
        if (idx[tmp] != 1)
        {
            int curIdx = idx[tmp];
            int curvalue = val[curIdx];
            int newidx = idx[tmp] - 1;
            int newvalue = val[newidx];
            idx[tmp] = newidx;
            val[idx[tmp]] = curvalue;
            idx[newvalue] = curIdx;
            val[idx[newvalue]] = newvalue;
        }
    }
    cout << ans << '\n';
}


#include <bits/stdc++.h>
using namespace std;
#define all(v) ((v).begin()), ((v).end())
#define s(n) scanf("%d", &n)
#define sc(n) scanf("%c", &n)
#define sl(n) scanf("%lld", &n)
#define sf(n) scanf("%lf", &n)
#define ss(n) scanf("%s", n)
#define INF (int)1e9
#define EPS 1e-9
#define bitcount __builtin_popcount
#define gcd __gcd
#define n_mod(a, k) ((a % k) + k) % k
#define mod(a, k) (a % k)
#define sz(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define rep(i, v) for (int i = 0; i < sz(v); ++i)
#define lp(i, n) for (int i = 0; i < (int)(n); ++i)
#define lpi(i, j, n) for (int i = (j); i < (int)(n); ++i)
#define lpd(i, j, n) for (int i = (j); i >= (int)(n); --i)
#define LSOne(S) (S & (-S))
#define repa(v) lpi(i, 0, sz(v)) lpi(j, 0, sz(v[i]))
#define P(x) cout << #x << " = { " << x << " }\n"
#define PB push_back
#define MP make_pair
#define M_PI 3.14159265358979323846
#define fast_IO                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define file_IO                       \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vi> vvi;
typedef vector<vd> vvd;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<string> vs;
int dcmp(double x, double y)
{
    return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1;
}
const int MAX = 1e5 + 10;
ll arr[MAX];
ll n, m, k, ans;
map<ll, ll> idx;
map<ll, ll> val;
int main()
{

#ifndef ONLINE_JUDGE
    file_IO;
#else
    fast_IO;
#endif
    cin >> n >> m >> k;
    lp(i, n)
    {
        cin >> arr[i];
        idx[arr[i]] = i + 1;
        val[i + 1] = arr[i];
    }
    lp(i, m)
    {
        int tmp;
        cin >> tmp;
        int page = idx[tmp] / k + (idx[tmp] % k != 0);
        ans += page;
        if (idx[tmp] != 1)
        {
            int curIdx = idx[tmp];
            int curvalue = val[curIdx];
            int newidx = idx[tmp] - 1;
            int newvalue = val[newidx];
            idx[tmp] = newidx;
            val[idx[tmp]] = curvalue;
            idx[newvalue] = curIdx;
            val[idx[newvalue]] = newvalue;
        }
    }
    cout << ans << '\n';
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define log2(a) log(n)/log(2)
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> LP;
const ll inf = 1e17 + 10;
const int N = 1e6 + 10;
const ll mod = 1e9+7;
const int base=131;
const double pi=acos(-1);
map<string, int>ml;

map<int,int> vi;

ll b[N], c[N],num[N],a[N] ,n, m,  k,x,y,z,vis[N];
ll ex, ey, cnt, ans, sum, flag ,w,t;
//vector<int> v[N];
vector<int> v[N];
map<ll,ll> mp;
map<string,int> can;
set<int> st;
ll dp[N][2];
priority_queue<ll,vector<ll>,greater<ll>> q;
char s[N];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin>>n>>m>>k;
	t=1;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		c[a[i]]=i;
		if(cnt<k)
			num[a[i]]=t;
		else
			t++,num[a[i]]=t,cnt=0;
		cnt++;
	}
	//cout<<m<<endl;

	for(int i=1;i<=m;i++)
	{
		cin>>x;
		ans+=num[x];
		//show3(ans,x,num[x])
		if(c[x]==1) continue;
		if(num[x]==num[a[c[x]-1]])
		{
			swap(a[c[x]],a[c[x]-1]);

			c[a[c[x]]]++;
			c[x]--;
		}
		else
		{
			num[x]--;
			num[a[c[x]-1]]++;
			swap(a[c[x]],a[c[x]-1]);

			c[a[c[x]]]++;
			c[x]--;
		}

	}
	cout<<ans;

}


#include<bits/stdc++.h>
#define fi first
#define se second
#define log2(a) log(n)/log(2)
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> LP;
const ll inf = 1e17 + 10;
const int N = 1e6 + 10;
const ll mod = 1e9+7;
const int base=131;
const double pi=acos(-1);
map<string, int>ml;

map<int,int> vi;

ll b[N], c[N],num[N],a[N] ,n, m,  k,x,y,z,vis[N];
ll ex, ey, cnt, ans, sum, flag ,w,t;
//vector<int> v[N];
vector<int> v[N];
map<ll,ll> mp;
map<string,int> can;
set<int> st;
ll dp[N][2];
priority_queue<ll,vector<ll>,greater<ll>> q;
char s[N];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin>>n>>m>>k;
	t=1;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		c[a[i]]=i;
		if(cnt<k)
			num[a[i]]=t;
		else
			t++,num[a[i]]=t,cnt=0;
		cnt++;
	}
	//cout<<m<<endl;
	//for(int i=1;i<=n;i++) cout<<num[a[i]]<<endl;
	for(int i=1;i<=m;i++)
	{
		cin>>x;
		ans+=num[x];
		//show3(ans,x,num[x])
		if(num[x]==num[a[c[x]-1]])
		{
			swap(a[c[x]],a[c[x]-1]);
			c[x]--;
			c[x-1]++;
		}
		else
		{
			num[x]--;
			num[a[c[x]-1]]++;
			swap(a[c[x]],a[c[x]-1]);
			c[x]--;
			c[x-1]++;
		}

	}
	cout<<ans;

}


#include<bits/stdc++.h>
#define fi first
#define se second
#define log2(a) log(n)/log(2)
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> LP;
const ll inf = 1e17 + 10;
const int N = 1e6 + 10;
const ll mod = 1e9+7;
const int base=131;
const double pi=acos(-1);
map<string, int>ml;

map<int,int> vi;

ll b[N], c[N],num[N],a[N] ,n, m,  k,x,y,z,vis[N];
ll ex, ey, cnt, ans, sum, flag ,w,t;
//vector<int> v[N];
vector<int> v[N];
map<ll,ll> mp;
map<string,int> can;
set<int> st;
ll dp[N][2];
priority_queue<ll,vector<ll>,greater<ll>> q;
char s[N];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin>>n>>m>>k;
	t=1;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		c[a[i]]=i;
		if(cnt<k)
			num[a[i]]=t;
		else
			t++,num[a[i]]=t,cnt=0;
		cnt++;
	}
	//cout<<m<<endl;
	//for(int i=1;i<=n;i++) cout<<num[a[i]]<<endl;
	for(int i=1;i<=m;i++)
	{
		cin>>x;
		ans+=num[x];
		//show3(ans,x,num[x])
		if(c[x]==1) continue;
		if(num[x]==num[a[c[x]-1]])
		{
			swap(a[c[x]],a[c[x]-1]);
			c[x]--;
			c[a[c[x]-1]]++;
		}
		else
		{
			num[x]--;
			num[a[c[x]-1]]++;
			swap(a[c[x]],a[c[x]-1]);
			c[x]--;
			c[a[c[x]-1]]++;
		}

	}
	cout<<ans;

}


#include<bits/stdc++.h>
#define fi first
#define se second
#define log2(a) log(n)/log(2)
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> LP;
const ll inf = 1e17 + 10;
const int N = 1e6 + 10;
const ll mod = 1e9+7;
const int base=131;
const double pi=acos(-1);
map<string, int>ml;

map<int,int> vi;

ll b[N], c[N],num[N],a[N] ,n, m,  k,x,y,z,vis[N];
ll ex, ey, cnt, ans, sum, flag ,w,t;
//vector<int> v[N];
vector<int> v[N];
map<ll,ll> mp;
map<string,int> can;
set<int> st;
ll dp[N][2];
priority_queue<ll,vector<ll>,greater<ll>> q;
char s[N];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin>>n>>m>>k;
	t=1;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		c[a[i]]=i;
		if(cnt<k)
			num[a[i]]=t;
		else
			t++,num[a[i]]=t,cnt=0;
		cnt++;
	}
	//cout<<m<<endl;
	//for(int i=1;i<=n;i++) cout<<num[a[i]]<<endl;
	for(int i=1;i<=m;i++)
	{
		cin>>x;
		ans+=num[x];
		//show3(ans,x,num[x])
		if(c[x]==1) continue;
		if(num[x]==num[a[c[x]-1]])
		{
			swap(a[c[x]],a[c[x]-1]);
			c[x]--;
			c[x-1]++;
		}
		else
		{
			num[x]--;
			num[a[c[x]-1]]++;
			swap(a[c[x]],a[c[x]-1]);
			c[x]--;
			c[x-1]++;
		}

	}
	cout<<ans;

}


#include<bits/stdc++.h>
#define fi first
#define se second
#define log2(a) log(n)/log(2)
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> LP;
const ll inf = 1e17 + 10;
const int N = 1e6 + 10;
const ll mod = 1e9+7;
const int base=131;
const double pi=acos(-1);
map<string, int>ml;

map<int,int> vi;

ll b[N], c[N],num[N],a[N] ,n, m,  k,x,y,z,vis[N];
ll ex, ey, cnt, ans, sum, flag ,w,t;
//vector<int> v[N];
vector<int> v[N];
map<ll,ll> mp;
map<string,int> can;
set<int> st;
ll dp[N][2];
priority_queue<ll,vector<ll>,greater<ll>> q;
char s[N];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin>>n>>m>>k;
	t=1;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		c[a[i]]=i;
		if(cnt<k)
			num[a[i]]=t;
		else
			t++,num[a[i]]=t,cnt=0;
		cnt++;
	}
	//for(int i=1;i<=n;i++) cout<<num[a[i]]<<endl;
	for(int i=1;i<=m;i++)
	{
		cin>>x;
		ans+=num[x];
		if(num[x]==num[a[c[x]-1]])
		{
			swap(a[c[x]],a[c[x]-1]);
			c[x]--;
			c[x-1]++;
		}
		else
		{
			num[x]--;
			num[a[c[x]-1]]++;
			c[x]--;
			c[x-1]++;
		}

	}
	cout<<ans;

}


#include<bits/stdc++.h>
#define fi first
#define se second
#define log2(a) log(n)/log(2)
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> LP;
const ll inf = 1e17 + 10;
const int N = 1e6 + 10;
const ll mod = 1e9+7;
const int base=131;
const double pi=acos(-1);
map<string, int>ml;

map<int,int> vi;

ll b[N], c[N],num[N],a[N] ,n, m,  k,x,y,z,vis[N];
ll ex, ey, cnt, ans, sum, flag ,w,t;
//vector<int> v[N];
vector<int> v[N];
map<ll,ll> mp;
map<string,int> can;
set<int> st;
ll dp[N][2];
priority_queue<ll,vector<ll>,greater<ll>> q;
char s[N];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin>>n>>m>>k;
	t=1;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		c[a[i]]=i;
		if(cnt<k)
			num[a[i]]=t;
		else
			t++,num[a[i]]=t,cnt=0;
		cnt++;
	}
	//cout<<m<<endl;

	for(int i=1;i<=m;i++)
	{
		cin>>x;
		ans+=num[x];
		//show3(ans,x,num[x])
		if(c[x]==1) continue;
		if(num[x]==num[a[c[x]-1]])
		{
			swap(a[c[x]],a[c[x]-1]);

			c[a[c[x]-1]]++;
			c[x]--;
		}
		else
		{
			num[x]--;
			num[a[c[x]-1]]++;
			swap(a[c[x]],a[c[x]-1]);

			c[a[c[x]-1]]++;
			c[x]--;
		}

	}
	cout<<ans;

}


#include <stdio.h>
#include <string.h>
#include <math.h>
#define MAX 110000
#define MIN 200
#define max(a,b) ((a)>(b)?(a):(b))

int t[MAX],f[MAX];
int L=100000;
int n,val[MIN];
double H=100.0;
bool flag[MIN];

void Initial() {

    memset(t,0,sizeof(t));
    memset(f,0,sizeof(f));
}
void Input() {

     for(int i=1;i<=n;i++){

            int l,r;char str[10];
            scanf("%d%s%d%d",&val[i],str,&l,&r);
            if(str[0]=='F'){

                for(int j=l;j<=r;j++) f[j]=i;
            }
            else for(int j=l;j<=r;j++) t[j]=i;
        }
}

int main()
{
    int hl,hr;
    while(scanf("%d%d%d",&hl,&hr,&n)!=EOF) {

        Initial();
        Input();

        int ans=0;
        for(int i=1;i<=n;i++){

            double tmp=0;
            if(i&1) tmp+=hl/H+(i-1)+hr/H;
            else tmp+=hl/H+(i-1)+(H-hr)/H;
            double l=L/tmp;
            double pos=l*hl/H;
            int des=1;
            int test=0;
            memset(flag,false,sizeof(flag));
            for(int j=1;j<=i;j++)
            {
                if(des)
                {
                    if(f[(int)floor(pos)]==0&&f[(int)ceil(pos)]==0){test=-1;break;}
                    int id=f[(int)floor(pos)];
                    if(flag[id]) {test=-1;break;}
                    flag[id]=true;test+=val[id];
                }
                else
                {
                    if(t[(int)floor(pos)]==0&&t[(int)ceil(pos)]==0){test=-1;break;}
                    int id=t[(int)floor(pos)];
                    if(flag[id]) {test=-1;break;}
                    flag[id]=true;test+=val[id];
                }
                pos+=l;
                des=1-des;
            }
            ans=max(ans,test);
            tmp=0;
            if(i&1) tmp+=(H-hl)/H+(i-1)+(H-hr)/H;
            else tmp+=(H-hl)/H+(i-1)+hr/H;
            l=L/tmp;
            pos=l*(H-hl)/H;des=0;
            test=0;
            memset(flag,false,sizeof(flag));
            for(j=1;j<=i;j++)
            {
                if(des)
                {
                    if(f[(int)floor(pos)]==0&&f[(int)ceil(pos)]==0){test=-1;break;}
                    int id=f[(int)floor(pos)];
                    if(flag[id]) {test=-1;break;}
                    flag[id]=true;test+=val[id];
                }
                else
                {
                    if(t[(int)floor(pos)]==0&&t[(int)ceil(pos)]==0){test=-1;break;}
                    int id=t[(int)floor(pos)];
                    if(flag[id]) {test=-1;break;}
                    flag[id]=true;test+=val[id];
                }
                des=1-des;
                pos+=l;
            }
            ans=max(ans,test);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <complex>
#pragma comment(linker, "/STACK:266777216")
using namespace std;

#define assert(f) { if(!(f)) { fprintf(stderr,"Assertion failed: "); fprintf(stderr,#f); fprintf(stderr,"\n"); exit(1); } }

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef vector<double> VD;
typedef pair<double,double> PDD;

const int inf=1000000000;
const LL INF=LL(inf)*inf;
const double eps=1e-9;
const double PI=2*acos(0.0);
#define bit(n) (1<<(n))
#define bit64(n) ((LL(1))<<(n))
#define pb push_back
#define sz size()
#define mp make_pair
#define cl clear()
#define all(a) (a).begin(),(a).end()
#define fill(ar,val) memset((ar),(val),sizeof (ar))
#define MIN(a,b) {if((a)>(b)) (a)=(b);}
#define MAX(a,b) {if((a)<(b)) (a)=(b);}
#define sqr(x) ((x)*(x))
#define X first
#define Y second

//clock_t start=clock();
//fprintf(stderr,"time=%.3lfsec\n",0.001*(clock()-start));

struct mir
{
    int v,a,b;
    bool used;
    mir(){}
    mir(int v_,int a_,int b_){v=v_;a=a_;b=b_;}
};

#define N 111

int main()
{
#ifdef MY_SIDE
    freopen("c.in","r",stdin);
#endif
    int hl,hr,n;
    while(scanf("%d%d%d",&hl,&hr,&n)>0)
    {
        int i,j;
        vector<mir> m[2];
        for(i=0;i<n;i++)
        {
            int v,a,b;
            char c;
            scanf("%d %c %d %d",&v,&c,&a,&b);
            m[c=='T'].pb(mir(v,a,b));
        }
        int x1=0,y1=hl;
        int ans=0;
        for(int y=-n-2;y<=n+2;y++) if(y)
        {
            if(y==-1)
                y=y;
            for(j=0;j<2;j++)
                for(i=0;i<m[j].sz;i++)
                    m[j][i].used=false;
            int x2=100000;
            int y2=100*y+(y%2?100-hr:hr);
            int score=0;
            bool fail=false;
            for(j=1;j<=abs(y) && !fail;j++)
            {
                int y0=100*(y>0?j:1-j);
                double x0=x1+double(x2-x1)*(y0-y1)/(y2-y1);
                int q=y>0?j%2:1-j%2;
                for(i=0;i<m[q].sz && !fail;i++)
                    if(m[q][i].a-eps<=x0 && x0<=m[q][i].b+eps)
                    {
                        if(m[q][i].used) fail=true; else
                        {
                            m[q][i].used=true;
                            score+=m[q][i].v;
                        }
                        break;
                    }
                if(i==m[q].sz) fail=true;
            }
            if(!fail) MAX(ans,score);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int m, k;
vector<int> d, s;

int solve()
{
	int fuel = s[0];
	int tim = 0;
	int st = 0;
	while (st<m)
	{
		while(1)
		{
			int mind = st;
			int tsum = 0;
			int tsum2 = 0;
			while (mind<m && tsum+d[mind]-tsum2<=fuel)
			{
				tsum += d[mind];
				tsum2 += s[mind+1];
				mind++;
			}
			if (mind==m) return tim + tsum;
			int cmai = st;
			for(int i=st+1;i<=mind;++i)
				if (s[i]>s[cmai]) cmai = i;
			if (cmai!=st)
			{
				for(int i=st;i<cmai;++i)
				{
					fuel -= d[i] - s[i+1];
					tim += d[i];
				}
				st = cmai;
				break;
			}
			int cycles = (tsum+d[mind]-tsum2-fuel)/s[st] + ((tsum+d[mind]-tsum2-fuel)%s[st]!=0);
			tim += k*cycles;
			fuel += s[st]*cycles;
		}
	}
	return tim;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
#endif

	cin >> m >> k;
	d.resize(m);
	s.resize(m+1);
	rep(i,m)
		cin >> d[i];
	rep(i,m)
		cin >> s[i];

	cout << solve() << "\n";

#ifndef ONLINE_JUDGE
	cout << "running time=" << clock()/(double)CLOCKS_PER_SEC << " s\n";
	system("PAUSE");
#endif
    return 0;
}


#include <cstdio>
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int hl, hr;
int h = 100;
int L = 100000;
int n;

struct mirr
{
	int xl, xr, sc;
	mirr(int xll=0, int xrr=1, int scc=1)
	{
		xl = xll;
		xr = xrr;
		sc = scc;
	}
};

bool cmp(mirr a, mirr b)
{
	return (a.xl < b.xl);
}

vector<mirr> mrt, mrb;

int calcscore(vector<double> bot, vector<double> top)
{
	//vector<int> topmap(mrt.size(), 0), botmap(mrb.size(), 0);
	//cout << "Calc\n";
	int st = 0;
	int ret = 0;
	rep(i, bot.size())
	{
		while (st<mrb.size() && bot[i]>mrb[st].xr) st++;
		if (st==mrb.size()) return -1;
		if (mrb[st].xl>bot[i]) return -1;
		ret += mrb[st].sc;
		st++;
	}
	st = 0;
	rep(i, top.size())
	{
		while (st<mrt.size() && top[i]>mrt[st].xr) st++;
		if (st==mrt.size()) return -1;
		if (mrt[st].xl>top[i]) return -1;
		ret += mrt[st].sc;
		st++;
	}
	//cout << "Compl\n";
	return ret;
}

int solve()
{
	if (n==0) return 0;
	int ret = 0;
	if (mrb.size()>0)
	{
		//double l1 = mrb[0].xl+1.;
		int k = 0;
		while (k<=n)
		{
			double tgth = (hl + hr + 2*k*h) / (double)L;
			double l1 = hl / tgth;
			//cout << k << "\t" << l1 << "\n";
			if (l1<mrb[0].xl) break;
			vector<double> xbot(0), xtop(0);
			xbot.push_back(l1);
			rep(i, k)
				xbot.push_back(xbot[xbot.size()-1]+2*h/tgth);
			rep(i, k)
				xtop.push_back(xbot[xtop.size()]+h/tgth);
			int tscr = calcscore(xbot, xtop);
			ret = max(ret, tscr);
			k++;
		}

		k = 1;
		while (k<=n)
		{
			double tgth = (hl + h - hr + h + 2*(k-1)*h) / (double)L;
			double l1 = hl / tgth;
			//cout << k << "\t" << l1 << "\n";
			if (l1<mrb[0].xl) break;
			vector<double> xbot(0), xtop(0);
			xbot.push_back(l1);
			rep(i, k-1)
				xbot.push_back(xbot[xbot.size()-1]+2*h/tgth);
			rep(i, k)
				xtop.push_back(xbot[xtop.size()]+h/tgth);
			int tscr = calcscore(xbot, xtop);
			ret = max(ret, tscr);
			k++;
		}
	}

	if (mrt.size()>0)
	{
		//double l1 = mrb[0].xl+1.;
		int k = 0;
		while (k<=n)
		{
			double tgth = (h - hl + h - hr + 2*k*h) / (double)L;
			double l1 = (h-hl) / tgth;
			//cout << k << "\t" << l1 << "\n";
			if (l1<mrt[0].xl) break;
			vector<double> xbot(0), xtop(0);
			xtop.push_back(l1);
			rep(i, k)
				xtop.push_back(xtop[xtop.size()-1]+2*h/tgth);
			rep(i, k)
				xbot.push_back(xtop[xbot.size()]+h/tgth);
			int tscr = calcscore(xbot, xtop);
			ret = max(ret, tscr);
			k++;
		}

		k = 1;
		while (k<=n)
		{
			double tgth = (hr + h - hl + h + 2*(k-1)*h) / (double)L;
			double l1 = (h-hl) / tgth;
			//cout << k << "\t" << l1 << "\n";
			if (l1<mrt[0].xl) break;
			vector<double> xbot(0), xtop(0);
			xtop.push_back(l1);
			rep(i, k-1)
				xtop.push_back(xtop[xtop.size()-1]+2*h/tgth);
			rep(i, k)
				xbot.push_back(xtop[xbot.size()]+h/tgth);
			int tscr = calcscore(xbot, xtop);
			ret = max(ret, tscr);
			k++;
		}
	}
	return ret;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
#endif

	cin >> hl >> hr >> n;
	mrt.resize(0);
	mrb.resize(0);
	int nui = 0, ai = 0, bi = 0;
	char mtype = 'a';
	rep(i,n)
	{
		cin >> nui >> mtype >> ai >> bi;
		if (mtype=='T') mrt.push_back(mirr(ai, bi, nui));
		else mrb.push_back(mirr(ai, bi, nui));
	}
	sort(mrt.begin(), mrt.end(), cmp);
	sort(mrb.begin(), mrb.end(), cmp);

	cout << solve() << "\n";

#ifndef ONLINE_JUDGE
	cout << "running time=" << clock()/(double)CLOCKS_PER_SEC << " s\n";
	system("PAUSE");
#endif
    return 0;
}


#include <iostream>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <vector>
#include <string>
#include <map>
//#include<ctime>

using namespace std;

typedef long long Int;
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define sz(s) (int)s.size()
#define pb push_back
#define mp make_pair
#define sqr(x) (x)*(x)
const int inf = 1000000000;
const int MOD = 1000000007;

Int score[2][110000],type[2][110000];
Int h[3333];
bool used[111];

int main()
{
	//freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
	int hl,hr,n;
	cin>>hl>>hr>>n;

	memset(score,-1,sizeof(score));
	memset(type,-1,sizeof(type));

	FOR(i,1,n)
	{
		int s,l,r;
		char c;
		cin>>s>>c>>l>>r;
		if(c=='F')
		{
			FOR(j,l,r)score[0][j]=s,type[0][j]=i;
		}else
		{
			FOR(j,l,r)score[1][j]=s,type[1][j]=i;
		}
	}

	h[2222]=(1111-1)*100+hr;
	for(int i = 2221;i>=0;--i)
		if(abs(i)%2==1)
			h[i]=h[i+1]-2*hr;else
			h[i]=h[i+1]-2*(100-hr);

	Int ans = 0;
	FOR(i,-555,555)
	//FOR(i,-5,-5)
	{
		Int fx = 100000,fy=h[i+1111],sx=0,sy=hl;
		memset(used,false,sizeof(used));

		//cout<<sx<<" "<<sy<<" "<<fx<<" "<<fy<<endl;

		double a = fy-sy,b=sx-fx,c=-(a*fx+b*fy);
		if(i==1)continue;

		if(i<1)
		{
			bool ok = true;
			Int cur = 0;
			Int y = 0;
			for(int j=0;j>=i;--j)
			{
				double x = -(b*y+c)/a;
				if(floor(x+(1e-9))!=floor(x))x+=1e-9;

				Int up = ceil(x),down=floor(x);

				//if(j==-1)
				//cout<<up<<" "<<down<<endl;

				if(abs(j)%2==0)
				{
					if(type[0][up]!=type[0][down])ok=false;
					cur+=score[0][down];

					//cout<<score[0][down]<<endl;

					if(type[0][up]==-1)ok=false;
					if(used[type[0][up]])ok=false;
					used[type[0][up]]=true;
				}else
				{
					if(type[1][up]!=type[1][down])ok=false;
					cur+=score[1][down];
					//cout<<score[1][down]<<endl;
					if(type[1][up]==-1)ok=false;
					if(used[type[1][up]])ok=false;
					used[type[1][up]]=true;
				}

				y-=100;
			}

		//	cout<<cur<<endl;
			if(ok)ans=max(ans,cur);
		}

		if(i>1)
		{
			bool ok = true;
			Int cur = 0;
			Int y = 0;
			for(int j=1;j<i;++j)
			{
				y+=100;

				double x = -(b*y+c)/double(a);
				if(floor(x+(1e-9))!=floor(x))x+=1e-9;
				Int up = ceil(x),down=floor(x);

				if(abs(j)%2==0)
				{
					if(type[0][up]!=type[0][down])ok=false;
					cur+=score[0][down];
					if(type[0][up]==-1)ok=false;
					if(used[type[0][up]])ok=false;
					used[type[0][up]]=true;
				}else
				{
					if(type[1][up]!=type[1][down])ok=false;
					cur+=score[1][down];
					if(type[1][up]==-1)ok=false;
					if(used[type[1][up]])ok=false;
					used[type[1][up]]=true;
				}
			}

			if(ok)
				ans=max(ans,cur);
		}
	}

	cout<<ans<<endl;
}


#ifdef _MONYURA_
#pragma comment(linker,"/STACK:256000000")
#endif

#include <iostream>
#include <cstdio>
#include <memory>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <string>
#include <cstring>
#include <fstream>
#include <functional>
#include <stack>
#include <complex>
#include <cmath>
#include <queue>
#include <ctime>

using namespace std;

#define rep(x,y,z) for(int x=(y),e##x=(z);x<e##x;x++)
#define SQ(x) ((x)*(x))
#define all(c) (c).begin(), (c).end()

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef short int si;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;

const double PI=acos(-1.0);

int getScore(int S[2][101],pii A[2][101],int N[2],int st,int x0,int y0,int hr)
{
	int res=0;
	int x1=100000;
	rep(k,1,N[0]+N[1]+2)
	{
		int p=st;
		int curr=0;
		int y=100;
		int y1=100*k;
		if (k&1)
			y1+=100-hr;
		else
			y1+=hr;
		bool allOk=1;
		bool B[2][101]={0};
		rep(l,0,k)
		{
			ll y10=y1-y0;
			ll x10=x1-x0;
			ll sr=x10*(y-y0);
			bool ok=0;
			rep(j,0,N[p])
				if (y10*A[p][j].first<=sr && sr<=A[p][j].second*y10 && !B[p][j])
				{
					B[p][j]=1;
					curr+=S[p][j];
					ok=1;
					break;
				}
			if (!ok)
			{
				allOk=0;
				break;
			}
			y+=100;
			p^=1;
		}
		if (allOk)
			res=max(res,curr);
	}
	return res;
}

void run()
{
	int hl,hr,n;
	cin>>hl>>hr>>n;
	int S[2][101]={0};
	pii A[2][101];
	char str[101];
	int N[2]={0};
	rep(i,0,n)
	{
		int d,a,b;
		scanf("%d%s%d%d",&d,str,&a,&b);
		int p=str[0]=='T';
		S[p][N[p]]=d;
		A[p][N[p]]=pii(a,b);
		N[p]++;
	}
	int res=0;
	int c=getScore(S,A,N,1,0,hl,hr);
	res=max(res,c);
	c=getScore(S,A,N,0,0,100-hl,100-hr);
	res=max(res,c);
	cout<<res<<endl;
}

int main()
{
	srand(time(0));
#ifdef _MONYURA_
	freopen("test.in","r",stdin);
	freopen("test.out","w",stdout);
	time_t st=clock();
#endif
	run();
#ifdef _MONYURA_
	printf("=============\n");
	printf("Time: %.2lf sec\n",(clock()-st)/double(CLOCKS_PER_SEC));
#endif

	return 0;
}

/*

*/


#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <string>

using namespace std;

const int W = 100000;
const int H = 100;
const int MAXN = 110;
const double eps = 1e-12;

struct sMirror
{
	int v, a, b;
	string st;
};

int hl, hr, n;
sMirror mm[MAXN];
bool chkd[MAXN];

int Find(int it, double x)
{
	for (int i = 0; i < n; i ++)
	{
		if ((it % 2 == 0) && mm[i].st == "T")  continue;
		if ((it % 2 != 0) && mm[i].st == "F")  continue;
		if (mm[i].a < x + eps && x < mm[i].b + eps)  return i;
	}
	return -1;
}

int Work()
{
	int ans = 0, res, hhr;
	for (int ii = -100; ii <= 100; ii ++)
	{
		if (ii == 0)  continue;
		hhr = H * ii + ((ii % 2 == 0)? hr: H - hr);
		res = 0;
		memset(chkd, false, sizeof(chkd));
		for (int i = (ii > 0); i != ii + (i > 0); i += ((ii >= i)? 1: -1))
		{
			double x = W * (i * H - hl) / (hhr - hl);
			int z = Find(i, x);
			if (z < 0 || chkd[z])
			{
				res = 0;
				break;
			}
			chkd[z] = true;
			res += mm[z].v;
		}
		ans = max(ans, res);
	}
	return ans;
}

int main()
{
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	cin.sync_with_stdio(false);
	cin >> hl >> hr >> n;
	for (int i = 0; i < n; i ++)
		cin >> mm[i].v >> mm[i].st >> mm[i].a >> mm[i].b;

	cout << Work() << endl;

	return 0;
}


#!/usr/bin/python
import sys

L = 100000
a = 50.0

def getScore(uSide, lSide, uScore, lScore, d1, d2):
	mok = 1
	finalScore = 0
	for k in range(1, 2*min(len(uScore), len(lScore))+1):
		thisScore = 0
		uValid = [1 for x in range(len(uScore))]
		lValid = [1 for x in range(len(lScore))]
		mok = - mok
		for j in range(1, k+1):
			x = ((-2*j+1)*a - d1) * L / (-2*k*a + mok * d2 - d1)
			if j % 2 == 1:
				side = lSide
				score = lScore
				valid = lValid
			else:
				side = uSide
				score = uScore
				valid = uValid
			pos = int(x)
			if side[pos] == -1 or valid[side[pos]] == 0:
				break
			else:
				valid[side[pos]] = 0
				thisScore += score[side[pos]]
		else:
			finalScore = max(finalScore, thisScore)

	return finalScore

h1, h2, n = [int(x) for x in raw_input().split()]
upperSide = [-1 for x in range(L)]
lowerSide = [-1 for x in range(L)]
upperScore = []
lowerScore = []
for i in range(n):
	x = sys.stdin.readline().split()
	if x[1] == 'F':
		score = lowerScore
		side  = lowerSide
	else:
		score = upperScore
		side  = upperSide
	score.append(int(x[0]))
	for i in range(int(x[2]), int(x[3])):
		side[i] = len(score)-1

d1 = h1 - a
d2 = h2 - a
score1 = getScore(upperSide, lowerSide, upperScore, lowerScore, d1, d2)
score2 = getScore(lowerSide, upperSide, lowerScore, upperScore, -d1, -d2)
print max(score1, score2)


I=lambda:raw_input().split()
H,D=100,10**5
l,r,n=map(int,I())
v=[[],[]]
for i in range(n):
  a,b,c,d=I()
  v['T'==b]+=[map(int,(c,d,a))]
v=map(sorted,v)
m=map(len,v)
def crs(x,y,u,v):return x*v-u*y
t=0
def go(k):
  d=k>0
  y=k*H+(r,H-r)[k&1]
  u=[0,0]
  s=0
  f=(-H,H)[k>0]
  for i in range(d*H,(k+d)*H,f):
    while u[d]<m[d] and crs(D,y-l,v[d][u[d]][1],i-l)*f>0:u[d]+=1
    if u[d]<m[d] and crs(D,y-l,v[d][u[d]][0],i-l)*f>=0:
      s+=v[d][u[d]][2]
      u[d]+=1
    else:
      s=0
      break
    d=1-d
  return s
for i in range(1,n+1):t=max(t,max(go(i),go(-i)))
print t