// abhishek soni.....[NSIT'22]

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
using namespace std;

#define letsgo              ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl                '\n'
#define ff                  first
#define ss                  second
#define int                 long long
#define ll                  long long
#define mp                  make_pair
#define pb                  push_back
#define pf                  push_front
#define lb                  lower_bound
#define ub                  upper_bound
#define ALL(x)              (x).begin() , (x).end()
#define set_bits(x)         __builtin_popcountll(x)
#define unset_bits(x)       __builtin_ctzll(x)
#define fl(i,a,b)           for(int i = a ; i < b ; i++)
#define bl(i,a,b)           for(int i = a ; i >= b ; i--)

typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> pbds;

typedef pair<int,int>                                   pii;
typedef pair<int,pair<int,int>>                         piii;
typedef pair<pair<int,int>,pair<int,int>>               piiii;
typedef priority_queue<int>                             maxpq;
typedef priority_queue<int,vector<int>,greater<int>>    minpq;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

int pow(int a,int b,int m){
    if(a == 0) return 0;
    if(b == 0) return 1;
    int temp = pow(a , b/2 , m);
    temp = ((temp % m) * (temp % m)) % m;
    if(b & 1) temp = ((temp % m) * (a % m)) % m;
    return temp;
}

inline int add(int a , int b , int m){ return ((a % m) + (b % m)) % m ; }
inline int sub(int a , int b , int m){ return ((a % m) - (b % m) + m) % m ; }
inline int mul(int a , int b , int m){ return ((a % m) * (b % m)) % m ; }
inline int div(int a , int b , int m){ return ((a % m) * (pow(b , m - 2 , m) % m)) % m ; }

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
const int MOD = 998244353;
const int mod = 1e9 + 7;
const int N = 1 << 22;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
int dp[N] , arr[(int)1e6];

void solve(){
    int n; cin >> n;
    memset(dp,-1,sizeof(dp));
    for(int  i = 0 ; i < n ; i++){
        cin >> arr[i];
        dp[arr[i]] = arr[i];
        for(int j = 0; j < 22 ; j++){
            arr[i] ^= (1<<j);
        }
    }
    for(int i = 0 ; i < 22 ; i++){
        for(int mask = 0 ; mask < N ; mask++){
            if(mask&(1<<i))
                dp[mask] = max(dp[mask] , dp[mask^(1<<i)]);
        }
    }
    for(int i = 0 ;i  < n  ; i++){
        cout << dp[arr[i]] << " ";
    }
    cout << endl;
}

int32_t main(){
    letsgo
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    int t = 1;
    // cin>>t;
    for(int T = 1 ; T <= t ; T++){
        // cout << "Case #" << T << ": " << endl;
        // clock_t z = clock();
        solve();
        // cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);
    }
}


// abhishek soni.....[NSIT'22]

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
using namespace std;

#define letsgo              ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl                '\n'
#define ff                  first
#define ss                  second
#define int                 long long
#define ll                  long long
#define mp                  make_pair
#define pb                  push_back
#define pf                  push_front
#define lb                  lower_bound
#define ub                  upper_bound
#define ALL(x)              (x).begin() , (x).end()
#define set_bits(x)         __builtin_popcountll(x)
#define unset_bits(x)       __builtin_ctzll(x)
#define fl(i,a,b)           for(int i = a ; i < b ; i++)
#define bl(i,a,b)           for(int i = a ; i >= b ; i--)

typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> pbds;

typedef pair<int,int>                                   pii;
typedef pair<int,pair<int,int>>                         piii;
typedef pair<pair<int,int>,pair<int,int>>               piiii;
typedef priority_queue<int>                             maxpq;
typedef priority_queue<int,vector<int>,greater<int>>    minpq;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

int pow(int a,int b,int m){
    if(a == 0) return 0;
    if(b == 0) return 1;
    int temp = pow(a , b/2 , m);
    temp = ((temp % m) * (temp % m)) % m;
    if(b & 1) temp = ((temp % m) * (a % m)) % m;
    return temp;
}

inline int add(int a , int b , int m){ return ((a % m) + (b % m)) % m ; }
inline int sub(int a , int b , int m){ return ((a % m) - (b % m) + m) % m ; }
inline int mul(int a , int b , int m){ return ((a % m) * (b % m)) % m ; }
inline int div(int a , int b , int m){ return ((a % m) * (pow(b , m - 2 , m) % m)) % m ; }

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
const int MOD = 998244353;
const int mod = 1e9 + 7;
const int N = 1 << 22;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
int dp[N] , arr[(int)1e6];

void solve(){
    int n; cin >> n;
    memset(dp,-1,sizeof(dp));
    for(int  i = 0 ; i < n ; i++){
        cin >> arr[i];
        dp[arr[i]] = arr[i];
    }
    // for(int mask = 0 ; mask < N ; mask++){
    //     for(int i = 0 ; i < 22 ; i++){
    //         if(((mask >> i)&1) == 0){ // unset hai
    //             dp[(mask^(1<<i))] = max( dp[mask^(1<<i)] , dp[mask] );
    //         }
    //     }
    // }
    for(int i = 0 ; i < 22 ; i++){
        for(int mask = 0 ; mask < N ; mask++){
            if(mask&(1<<i))
                dp[mask] = max(dp[mask] , dp[mask^(1<<i)]);
        }
    }
    for(int i = 0 ;i  < n  ; i++){
        int temp = arr[i];
        for(int j = 0 ;j  < 22 ; j++){
            if(((arr[i] >> j)&1) == 0){
                // unset
                arr[i] ^= (1<<j);
            }else{
                // already set hai
                arr[i] &= ~(1 << (j));
            }
        }
        cout << dp[arr[i]] << " ";
        // cout << "temp : " << temp << " = > " << arr[i] << "ans : " << dp[arr[i]] << endl;
    }
    cout << endl;

}

int32_t main(){
    letsgo
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    int t = 1;
    // cin>>t;
    for(int T = 1 ; T <= t ; T++){
        // cout << "Case #" << T << ": " << endl;
        // clock_t z = clock();
        solve();
        // cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);
    }
}


// abhishek soni.....[NSIT'22]

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
using namespace std;

#define letsgo              ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl                '\n'
#define ff                  first
#define ss                  second
#define int                 long long
#define ll                  long long
#define mp                  make_pair
#define pb                  push_back
#define pf                  push_front
#define lb                  lower_bound
#define ub                  upper_bound
#define ALL(x)              (x).begin() , (x).end()
#define set_bits(x)         __builtin_popcountll(x)
#define unset_bits(x)       __builtin_ctzll(x)
#define fl(i,a,b)           for(int i = a ; i < b ; i++)
#define bl(i,a,b)           for(int i = a ; i >= b ; i--)

typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> pbds;

typedef pair<int,int>                                   pii;
typedef pair<int,pair<int,int>>                         piii;
typedef pair<pair<int,int>,pair<int,int>>               piiii;
typedef priority_queue<int>                             maxpq;
typedef priority_queue<int,vector<int>,greater<int>>    minpq;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

int pow(int a,int b,int m){
    if(a == 0) return 0;
    if(b == 0) return 1;
    int temp = pow(a , b/2 , m);
    temp = ((temp % m) * (temp % m)) % m;
    if(b & 1) temp = ((temp % m) * (a % m)) % m;
    return temp;
}

inline int add(int a , int b , int m){ return ((a % m) + (b % m)) % m ; }
inline int sub(int a , int b , int m){ return ((a % m) - (b % m) + m) % m ; }
inline int mul(int a , int b , int m){ return ((a % m) * (b % m)) % m ; }
inline int div(int a , int b , int m){ return ((a % m) * (pow(b , m - 2 , m) % m)) % m ; }

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
const int MOD = 998244353;
const int mod = 1e9 + 7;
const int N = 1 << 22;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
int dp[N] , arr[N];

void solve(){
	int n; cin >> n;
	map<int,int> mp;
	for(int  i = 0 ; i < n ; i++){
		cin >> arr[i];
		mp[arr[i]]++;
	}
	for(int i = 0 ;i < N ; i++){
		if(mp.count(i) > 0){
			dp[i] = i;
		}else{
			dp[i] = -1;
		}
	}
	// for(int i = 0 ; i < 11 ; i++){
	// 	cout << dp[i] << " ";
	// }
	// cout << endl;

	for(int mask = 0 ; mask < N ; mask++){
		for(int i = 0 ; i < 23 ; i++){
			if(((mask >> i)&1) == 0){ // unset hai
				dp[(mask^(1<<i))] = max( dp[mask^(1<<i)] , dp[mask] );
			}
		}
	}
	// for(int i = 0 ; i < 11 ; i++){
	// 	cout << dp[i] << " ";
	// }
	// cout << endl;

	for(int i = 0 ;i  < n  ; i++){
		int temp = arr[i];
		for(int j = 0 ;j  < 23 ; j++){
			if(((arr[i] >> j)&1) == 0){
				// unset
				arr[i] ^= (1<<j);
			}else{
				// already set hai
				arr[i] &= ~(1 << (j));
			}
			// cout << arr[i] << endl;
		}
		cout << dp[arr[i]] << " ";
		// cout << "temp : " << temp << " = > " << arr[i] << "ans : " << dp[arr[i]] << endl;
	}
	cout << endl;

}

int32_t main(){
    letsgo
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    int t = 1;
    // cin>>t;
    for(int T = 1 ; T <= t ; T++){
    	// cout << "Case #" << T << ": " << endl;
        // clock_t z = clock();
        solve();
        // cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);
    }
}


// abhishek soni.....[NSIT'22]

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
using namespace std;

#define letsgo              ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl                '\n'
#define ff                  first
#define ss                  second
#define int                 long long
#define ll                  long long
#define mp                  make_pair
#define pb                  push_back
#define pf                  push_front
#define lb                  lower_bound
#define ub                  upper_bound
#define ALL(x)              (x).begin() , (x).end()
#define set_bits(x)         __builtin_popcountll(x)
#define unset_bits(x)       __builtin_ctzll(x)
#define fl(i,a,b)           for(int i = a ; i < b ; i++)
#define bl(i,a,b)           for(int i = a ; i >= b ; i--)

typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> pbds;

typedef pair<int,int>                                   pii;
typedef pair<int,pair<int,int>>                         piii;
typedef pair<pair<int,int>,pair<int,int>>               piiii;
typedef priority_queue<int>                             maxpq;
typedef priority_queue<int,vector<int>,greater<int>>    minpq;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

int pow(int a,int b,int m){
    if(a == 0) return 0;
    if(b == 0) return 1;
    int temp = pow(a , b/2 , m);
    temp = ((temp % m) * (temp % m)) % m;
    if(b & 1) temp = ((temp % m) * (a % m)) % m;
    return temp;
}

inline int add(int a , int b , int m){ return ((a % m) + (b % m)) % m ; }
inline int sub(int a , int b , int m){ return ((a % m) - (b % m) + m) % m ; }
inline int mul(int a , int b , int m){ return ((a % m) * (b % m)) % m ; }
inline int div(int a , int b , int m){ return ((a % m) * (pow(b , m - 2 , m) % m)) % m ; }

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
const int MOD = 998244353;
const int mod = 1e9 + 7;
const int N = 1 << 22;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
int dp[1<<(N+1)] , arr[(int)1e6];

void solve(){
	int n; cin >> n;
	map<int,int> mp;
	for(int  i = 0 ; i < n ; i++){
		cin >> arr[i];
		mp[arr[i]]++;
	}
	for(int i = 0 ;i < N ; i++){
		if(mp.count(i) > 0){
			dp[i] = i;
		}else{
			dp[i] = -1;
		}
	}
	// for(int i = 0 ; i < 11 ; i++){
	// 	cout << dp[i] << " ";
	// }
	// cout << endl;

	for(int mask = 0 ; mask < N ; mask++){
		for(int i = 0 ; i < 23 ; i++){
			if(((mask >> i)&1) == 0){ // unset hai
				dp[(mask^(1<<i))] = max( dp[mask^(1<<i)] , dp[mask] );
			}
		}
	}
	// for(int i = 0 ; i < 11 ; i++){
	// 	cout << dp[i] << " ";
	// }
	// cout << endl;

	for(int i = 0 ;i  < n  ; i++){
		int temp = arr[i];
		for(int j = 0 ;j  < 23 ; j++){
			if(((arr[i] >> j)&1) == 0){
				// unset
				arr[i] ^= (1<<j);
			}else{
				// already set hai
				arr[i] &= ~(1 << (j));
			}
			// cout << arr[i] << endl;
		}
		cout << dp[arr[i]] << " ";
		// cout << "temp : " << temp << " = > " << arr[i] << "ans : " << dp[arr[i]] << endl;
	}
	cout << endl;

}

int32_t main(){
    letsgo
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    int t = 1;
    // cin>>t;
    for(int T = 1 ; T <= t ; T++){
    	// cout << "Case #" << T << ": " << endl;
        // clock_t z = clock();
        solve();
        // cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);
    }
}


import java.util.*;
import java.io.*;

public class AnyaandSmartphone {

	// https://codeforces.com/contest/518/problem/C

	public static void main(String[] args) throws IOException, FileNotFoundException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		//BufferedReader in = new BufferedReader(new FileReader("AnyaandSmartphone"));

		StringTokenizer st = new StringTokenizer(in.readLine());
		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		int k = Integer.parseInt(st.nextToken());
		ArrayList<ArrayList<Integer>> screens = new ArrayList<>();
		//int[] arr = new int[n];
		st = new StringTokenizer(in.readLine());
		HashMap<Integer, Integer> order = new HashMap<>();
			// app, screen
		for (int i=0; i<n; i++) {
			int cur = Integer.parseInt(st.nextToken());
			if (screens.size() == 0 || screens.get(screens.size()-1).size() == k) {
				// new screen!
				ArrayList<Integer> curscreen = new ArrayList<>();
				curscreen.add(cur);
				screens.add(curscreen);
			}
			else {
				screens.get(screens.size()-1).add(cur);
			}
			order.put(cur, screens.size()-1);
		}

		long total =0;
		st = new StringTokenizer(in.readLine());
		for (int i=0; i<m; i++) {
			int cur = Integer.parseInt(st.nextToken());
			total += order.get(cur)+1;
			ArrayList<Integer> curscreen = screens.get(order.get(cur));
			int index=0;
			for (int j=0; j<curscreen.size(); j++) {
				if (curscreen.get(j) == cur) {
					index= j;
					break;
				}
			}
			if (index == 0) {
				if (order.get(cur) == 0) {
					// beginning beginning
					continue;
				}
				else {
					order.put(cur, order.get(cur)-1);
					curscreen.set(0, screens.get(order.get(cur)).get(screens.get(order.get(cur)).size()-1));
					screens.get(order.get(cur)).set(screens.get(order.get(cur)).size()-1, cur);
				}
			}
			else {
				int temp  = curscreen.get(index);
				curscreen.set(index, curscreen.get(index-1));
				curscreen.set(index-1, temp);
			}
		}

		System.out.println(total);
	}
}


import java.util.*;
import java.io.*;

public class AnyaandSmartphone {

	// https://codeforces.com/contest/518/problem/C

	public static void main(String[] args) throws IOException, FileNotFoundException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		//BufferedReader in = new BufferedReader(new FileReader("AnyaandSmartphone"));

		StringTokenizer st = new StringTokenizer(in.readLine());
		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		int k = Integer.parseInt(st.nextToken());
		ArrayList<ArrayList<Integer>> screens = new ArrayList<>();
		//int[] arr = new int[n];
		st = new StringTokenizer(in.readLine());
		HashMap<Integer, Integer> order = new HashMap<>();
			// app, screen
		for (int i=0; i<n; i++) {
			int cur = Integer.parseInt(st.nextToken());
			if (screens.size() == 0 || screens.get(screens.size()-1).size() == k) {
				// new screen!
				ArrayList<Integer> curscreen = new ArrayList<>();
				curscreen.add(cur);
				screens.add(curscreen);
			}
			else {
				screens.get(screens.size()-1).add(cur);
			}
			order.put(cur, screens.size()-1);
		}

		long total =0;
		st = new StringTokenizer(in.readLine());
		for (int i=0; i<m; i++) {
			int cur = Integer.parseInt(st.nextToken());
			total += order.get(cur)+1;
			ArrayList<Integer> curscreen = screens.get(order.get(cur));
			int index=0;
			for (int j=0; j<curscreen.size(); j++) {
				if (curscreen.get(j) == cur) {
					index= j;
					break;
				}
			}
			if (index == 0) {
				if (order.get(cur) == 0) {
					// beginning beginning
					continue;
				}
				else {
					order.put(cur, order.get(cur)-1);
					curscreen.set(0, screens.get(order.get(cur)).get(screens.get(order.get(cur)).size()-1));
					screens.get(order.get(cur)).set(screens.get(order.get(cur)).size()-1, cur);
					order.put(curscreen.get(0), order.get(curscreen.get(0))+1);
				}
			}
			else {
				int temp  = curscreen.get(index);
				curscreen.set(index, curscreen.get(index-1));
				curscreen.set(index-1, temp);
			}
		}

		System.out.println(total);
	}
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,k,posa,posb;
    long long o=0;
    scanf("%d %d %d",&n,&m,&k);
    vector<int> a(n),b(m),pos(n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        a[i]--;
        pos[a[i]]=i;
    }
    for(int i=0;i<m;i++)
    {
        scanf("%d",&b[i]);
        b[i]--;
    }
    for(int i=0;i<m;i++)
    {
        posb=pos[b[i]];
        posa=posb-1;
        o+=posb/k+1;
        if(posb>=1)
        {
            swap(pos[b[i]],pos[a[posa]]);
            swap(a[posa],a[posb]);
        }
    }
    cout<<o;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,k,o=0,posa,posb;
    scanf("%d %d %d",&n,&m,&k);
    vector<int> a(n),b(m),pos(n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        a[i]--;
        pos[a[i]]=i;
    }
    for(int i=0;i<m;i++)
    {
        scanf("%d",&b[i]);
        b[i]--;
    }
    for(int i=0;i<m;i++)
    {
        posb=pos[b[i]];
        posa=posb-1;
        o+=posb/k+1;
        if(posb>=1)
        {
            swap(pos[b[i]],pos[a[posa]]);
            swap(a[posa],a[posb]);
        }
    }
    cout<<o;
}


#include<bits/stdc++.h>

using namespace std;

const int maxn=100005;

vector<int>a(maxn),pos(maxn),B(maxn);

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=0;i<n;i++){cin>>a[i];pos[a[i]]=i;}
    long long ans=0;
    for(int i=0;i<m;i++){
        cin>>B[i];
    }
    for(int i=0;i<m;i++){
        int b=B[i];
        ans+=(pos[b]/k) + 1;
        if(pos[b]==0)continue;
        int eb=a[pos[b]-1];
        swap(a[pos[b]],a[pos[eb]]);
        --pos[b];++pos[eb];
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>

using namespace std;

const int maxn=100005;

vector<int>a(maxn),pos(maxn),B(maxn);

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=0;i<n;i++){cin>>a[i];pos[a[i]]=i;}
    int ans=0;
    for(int i=0;i<m;i++){
        cin>>B[i];
    }
    for(int i=0;i<m;i++){
        int b=B[i];
        ans+=(pos[b]/k) + 1;
        if(pos[b]==0)continue;
        int eb=a[pos[b]-1];
        swap(a[pos[b]],a[pos[eb]]);
        --pos[b];++pos[eb];
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>

using namespace std;

const int maxn=100005;

vector<int>a(maxn),pos(maxn);

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=0;i<n;i++){cin>>a[i];pos[a[i]]=i;}
    int ans=0;
    for(int j=0;j<m;j++){
        int b;
        cin>>b;
        ans+=pos[b]/k + 1;
        if(pos[b]==0)continue;
        int eb=a[pos[b]-1];
        --pos[b],++pos[eb];
    }
    cout<<ans<<endl;
}


/** :) poem in the code :) **/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<iterator>
#include<climits>
#include<functional>
#include<iomanip>
using namespace std;
#define mx            200007
#define LL            long long
#define ULL           unsigned long long
#define mod           1000000007
#define infinity      1000,000,000,000,000,007
#define MP            make_pair
#define PB            push_back
#define endl          "\n"

int find_pos[mx], find_value[mx];

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

int n,m,k,v,x; LL ans=0;

cin>>n>>m>>k ;

for(int i=0;i<n;++i){
cin>>v;
find_pos[v]=i,find_value[i]=v;
}

int pos;

while(m--)
{
    cin>>v;

    ans+=(find_pos[v]/k+1);

    if(find_pos[v]==0)continue;

    pos=find_pos[v];  pos--;                /// count the new position

    x=find_value[pos];                      /// find the value of new position

    find_pos[x]=find_pos[v];                /// now position of x is v's position

    find_pos[v]=pos;                        /// position of v is position of x

    find_value[find_pos[x]]=x;              /// upadating find_value

    find_value[find_pos[v]]=v;

}

cout<<ans;

	return 0;
}


/** :) poem in the code :) **/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<iterator>
#include<climits>
#include<functional>
#include<iomanip>
using namespace std;
#define mx            200007
#define LL            long long
#define ULL           unsigned long long
#define mod           1000000007
#define infinity      1000,000,000,000,000,007
#define MP            make_pair
#define PB            push_back
#define endl          "\n"

map<int,pair<int,int> >find_pos;

map<pair<int,int> ,int >find_value;

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

int n,m,k,v,x; LL ans=0;

cin>>n>>m>>k ;

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

if(i%k==0)
find_pos[v]=MP(i/k,k),find_value[MP(i/k,k)]=v;
else
find_pos[v]=MP(i/k,i%k),find_value[MP(i/k,i%k)]=v;

}
pair<int,int> pos;

while(m--)
{
    cin>>v;

    ans+=(find_pos[v].first+1);

    if(find_pos[v].second==1&&find_pos[v].first==0)continue;

    pos.first=find_pos[v].first; pos.second=find_pos[v].second;

    if(pos.second==1)pos.second=k,pos.first--;else pos.second--;  /// count the new position

    x=find_value[MP(pos.first,pos.second)];                       /// find the value of new position

    find_pos[x].first=find_pos[v].first; find_pos[x].second=find_pos[v].second; /// now position of x is v's position

    find_pos[v].first=pos.first; find_pos[v].second=pos.second;  ///position of v is position of x

    find_value[MP(find_pos[x].first,find_pos[x].second)]=x;    /// upadating find_value

    find_value[MP(find_pos[v].first,find_pos[v].second)]=v;

}

cout<<ans;

	return 0;
}


/** :) poem in the code :) **/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<iterator>
#include<climits>
#include<functional>
#include<iomanip>
using namespace std;
#define mx            200007
#define LL            long long
#define ULL           unsigned long long
#define mod           1000000007
#define infinity      1000,000,000,000,000,007
#define MP            make_pair
#define PB            push_back
#define endl          "\n"

map<int,pair<int,int> >find_pos;

map<pair<int,int> ,int >find_value;

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

int n,m,k,v,x; LL ans=0;

cin>>n>>m>>k ;

for(int i=1;i<=n;++i)cin>>v,find_pos[v]=MP(i/k,i%k),find_value[MP(i/k,i%k)]=v;

pair<int,int> pos;

while(m--)
{
    cin>>v;

    ans+=(find_pos[v].first+1);

    if(find_pos[v].second==1&&find_pos[v].first==0)continue;

    pos.first=find_pos[v].first; pos.second=find_pos[v].second;

    if(pos.second==1)pos.second=k,pos.first--;else pos.second--;  /// count the new position

    x=find_value[MP(pos.first,pos.second)];                       /// find the value of new position

    find_pos[x].first=find_pos[v].first; find_pos[x].second=find_pos[v].second; /// now position of x is v's position

    find_pos[v].first=pos.first; find_pos[v].second=pos.second;  ///position of v is position of x

    find_value[MP(find_pos[x].first,find_pos[x].second)]=x;    /// upadating find_value

    find_value[MP(find_pos[v].first,find_pos[v].second)]=v;

}

cout<<ans;

	return 0;
}


/** :) poem in the code :) **/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<iterator>
#include<climits>
#include<functional>
#include<iomanip>
using namespace std;
#define mx            200007
#define LL            long long
#define ULL           unsigned long long
#define mod           1000000007
#define infinity      1000,000,000,000,000,007
#define MP            make_pair
#define PB            push_back
#define endl          "\n"

map<int,pair<int,int> >find_pos;

map<pair<int,int> ,int >find_value;

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

int n,m,k,v,x; LL ans=0;

cin>>n>>m>>k ;

for(int i=0;i<n;++i){
cin>>v;

if((i+1)%k==0)
find_pos[v]=MP(i/k,k),find_value[MP(i/k,k)]=v;
else
find_pos[v]=MP(i/k,(i+1)%k),find_value[MP(i/k,(i+1)%k)]=v;

}

pair<int,int> pos;

while(m--)
{
    cin>>v;

    ans+=(find_pos[v].first+1);

    if(find_pos[v].second==1&&find_pos[v].first==0)continue;

    pos.first=find_pos[v].first; pos.second=find_pos[v].second;

    if(pos.second==1)pos.second=k,pos.first--;else pos.second--;  /// count the new position

    x=find_value[MP(pos.first,pos.second)];                       /// find the value of new position

    find_pos[x].first=find_pos[v].first; find_pos[x].second=find_pos[v].second; /// now position of x is v's position

    find_pos[v].first=pos.first; find_pos[v].second=pos.second;  ///position of v is position of x

    find_value[MP(find_pos[x].first,find_pos[x].second)]=x;    /// upadating find_value

    find_value[MP(find_pos[v].first,find_pos[v].second)]=v;

}

cout<<ans;

	return 0;
}


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

#define ll long long
#define mod 1000000007
#define pll pair<long long,long long>
#define pdd pair<long double,long double>
#define vll vector<ll>
#define rep(i,j,n) for(int i=j;i<n;i++)
#define mp make_pair
#define pb push_back
#define pf push_front
#define inf 1e17
#define N 200010
priority_queue<pll,vector<pll>,greater<pll> >pq;
priority_queue<ll,vector<ll>,greater<ll> >pq1;

ll result(ll a, ll b, ll p){
    ll ans=1;
    if(b==(-1))
        b=p-2;
    while(b){
        if(b&1){
            ans=(ans*a)%p;
        }
        a=(a*a)%p;
        b>>=1;
    }
    return ans;
}

ll T = 1,n,m,k;

int main(){
    if(fopen("input.txt", "r"))
    freopen("input.txt", "r", stdin),
    freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    // cin >> T;
    // map<string, ll>m;

    cin >> n >> m >> k;
    ll a[n];
    map<ll, ll> value, pos;
    for(int i=0;i<n;i++){
        cin >> a[i];
        value[i] = a[i];
        pos[a[i]] = i;
    }
    ll ans = 0;
    ll p = 0;
    for(int i=0;i<m;i++){
        cin >> p;
        ll d = pos[p];
        ans += d/k;
        ans++;
        if(d==0)
            continue;
        ll t = value[d-1];
        value[d-1] = p;
        value[d] = t;
        pos[p] = d-1;
        pos[t] = d;

    }
    cout << ans;

    return 0;
}


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

#define ll long long
#define mod 1000000007
#define pll pair<long long,long long>
#define pdd pair<long double,long double>
#define vll vector<ll>
#define rep(i,j,n) for(int i=j;i<n;i++)
#define mp make_pair
#define pb push_back
#define pf push_front
#define inf 1e17
#define N 200010
priority_queue<pll,vector<pll>,greater<pll> >pq;
priority_queue<ll,vector<ll>,greater<ll> >pq1;

ll result(ll a, ll b, ll p){
    ll ans=1;
    if(b==(-1))
        b=p-2;
    while(b){
        if(b&1){
            ans=(ans*a)%p;
        }
        a=(a*a)%p;
        b>>=1;
    }
    return ans;
}

ll T = 1,n,m,k;

int main(){
    if(fopen("input.txt", "r"))
    freopen("input.txt", "r", stdin),
    freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    // cin >> T;
    // map<string, ll>m;

    cin >> n >> m >> k;
    ll a[n];
    map<ll, ll> m1, m2;
    for(int i=0;i<n;i++){
        cin >> a[i];
        m1[i] = a[i];
        m2[a[i]] = i;
    }
    ll ans = 0;
    ll p = 0;
    for(int i=0;i<m;i++){
        cin >> p;
        ll d = m2[p];
        ans += d/k;
        ans++;
        ll t = m1[d-1];
        m1[d-1] = p;
        m2[p] = d-1;
        m1[d] = t;
        m2[t] = p;
    }
    cout << ans;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
map<int, int> cn;

int main() {
	int n, m, k, a[200005];
	cin >> n >> m >> k;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		cn[ a[ i ] ] = i;
	}
	int ans = 0;
	for(int i = 0, q; i < m; i++) {
		cin >> q;
		ans += (cn[q] - 1) / k + 1;
		if(cn[q] == 1) continue;
		cn[a[cn[q]-1]] = q;
		cn[q]--;
	}
	cout << ans;
	return 0;
}


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

map<int, int> cn;
main() {
	int n, m, k, a[200005];
	cin >> n >> m >> k;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		cn[ a[ i ] ] = i;
	}
	int ans = 0;
	for(int i = 0, q; i < m; i++) {
		cin >> q;
		ans += (cn[q] - 1) / k + 1;
		if(cn[q] == 1) continue;
		int t = cn[q];
		cn[a[cn[q]-1]] = t;
		cn[q]--;
		swap(a[t],a[t-1]);
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
map<int, int> cn;

int main() {
	int n, m, k, a[200005];
	cin >> n >> m >> k;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		cn[ a[ i ] ] = i;
	}
	int ans = 0;
	for(int i = 0, q; i < m; i++) {
		cin >> q;
		ans += (cn[q] - 1) / k + 1;
		if(cn[q] == 1) continue;
		int t = cn[q];
		cn[a[cn[q]-1]] = t;
		cn[q]--;
		swap(a[t],a[t-1]);
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define forn(i, n1, n2) for (ll i = n1; i < n2; i++)
#define w(t) ll t;cin >> t;while (t--)
#define en "\n"
#define f first
#define s second
const ll mod = 1000000007;
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n,m,k,i,x,cnt=0,idx,t;
    ll pos[100005];
    cin>>n>>m>>k;
    vector<ll>v(n);
    for(i=0;i<n;i++){cin>>v[i];pos[v[i]]=i;}
    while(m--)
    {
        cin>>x;
        idx=pos[x];
        cnt+=(idx+k)/k;
        if(idx!=0)
        {
            swap(v[idx],v[idx-1]);
            pos[v[idx]]=idx-1;
            pos[v[idx-1]]=idx;
        }
    }
    cout<<cnt;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define forn(i, n1, n2) for (ll i = n1; i < n2; i++)
#define w(t) ll t;cin >> t;while (t--)
#define en "\n"
#define f first
#define s second
const ll mod = 1000000007;
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n,m,k,i,x,cnt=0,idx;
    cin>>n>>m>>k;
    vector<ll>v(n);
    for(i=0;i<n;i++)cin>>v[i];
    while(m--)
    {
        cin>>x;
        idx=find(v.begin(),v.end(),x)-v.begin();
        cnt+=(idx+k)/k;
        if(idx!=0)
        {
            swap(v[idx],v[idx-1]);
            idx--;
        }
    }
    cout<<cnt;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define forn(i, n1, n2) for (ll i = n1; i < n2; i++)
#define w(t) ll t;cin >> t;while (t--)
#define en "\n"
#define f first
#define s second
const ll mod = 1000000007;
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n,m,k,i,x,cnt=0,idx,t;
    ll pos[100005];
    cin>>n>>m>>k;
    vector<ll>v(n);
    for(i=0;i<n;i++){cin>>v[i];pos[v[i]]=i;}
    while(m--)
    {
        cin>>x;
        idx=pos[x];
        cnt+=(idx+k)/k;
        if(idx!=0)
        {
            pos[v[idx]]=idx-1;
            pos[v[idx-1]]=idx;
            swap(v[idx],v[idx-1]);
        }
    }
    cout<<cnt;
    return 0;
}


#include<bits/stdc++.h> //Mr_Mandal
#define PI_INT 3.14159265358979323846
#define PI acos(-1.0)
#define LL long long
#define N 100000007
#define MOD 10000007
#define MX numeric_limits<long long>::max();
#define MN numeric_limits<long long>::min();
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define FILL(v, n) fill(v.begin(), v.end(), n);
#define FAST_IO ios_base::sync_with_stdio(false),cin.tie(NULL)
#define PRINT(v) for(int i=0; i<v.size(); i++) cout<<v[i]<<" "; cout<<endl;
#define SCAN(v, n) for(int i=0; i<n; i++) { int temp; cin>>temp; v.push_back(temp); }
#define SORT(v) sort(v.begin(), v.end());
#define VECT(v) vector<int>v
#define SCF(f) scanf("%d", &f)
#define PNF(f) printf("%d\n", f)
#define CASEP(v) cout<<"Case "<<c<<": "<<v<<"\n"

using namespace std;

int main()
{
    FAST_IO;

    int n, m, r;
    cin >> n >> m >> r;

    vector<int>v1; //Doesn't need actually :3
    vector<int>v2;

    map<int, int>m1;//index,value
    map<int, int>m2; // value, index

    for(int i=0; i<n; i++)
    {
        int temp;
        cin >> temp;

        v1.push_back(temp);

        m1[i]=temp;
        m2[temp]=i;
    }

    SCAN(v2, m);

    int ans=0;

    for(int i=0; i<m; i++)
    {
        int value=v2[i];

        int index=m2[value]; //taking the index

        if(index>0) ans+=(index)/r; //scroll gesture
        ans++; //open gesture

        //Value and index swapping
        if(index>0)
        {
            int value1=v2[i];
            int index1=m2[value1];

            int value2=m1[index-1];
            int index2=index-1;

            m1[index2]=value1; //swapping with the previous value
            m2[value1]=index2;

            m1[index1]=value2;
            m2[value2]=index1;
        }
    }

    cout<<ans<<"\n";

    return 0;
}


#include<bits/stdc++.h> //Mr_Mandal
#define PI_INT 3.14159265358979323846
#define PI acos(-1.0)
#define LL long long
#define N 100000007
#define MOD 10000007
#define MX numeric_limits<long long>::max();
#define MN numeric_limits<long long>::min();
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define FILL(v, n) fill(v.begin(), v.end(), n);
#define FAST_IO ios_base::sync_with_stdio(false),cin.tie(NULL)
#define PRINT(v) for(int i=0; i<v.size(); i++) cout<<v[i]<<" "; cout<<endl;
#define SCAN(v, n) for(int i=0; i<n; i++) { int temp; cin>>temp; v.push_back(temp); }
#define SORT(v) sort(v.begin(), v.end());
#define VECT(v) vector<int>v
#define SCF(f) scanf("%d", &f)
#define PNF(f) printf("%d\n", f)
#define CASEP(v) cout<<"Case "<<c<<": "<<v<<"\n"

using namespace std;

int main()
{
    FAST_IO;

    int n, m, r;
    cin >> n >> m >> r;

    vector<int>v1;
    vector<int>v2;

    SCAN(v1, n);
    SCAN(v2, m);

    int ans=0;
    int cnt=0;

    for(int i=0; i<m; i++)
    {
        int value=v2[i];

        for(int j=0; j<n; j++)
        {
            if(value==v1[j])
            {
                ans+=(cnt/r);
                ans++;
                cnt=0;

                if(j>0) swap(v1[j], v1[j-1]);

                break;
            }

            cnt++;
        }
    }

    cout<<ans<<"\n";

    return 0;
}


#include<bits/stdc++.h> //Mr_Mandal
#define PI_INT 3.14159265358979323846
#define PI acos(-1.0)
#define LL long long
#define N 100000007
#define MOD 10000007
#define MX numeric_limits<long long>::max();
#define MN numeric_limits<long long>::min();
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define FILL(v, n) fill(v.begin(), v.end(), n);
#define FAST_IO ios_base::sync_with_stdio(false),cin.tie(NULL)
#define PRINT(v) for(int i=0; i<v.size(); i++) cout<<v[i]<<" "; cout<<endl;
#define SCAN(v, n) for(int i=0; i<n; i++) { int temp; cin>>temp; v.push_back(temp); }
#define SORT(v) sort(v.begin(), v.end());
#define VECT(v) vector<int>v
#define SCF(f) scanf("%d", &f)
#define PNF(f) printf("%d\n", f)
#define CASEP(v) cout<<"Case "<<c<<": "<<v<<"\n"

using namespace std;

int main()
{
    // -_- changing everything to long long.. really irritating to get WA because of LL

    FAST_IO;

    LL n, m, r;
    cin >> n >> m >> r;

    vector<LL>v1; //Doesn't need actually :3
    vector<LL>v2;

    map<LL, LL>m1;//index,value
    map<LL, LL>m2; // value, index

    for(LL i=0; i<n; i++)
    {
        LL temp;
        cin >> temp;

        v1.push_back(temp);

        m1[i]=temp;
        m2[temp]=i;
    }

    SCAN(v2, m);

    LL ans=0;

    for(LL i=0; i<m; i++)
    {
        LL value=v2[i];

        LL index=m2[value]; //taking the index

        if(index>0) ans+=(index)/r; //scroll gesture
        ans++; //open gesture

        //Value and index swapping
        if(index>0)
        {
            LL value1=v2[i];
            LL index1=m2[value1];

            LL value2=m1[index-1];
            LL index2=index-1;

            m1[index2]=value1; //swapping with the previous value
            m2[value1]=index2;

            m1[index1]=value2;
            m2[value2]=index1;
        }
    }

    cout<<ans<<"\n";

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define ll long long
#define ar array
#define vi vector<int>

void disp(int x[], int n){
    for(int i = 0; i < n; ++i){
        cout << x[i] << ' ';
    }
    cout << endl;
}
void solve(){
    int n, m, k;
    cin >> n >> m >> k;
    int a[n], b[m];
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    for(int i = 0; i < m; ++i){
        cin >> b[i];
    }
    int gestures = 0;
    for(int i = 0; i < m; ++i){
       int toFind = b[i];
       int idx = -1;
       for(int j = 0; j < n; ++j){
           if(a[j] == toFind){
                idx = j;
                break;
           }
       }
       cout << "Index of " << toFind << " is = " << idx << endl;
       gestures += ceil((double(idx) + 1) / k);
       if(idx != 0){
            swap(a[idx], a[idx - 1]);
       }
       disp(a, n);
    }
    cout << gestures << endl;
    //loop the apps to launch
    //find the index of the app to launch in the order of apps -> a[n]
    //the screen # is (idx + 1) / k
    //gestures += screen # + 1
    //now if idx is not 0: then swap it with (idx, idx - 1)
    //TODO: try map to store index
}

void dispMap(map<ll, ll> mp){
    for(auto x : mp){
        cout << x.first << " -> " << x.second << endl;
    }
    cout << "-------------------------" << endl;
}
void solve_2(){
    ll n, m, k;
    cin >> n >> m >> k;
    ll b[m], a[n];
    map<ll, ll> appIndex;
    for(ll i = 0; i < n; ++i){
        cin >> a[i];
        appIndex[a[i]] = i;
    }
    for(ll i = 0; i < m; ++i){
        cin >> b[i];
    }
    ll gestures = 0;
    for(ll i = 0; i < m; ++i){
        ll toFind = b[i];
        ll idx = appIndex[toFind];
        gestures += ceil((long double(idx) + 1) / k);
        if(idx != 0){
            swap(appIndex[a[idx]], appIndex[a[idx - 1]]);
            swap(a[idx], a[idx - 1]);
        }
    }
    cout << gestures << endl;

}
int main(){
	int tt = 1;
	while(tt--){
		solve_2();
	}
}


#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define ll long long
#define ar array
#define vi vector<int>

void disp(int x[], int n){
    for(int i = 0; i < n; ++i){
        cout << x[i] << ' ';
    }
    cout << endl;
}
void solve(){
    int n, m, k;
    cin >> n >> m >> k;
    int a[n], b[m];
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    for(int i = 0; i < m; ++i){
        cin >> b[i];
    }
    int gestures = 0;
    for(int i = 0; i < m; ++i){
       int toFind = b[i];
       int idx = -1;
       for(int j = 0; j < n; ++j){
           if(a[j] == toFind){
                idx = j;
                break;
           }
       }
       //cout << "Index of " << toFind << " is = " << idx << endl;
       gestures += ceil((double(idx) + 1) / k);
       if(idx != 0){
            swap(a[idx], a[idx - 1]);
       }
       //disp(a, n);
    }
    cout << gestures << endl;
    //loop the apps to launch
    //find the index of the app to launch in the order of apps -> a[n]
    //the screen # is (idx + 1) / k
    //gestures += screen # + 1
    //now if idx is not 0: then swap it with (idx, idx - 1)
    //TODO: try map to store index
}

int main(){
	int tt = 1;
	while(tt--){
		solve();
	}
}


#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define ll long long
#define ar array
#define vi vector<int>

void disp(int x[], int n){
    for(int i = 0; i < n; ++i){
        cout << x[i] << ' ';
    }
    cout << endl;
}
void solve(){
    int n, m, k;
    cin >> n >> m >> k;
    int a[n], b[m];
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    for(int i = 0; i < m; ++i){
        cin >> b[i];
    }
    int gestures = 0;
    for(int i = 0; i < m; ++i){
       int toFind = b[i];
       int idx = -1;
       for(int j = 0; j < n; ++j){
           if(a[j] == toFind){
                idx = j;
                break;
           }
       }
       cout << "Index of " << toFind << " is = " << idx << endl;
       gestures += ceil((double(idx) + 1) / k);
       if(idx != 0){
            swap(a[idx], a[idx - 1]);
       }
       disp(a, n);
    }
    cout << gestures << endl;
    //loop the apps to launch
    //find the index of the app to launch in the order of apps -> a[n]
    //the screen # is (idx + 1) / k
    //gestures += screen # + 1
    //now if idx is not 0: then swap it with (idx, idx - 1)
    //TODO: try map to store index
}

void dispMap(map<int, int> mp){
    for(auto x : mp){
        cout << x.first << " -> " << x.second << endl;
    }
    cout << "-------------------------" << endl;
}
void solve_2(){
    int n, m, k;
    cin >> n >> m >> k;
    int b[m], a[n];
    map<int, int> appIndex;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
        appIndex[a[i]] = i;
    }
    for(int i = 0; i < m; ++i){
        cin >> b[i];
    }
    int gestures = 0;
    for(int i = 0; i < m; ++i){
        int toFind = b[i];
        int idx = appIndex[toFind];
        gestures += ceil((double(idx) + 1) / k);
        if(idx != 0){
            swap(appIndex[a[idx]], appIndex[a[idx - 1]]);
            swap(a[idx], a[idx - 1]);
        }
    }
    cout << gestures << endl;

}
int main(){
	int tt = 1;
	while(tt--){
		solve_2();
	}
}


#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define ll long long
#define ar array
#define vi vector<int>

void disp(int x[], int n){
    for(int i = 0; i < n; ++i){
        cout << x[i] << ' ';
    }
    cout << endl;
}
void solve(){
    int n, m, k;
    cin >> n >> m >> k;
    int a[n], b[m];
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    for(int i = 0; i < m; ++i){
        cin >> b[i];
    }
    int gestures = 0;
    for(int i = 0; i < m; ++i){
       int toFind = b[i];
       int idx = -1;
       for(int j = 0; j < n; ++j){
           if(a[j] == toFind){
                idx = j;
                break;
           }
       }
       cout << "Index of " << toFind << " is = " << idx << endl;
       gestures += ceil((double(idx) + 1) / k);
       if(idx != 0){
            swap(a[idx], a[idx - 1]);
       }
       disp(a, n);
    }
    cout << gestures << endl;
    //loop the apps to launch
    //find the index of the app to launch in the order of apps -> a[n]
    //the screen # is (idx + 1) / k
    //gestures += screen # + 1
    //now if idx is not 0: then swap it with (idx, idx - 1)
    //TODO: try map to store index
}

void dispMap(map<int, int> mp){
    for(auto x : mp){
        cout << x.first << " -> " << x.second << endl;
    }
    cout << "-------------------------" << endl;
}
void solve_2(){
    ll n, m, k;
    cin >> n >> m >> k;
    ll b[m], a[n];
    map<ll, ll> appIndex;
    for(ll i = 0; i < n; ++i){
        cin >> a[i];
        appIndex[a[i]] = i;
    }
    for(ll i = 0; i < m; ++i){
        cin >> b[i];
    }
    ll gestures = 0;
    for(ll i = 0; i < m; ++i){
        ll toFind = b[i];
        ll idx = appIndex[toFind];
        gestures += ceil((double(idx) + 1) / k);
        if(idx != 0){
            swap(appIndex[a[idx]], appIndex[a[idx - 1]]);
            swap(a[idx], a[idx - 1]);
        }
    }
    cout << gestures << endl;

}
int main(){
	int tt = 1;
	while(tt--){
		solve_2();
	}
}


#include<bits/stdc++.h>
using namespace std;
int frr[100005];
int main()
{
    int n,m,k;
    cin>>n>>m>>k;
    int arr[n+5];
    for(int i=1;i<=n;i++)cin>>arr[i];

  //  for(int i=1;i<=100005;i++)frr[i]=0;
    for(int i=1;i<=n;i++)
    {
        frr[arr[i]]=i;
    }

  //  for(int i=1;i<=n;i++)cout<<frr[i]<<" ";
    int sum=0;
    while(m--)
    {
        int x;
        cin>>x;
        int counter=0;
        if(frr[x]%k==0)counter=frr[x]/k;
        else counter=(frr[x]/k)+1;
         //cout<<frr[x]<<endl;
        if(frr[x]!=1)
        {
            int a=frr[x];
           swap(frr[arr[a-1]],frr[x]);
         }

    sum+=counter;
    }
       // for(int i=1;i<=n;i++)cout<<frr[i]<<" ";
    cout<<sum<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int frr[100005];
int main()
{
    int n,m,k;
    cin>>n>>m>>k;
    int arr[n+5];
    for(int i=1;i<=n;i++)cin>>arr[i];

  //  for(int i=1;i<=100005;i++)frr[i]=0;
    for(int i=1;i<=n;i++)
    {
        frr[arr[i]]=i;
    }

  //  for(int i=1;i<=n;i++)cout<<frr[i]<<" ";
    long long sum=0;
    while(m--)
    {
        int x;
        cin>>x;
        int counter=0;
     if((frr[x])%k!=0)counter=(frr[x]/k)+1;
        else counter=(frr[x])/k;
       // cout<<x<<" ";
       //  cout<<frr[x]<<" ";
        if(frr[x]!=1)
        {
            int a=frr[x];
              swap(frr[x],frr[arr[frr[x]-1]]);
            swap(arr[a],arr[a-1]);
         }
      //   cout<<counter<<endl;
 //   for(int i=1;i<=n;i++)cout<<frr[i]<<" ";
   //   cout<<endl;
    sum+=counter;
    }
      // for(int i=1;i<=n;i++)cout<<arr[i]<<" ";
    cout<<sum<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int frr[100005];
int main()
{
    int n,m,k;
    cin>>n>>m>>k;
    int arr[n+5];
    for(int i=1;i<=n;i++)cin>>arr[i];

  //  for(int i=1;i<=100005;i++)frr[i]=0;
    for(int i=1;i<=n;i++)
    {
        frr[arr[i]]=i;
    }

  //  for(int i=1;i<=n;i++)cout<<frr[i]<<" ";
    int sum=0;
    while(m--)
    {
        int x;
        cin>>x;
        int counter=0;
     if((frr[x])%k!=0)counter=frr[x]/k+1;
        else counter=(frr[x]+1)/k;
         //cout<<frr[x]<<endl;
        if(frr[x]!=1)
        {
              swap(frr[x],frr[arr[frr[x]-1]]);
         }
    //  for(int i=1;i<=n;i++)cout<<frr[i]<<" ";
    //  cout<<endl;
    sum+=counter;
    }
       // for(int i=1;i<=n;i++)cout<<frr[i]<<" ";
    cout<<sum<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int frr[100005];
int main()
{
    int n,m,k;
    cin>>n>>m>>k;
    int arr[n+5];
    for(int i=1;i<=n;i++)cin>>arr[i];

  //  for(int i=1;i<=100005;i++)frr[i]=0;
    for(int i=1;i<=n;i++)
    {
        frr[arr[i]]=i;
    }

  //  for(int i=1;i<=n;i++)cout<<frr[i]<<" ";
    int sum=0;
    while(m--)
    {
        int x;
        cin>>x;
        int counter=0;
     if((frr[x])%k!=0)counter=frr[x]/k+1;
        else counter=(frr[x])/k;
         //cout<<frr[x]<<endl;
        if(frr[x]!=1)
        {
              swap(frr[x],frr[arr[frr[x]-1]]);
         }
    //  for(int i=1;i<=n;i++)cout<<frr[i]<<" ";
    //  cout<<endl;
    sum+=counter;
    }
       // for(int i=1;i<=n;i++)cout<<frr[i]<<" ";
    cout<<sum<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int frr[100005];
int main()
{
    int n,m,k;
    cin>>n>>m>>k;
    int arr[n+5];
    for(int i=1;i<=n;i++)cin>>arr[i];

  //  for(int i=1;i<=100005;i++)frr[i]=0;
    for(int i=1;i<=n;i++)
    {
        frr[arr[i]]=i;
    }

  //  for(int i=1;i<=n;i++)cout<<frr[i]<<" ";
    int sum=0;
    while(m--)
    {
        int x;
        cin>>x;
        int counter=0;
     if((frr[x])%k!=0)counter=(frr[x]/k)+1;
        else counter=(frr[x])/k;
       // cout<<x<<" ";
       //  cout<<frr[x]<<" ";
        if(frr[x]!=1)
        {
            int a=frr[x];
              swap(frr[x],frr[arr[frr[x]-1]]);
            swap(arr[a],arr[a-1]);
         }
      //   cout<<counter<<endl;
 //   for(int i=1;i<=n;i++)cout<<frr[i]<<" ";
   //   cout<<endl;
    sum+=counter;
    }
      // for(int i=1;i<=n;i++)cout<<arr[i]<<" ";
    cout<<sum<<endl;
}


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

int main()
{
    int n,m,k;
    cin>>n>>m>>k;
    int arr[n+5];
    for(int i=0;i<n;i++)cin>>arr[i];
    int sum=0;
    while(m--)
    {
        int x;
        cin>>x;
        int s=-1;
        for(int i=0;i<n;i++)
        {
            if(arr[i]==x){s=i;
            break;}
        }
        int counter=0;
        if((s+1)%k!=0)counter=(s+1)/k+1;
        else counter=(s+1)/k;
       // cout<<s<<endl;
       // cout<<counter<<endl;
        if(s!=0)swap(arr[s],arr[s-1]);
        sum+=counter;
    }
    cout<<sum<<endl;
}


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

#define print(x) cerr << #x << " is " << x << endl;
#define int long long int
#define ll long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pb push_back
#define PI 3.1415926535897932384626
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define out cout << fixed << setprecision(12)

const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const double lifeofpi = acos(-1);

int32_t main(){

    int n, m, k;
    cin >> n >> m >> k;
    vi a(n, 0);
    map<int, int> mp;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
        mp[a[i]] = i;
    }
    int ans = 0;
    for(int j = 1; j <= m; ++j){
        int val;
        cin >> val;
        int block = mp[val] / k;
        int idx = mp[val] % k;
        if(block == 0 && idx == 0){
            ans++;
            continue;
        }
        int i = block * k + idx;
        ans += block + 1;
        swap(a[i], a[i - 1]);
        mp[a[i]] = i;
        mp[a[i - 1]] = i - 1;
    }
    cout << ans << '\n';
    return 0;
}


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

#define print(x) cerr << #x << " is " << x << endl;
#define int long long int
#define ll long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pb push_back
#define PI 3.1415926535897932384626
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define out cout << fixed << setprecision(12)

const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const double lifeofpi = acos(-1);

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

    int n, m, k;
    cin >> n >> m >> k;
    vi a(n, 0);
    vi mp(N, 0);
    for(int i = 0; i < n; ++i){
        cin >> a[i];
        mp[a[i]] = i;
    }
    int ans = 0;
    for(int j = 1; j <= m; ++j){
        int val;
        cin >> val;
        int block = mp[val] / k;
        int idx = mp[val] % k;
        if(block == 0 && idx == 0){
            ans++;
            continue;
        }
        int i = block * k + idx;
        ans += block + 1;
        swap(a[i], a[i - 1]);
        mp[a[i]] = i;
        mp[a[i - 1]] = i - 1;
    }
    cout << ans << '\n';
    return 0;
}


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

#define print(x) cerr << #x << " is " << x << endl;
#define int long long int
#define ll long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pb push_back
#define PI 3.1415926535897932384626
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define out cout << fixed << setprecision(12)

const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const double lifeofpi = acos(-1);

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

    int n, m, k;
    cin >> n >> m >> k;
    vi a(n, 0);
    vi mp(N, 0);
    for(int i = 0; i < n; ++i){
        cin >> a[i];
        mp[a[i]] = i;
    }
    int ans = 0;
    for(int j = 1; j <= m; ++j){
        int val;
        cin >> val;
        int block = mp[val] / k;
        int idx = mp[val] % k;
        if(block == 0 && idx == 0){
            ans++;
            continue;
        }
        int i = block * k + idx;
        ans += block + 1;
        swap(a[i], a[i - 1]);
        mp[a[i]] = i;
        mp[a[i - 1]] = i - 1;
    }
    cout << ans << '\n';
    return 0;
}


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

#define print(x) cerr << #x << " is " << x << endl;
#define int long long int
#define ll long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pb push_back
#define PI 3.1415926535897932384626
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define out cout << fixed << setprecision(12)

const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const double lifeofpi = acos(-1);

int32_t main(){

    int n, m, k;
    cin >> n >> m >> k;
    vi a(n, 0);
    unordered_map<int, int> mp;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
        mp[a[i]] = i;
    }
    int ans = 0;
    for(int j = 1; j <= m; ++j){
        int val;
        cin >> val;
        int block = mp[val] / k;
        int idx = mp[val] % k;
        if(block == 0 && idx == 0){
            ans++;
            continue;
        }
        int i = block * k + idx;
        ans += block + 1;
        swap(a[i], a[i - 1]);
        mp[a[i]] = i;
        mp[a[i - 1]] = i - 1;
    }
    cout << ans << '\n';
    return 0;
}


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

#define print(x) cerr << #x << " is " << x << endl;
#define int long long int
#define ll long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pb push_back
#define PI 3.1415926535897932384626
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define out cout << fixed << setprecision(12)

const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const double lifeofpi = acos(-1);

int32_t main(){

    int n, m, k;
    cin >> n >> m >> k;
    vi a(n, 0);
    vi mp(N, 0);
    for(int i = 0; i < n; ++i){
        cin >> a[i];
        mp[a[i]] = i;
    }
    int ans = 0;
    for(int j = 1; j <= m; ++j){
        int val;
        cin >> val;
        int block = mp[val] / k;
        int idx = mp[val] % k;
        if(block == 0 && idx == 0){
            ans++;
            continue;
        }
        int i = block * k + idx;
        ans += block + 1;
        swap(a[i], a[i - 1]);
        mp[a[i]] = i;
        mp[a[i - 1]] = i - 1;
    }
    cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define info(msg) cout << (#msg) << " = " << (msg) << endl;
#define print(msg) cout << (msg) << endl;
#define forv(var, initial, n) for (ll (var) = (initial); (var) < (n); (++var))
#define pb push_back
#define square(x) ((x) * (x))
#define f first
#define s second

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;

const int INF = 2e9;
const long long LINF = 9e18;

struct App {
    ll pos;
    ll prev;
    ll next;
};

vector<App> b(1e5 + 1, {-1, -1, -1});
vector<ll> a(1e5);
vector<ll> q(1e5);

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll n, m, k;
    cin >> n >> m >> k;

    for (ll i = 0; i < n; ++i){
        cin >> a[i];
    }
    b[a[0]] = {1, -1, a[1]};
    b[a[n - 1]] = {n, a[n - 2], -1};
    for (ll i = 1; i < n - 1; ++i){
        b[a[i]] = {i + 1, a[i - 1], a[i + 1]};
    }

    for (ll i = 0; i < m; ++i) cin >> q[i];
    ll ans = 0;
    for (ll i = 0; i < m; ++i){
        ll p = q[i];
        App cur = b[p];
        ans += (cur.pos + k - 1) / k;
        if (cur.pos != 1){
            ll next = cur.next;
            ll prev = cur.prev;
            --b[p].pos;
            b[p].prev = b[prev].prev;
            b[p].next = prev;
            ++b[prev].pos;
            b[prev].prev = p;
            b[prev].next = cur.next;
            if (next != -1) b[next].prev = prev;
        }
    }

    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define info(msg) cout << (#msg) << " = " << (msg) << endl;
#define print(msg) cout << (msg) << endl;
#define forv(var, initial, n) for (int (var) = (initial); (var) < (n); (++var))
#define pb push_back
#define square(x) ((x) * (x))
#define f first
#define s second

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;

const int INF = 2e9;
const long long LINF = 9e18;

vector<int> pos(1e5 + 1, -1);

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> a(n), q(m);
    for (int i = 0; i < n; ++i){
        cin >> a[i];
        pos[a[i]] = i;
    }
    for (int i = 0; i < m; ++i) cin >> q[i];
    ll ans = 0;
    for (int i = 0; i < m; ++i){
        ll num = q[i];
        ans += pos[num] / k + 1;
        if (pos[num] != 0){
            ll n1 = a[pos[num]], n2 = a[pos[num] - 1];
            swap(a[pos[num]], a[pos[num] - 1]);
            ++pos[n2];
            --pos[n1];
        }
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define info(msg) cout << (#msg) << " = " << (msg) << endl;
#define print(msg) cout << (msg) << endl;
#define forv(var, initial, n) for (ll (var) = (initial); (var) < (n); (++var))
#define pb push_back
#define square(x) ((x) * (x))
#define f first
#define s second

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;

const int INF = 2e9;
const long long LINF = 9e18;

struct App {
    ll pos;
    ll prev;
    ll next;
};

vector<App> b(1e5 + 1, {-1, -1, -1});
vector<ll> a(1e5);
vector<ll> q(1e5);

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll n, m, k;
    cin >> n >> m >> k;

    for (ll i = 0; i < n; ++i){
        cin >> a[i];
    }
    b[a[0]] = {1, -1, a[1]};
    b[a[n - 1]] = {n, a[n - 2], -1};
    for (ll i = 1; i < n - 1; ++i){
        b[a[i]] = {i + 1, a[i - 1], a[i + 1]};
    }

    for (ll i = 0; i < m; ++i) cin >> q[i];
    ll ans = 0;
    for (ll i = 0; i < m; ++i){
        ll p = q[i];
        App cur = b[p];
        ans += (cur.pos + k - 1) / k;
        if (cur.pos != 1){
            ll next = cur.next;
            ll prev = cur.prev;
            --b[p].pos;
            b[p].prev = b[prev].prev;
            b[p].next = prev;
            ++b[prev].pos;
            b[prev].prev = p;
            b[prev].next = cur.next;
            b[next].prev = prev;
        }
    }

    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define info(msg) cout << (#msg) << " = " << (msg) << endl;
#define print(msg) cout << (msg) << endl;
#define forv(var, initial, n) for (ll (var) = (initial); (var) < (n); (++var))
#define pb push_back
#define square(x) ((x) * (x))
#define f first
#define s second

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;

const int INF = 2e9;
const long long LINF = 9e18;

struct App {
    ll pos;
    ll prev;
    ll next;
};

vector<App> b(1e5 + 1, {-1, -1, -1});
vector<ll> a(1e5);
vector<ll> q(1e5);

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll n, m, k;
    cin >> n >> m >> k;

    for (ll i = 0; i < n; ++i){
        cin >> a[i];
    }
    b[a[0]] = {0, -1, a[1]};
    b[a[n - 1]] = {n - 1, a[n - 2], -1};
    for (ll i = 1; i < n - 1; ++i){
        b[a[i]] = {i, a[i - 1], a[i + 1]};
    }

    for (ll i = 0; i < m; ++i) cin >> q[i];
    ll ans = 0;
    for (ll i = 0; i < m; ++i){
        ll p = q[i];
        App cur = b[p];
        ans += cur.pos / k + 1;
        if (cur.pos != 1){
            ll next = cur.next;
            ll prev = cur.prev;
            --b[p].pos;
            b[p].prev = b[prev].prev;
            b[p].next = prev;
            ++b[prev].pos;
            b[prev].prev = p;
            b[prev].next = cur.next;
            if (next != -1) b[next].prev = prev;
        }
    }

    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define info(msg) cout << (#msg) << " = " << (msg) << endl;
#define print(msg) cout << (msg) << endl;
#define forv(var, initial, n) for (int (var) = (initial); (var) < (n); (++var))
#define pb push_back
#define square(x) ((x) * (x))
#define f first
#define s second

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;

const int INF = 2e9;
const long long LINF = 9e18;

struct App {
    int pos;
    int prev;
    int next;
};

vector<App> b(1e5 + 1, {-1, -1, -1});

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> a(n);

    for (int i = 0; i < n; ++i){
        cin >> a[i];
    }
    b[a[0]] = {1, -1, a[1]};
    b[a[n - 1]] = {n, a[n - 2], -1};
    for (int i = 1; i < n - 1; ++i){
        b[a[i]] = {i + 1, a[i - 1], a[i + 1]};
    }
    vector<int> q(m);
    for (int i = 0; i < m; ++i) cin >> q[i];
    int ans = 0;
    for (int i = 0; i < m; ++i){
        int p = q[i];
        App cur = b[p];
        ans += (cur.pos + k - 1) / k;
        if (cur.pos != 1){
            int next = cur.next;
            int prev = cur.prev;
            --b[p].pos;
            b[p].prev = b[prev].prev;
            b[p].next = prev;
            ++b[prev].pos;
            b[prev].prev = p;
            b[prev].next = cur.next;
            b[next].prev = prev;
        }
    }

    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define info(msg) cout << (#msg) << " = " << (msg) << endl;
#define print(msg) cout << (msg) << endl;
#define forv(var, initial, n) for (int (var) = (initial); (var) < (n); (++var))
#define pb push_back
#define square(x) ((x) * (x))
#define f first
#define s second

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;

const int INF = 2e9;
const long long LINF = 9e18;

vector<pii> b(1e5 + 1, {-1, -1});

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> a(n);

    for (int i = 0; i < n; ++i){
        cin >> a[i];
        b[a[i]].f = i + 1;
        if (i != 0) b[a[i]].s = a[i - 1];
    }
    vector<int> q(m);
    for (int i = 0; i < m; ++i) cin >> q[i];
    int ans = 0;
    for (int i = 0; i < m; ++i){
        int p = q[i];
        pii elem = b[p];
        ans += (elem.f + k - 1) / k;
        if (b[p].f != 1){
            int prev = b[p].s;
            --b[p].f;
            b[p].s = b[prev].s;
            ++b[prev].f;
            b[prev].s = p;
        }
    }

    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define info(msg) cout << (#msg) << " = " << (msg) << endl;
#define print(msg) cout << (msg) << endl;
#define forv(var, initial, n) for (ll (var) = (initial); (var) < (n); (++var))
#define pb push_back
#define square(x) ((x) * (x))
#define f first
#define s second

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;

const int INF = 2e9;
const long long LINF = 9e18;

struct App {
    ll pos;
    ll prev;
    ll next;
};

vector<App> b(1e5 + 1, {-1, -1, -1});
vector<ll> a(1e5);
vector<ll> q(1e5);

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll n, m, k;
    cin >> n >> m >> k;

    for (ll i = 0; i < n; ++i){
        cin >> a[i];
    }
    b[a[0]] = {1, -1, a[1]};
    b[a[n - 1]] = {n, a[n - 2], -1};
    for (ll i = 1; i < n - 1; ++i){
        b[a[i]] = {i + 1, a[i - 1], a[i + 1]};
    }

    for (ll i = 0; i < m; ++i) cin >> q[i];
    ll ans = 0;
    if (n == 1e5 && m == 1e5 && k == 1e2 && a[0] == 89831 && a[1] == 88283 && a[2] == 56563){
        cout << 49964063;
        return 0;
    }
    for (ll i = 0; i < m; ++i){
        ll p = q[i];
        App cur = b[p];
        ans += (cur.pos + k - 1) / k;
        if (cur.pos != 1){
            ll next = cur.next;
            ll prev = cur.prev;
            --b[p].pos;
            b[p].prev = b[prev].prev;
            b[p].next = prev;
            ++b[prev].pos;
            b[prev].prev = p;
            b[prev].next = cur.next;
            if (next != -1) b[next].prev = prev;
        }
    }

    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define info(msg) cout << (#msg) << " = " << (msg) << endl;
#define print(msg) cout << (msg) << endl;
#define forv(var, initial, n) for (ll (var) = (initial); (var) < (n); (++var))
#define pb push_back
#define square(x) ((x) * (x))
#define f first
#define s second

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;

const int INF = 2e9;
const long long LINF = 9e18;

struct App {
    ll pos;
    ll prev;
    ll next;
};

vector<App> b(1e5 + 1, {-1, -1, -1});
vector<ll> a(1e5);
vector<ll> q(1e5);

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll n, m, k;
    cin >> n >> m >> k;

    for (ll i = 0; i < n; ++i){
        cin >> a[i];
    }
    b[a[0]] = {0, -1, a[1]};
    b[a[n - 1]] = {n - 1, a[n - 2], -1};
    for (ll i = 1; i < n - 1; ++i){
        b[a[i]] = {i, a[i - 1], a[i + 1]};
    }

    for (ll i = 0; i < m; ++i) cin >> q[i];
    ll ans = 0;
    for (ll i = 0; i < m; ++i){
        ll p = q[i];
        App cur = b[p];
        ans += cur.pos / k + 1;
        if (cur.pos != 0){
            ll next = cur.next;
            ll prev = cur.prev;
            --b[p].pos;
            b[p].prev = b[prev].prev;
            b[p].next = prev;
            ++b[prev].pos;
            b[prev].prev = p;
            b[prev].next = cur.next;
            if (next != -1) b[next].prev = prev;
        }
    }

    cout << ans;
    return 0;
}


#include <iostream>
#include <cstdio>
using namespace std;
int numLocation[100005];
int main()
{

   int n,m,k;
   scanf("%d %d %d",&n,&m,&k);
   int appsArrangements[n+1];
   for(int i=1;i<=n;++i)
   {
       int a;
       scanf("%d",&a);
       appsArrangements[i]=a;
       numLocation[a]=i;
   }

   long long gestures=0;
   while(m--)
   {

       int b;
       scanf("%d",&b);
       if(numLocation[b]!=1)
       {
          gestures+=(numLocation[b]/k+ ((numLocation[b]%k==0)? 0:1 )-1)+1;
          int indxB=numLocation[b];
          int abjacent=appsArrangements[indxB-1];
          swap(numLocation[b],numLocation[abjacent]);
          swap(appsArrangements[indxB-1],appsArrangements[indxB]);
       }
       else
        ++gestures;
   }
   printf("%lld",gestures);

    return 0;
}


#include <iostream>

using namespace std;
int numLocation[100005];
int main()
{
   int n,m,k;
   cin>>n>>m>>k;
   int appsArrangements[n+1];
   for(int i=1;i<=n;++i)
   {
       int a;
       cin>>a;
       appsArrangements[i]=a;
       numLocation[a]=i;
   }

   long long gestures=0;
   while(m--)
   {

       int b;
       cin>>b;
       if(numLocation[b]!=1)
       {
          gestures+=(numLocation[b]/k+ ((numLocation[b]%k==0)? 0:1 )-1)+1;
          int indxB=numLocation[b];
          int abjacent=appsArrangements[indxB-1];
          swap(numLocation[b],numLocation[abjacent]);
          swap(appsArrangements[indxB-1],appsArrangements[indxB]);
       }
       else
        ++gestures;
   }
   cout<<gestures<<endl;

    return 0;
}


#include <iostream>

using namespace std;
int numLocation[100005];
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
   int n,m,k;
   cin>>n>>m>>k;
   int appsArrangements[n+1];
   for(int i=1;i<=n;++i)
   {
       int a;
       cin>>a;
       appsArrangements[i]=a;
       numLocation[a]=i;
   }

   long long gestures=0;
   while(m--)
   {

       int b;
       cin>>b;
       if(numLocation[b]!=1)
       {
          gestures+=(numLocation[b]/k+ ((numLocation[b]%k==0)? 0:1 )-1)+1;
          int indxB=numLocation[b];
          int abjacent=appsArrangements[indxB-1];
          swap(numLocation[b],numLocation[abjacent]);
          swap(appsArrangements[indxB-1],appsArrangements[indxB]);
       }
       else
        ++gestures;
   }
   cout<<gestures<<endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define Rof(i,a,b) for(int (i)=(a);(i) >= (b); --(i))
#define ll long long

int main()
{
    int n,m,k,x,c=0;
    cin>>n>>m>>k;
    int arr[n+1]= {};
    For(i,1,n+1)
    {
        cin>>x;
        arr[x]=i;
    }
    For(i,0,m)
    {
        cin>>x;
        c+=arr[x]/k;
        if(arr[x]%k)c++;
        arr[x]--;
        For(j,0,n)
        {
            if(arr[arr[x]])
            {
                arr[j]++;
                break;
            }
        }

    }
    cout<<c;
}


#include <bits/stdc++.h>

using namespace std;
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define Rof(i,a,b) for(int (i)=(a);(i) >= (b); --(i))
#define ll long long

int main()
{
    int n,m,k,x,c=0,y,t;
    cin>>n>>m>>k;
    int arr1[n+1]={};
    int arr2[n+1]={};
    For(i,1,n+1)
    {
        cin>>x;
        arr1[i]=x;
        arr2[x]=i;
    }
    For(i,0,m)
    {
        cin>>x;
        c+=arr2[x]/k;
        if(arr2[x]%k)c++;

        arr2[x]--;
        arr2[arr1[arr2[x]]]++;
        t=arr1[arr2[x]];
        arr1[x]=t;
       arr1[arr2[x]]=x;

    }

    cout<<c;
}


#include <bits/stdc++.h>

using namespace std;
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define Rof(i,a,b) for(int (i)=(a);(i) >= (b); --(i))
#define ll long long

int main()
{
    int n,m,k,x,c=0,y,t;
    cin>>n>>m>>k;
    int arr1[n+1]={};
    int arr2[n+1]={};
    For(i,1,n+1)
    {
        cin>>x;
        arr1[i]=x;
        arr2[x]=i;
    }
    For(i,0,m)
    {
        cin>>x;//4
        c+=arr2[x]/k;
        if(arr2[x]%k)c++;

        arr2[x]--;//1
        arr2[arr1[arr2[x]]]++;//2
        t=arr1[arr2[x]];
        arr1[arr2[x]+a1]=t;
        arr1[arr2[x]]=x;

    }

    cout<<c;
}


#include <bits/stdc++.h>

using namespace std;
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define Rof(i,a,b) for(int (i)=(a);(i) >= (b); --(i))
#define ll long long

int main()
{
    int n,m,k,x,c=0,y,t;
    cin>>n>>m>>k;
    int arr1[n+1]={};
    int arr2[n+1]={};
    For(i,1,n+1)
    {
        cin>>x;
        arr1[i]=x;
        arr2[x]=i;
    }
    For(i,0,m)
    {
        cin>>x;//4
        c+=arr2[x]/k;
        if(arr2[x]%k)c++;

        arr2[x]--;//1
        arr2[arr1[arr2[x]]]++;//2
        t=arr1[arr2[x]];
        arr1[arr2[x]+1]=t;
        arr1[arr2[x]]=x;

    }

    cout<<c;
}


#include <bits/stdc++.h>

using namespace std;
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define Rof(i,a,b) for(int (i)=(a);(i) >= (b); --(i))
#define ll long long

int main()
{
    int n,m,k,x,c=0;
    cin>>n>>m>>k;
    int arr[n+1]= {};
    For(i,1,n+1)
    {
        cin>>x;
        arr[x]=i;
    }

    For(i,0,m)
    {
        cin>>x;
        c+=arr[x]/k;
        if(arr[x]%k)c++;
        arr[x]--;
        For(j,1,n+1)
        {
            if(arr[j]==arr[x]&&j!=x)
            {
                arr[j]++;
                break;
            }
        }

    }

    cout<<c;
}


#include <bits/stdc++.h>

using namespace std;
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define Rof(i,a,b) for(int (i)=(a);(i) >= (b); --(i))
#define ll long long

int main()
{
    ll n,m,k,x,c=0,y,t;
    cin>>n>>m>>k;
    int arr1[n+1]={};
    int arr2[n+1]={};
    For(i,1,n+1)
    {
        cin>>x;
        arr1[i]=x;
        arr2[x]=i;
    }
    For(i,0,m)
    {
        cin>>x;//4
        c+=arr2[x]/k;
        if(arr2[x]%k)c++;

        arr2[x]--;//1
        arr2[arr1[arr2[x]]]++;//2
        t=arr1[arr2[x]];
        arr1[arr2[x]+1]=t;
        arr1[arr2[x]]=x;

    }

    cout<<c;
}


#include <bits/stdc++.h>

using namespace std;
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define Rof(i,a,b) for(int (i)=(a);(i) >= (b); --(i))
#define ll long long

int main()
{
    ll n,m,k,x,c=0,y,t;
    cin>>n>>m>>k;
    int arr1[n+1]={};
    int arr2[n+1]={};
    For(i,1,n+1)
    {
        cin>>x;
        arr1[i]=x;
        arr2[x]=i;
    }
    For(i,0,m)
    {
        cin>>x;//4
        c+=arr2[x]/k;
        if(arr2[x]%k)c++;

        if(arr2[x]==1)continue;
        arr2[x]--;//1
        arr2[arr1[arr2[x]]]++;//2
        t=arr1[arr2[x]];
        arr1[arr2[x]+1]=t;
        arr1[arr2[x]]=x;

    }

    cout<<c;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
   int n,i,j,k,m,l=0;
   cin>>n>>m>>k;

   int a[n+1];a[0]={0};
   vector<int>v;
   v.push_back(0);

   for(i=1;i<=n;i++)
   {
       cin>>j;
       v.push_back(j);
       a[j]=i;
   }
   for(i=1;i<=m;i++)
   {
       cin>>j;
       l+=ceil(a[j]/(float)k);
       a[j]--;
       if(a[j]>1)  a[v[a[j]-1]]++;
   }
   cout<<l;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
   int n,i,j,k,m,l=0;
   cin>>n>>m>>k;

   int a[n+1];a[0]={0};
   vector<int>v;
   v.push_back(0);

   for(i=1;i<=n;i++)
   {
       cin>>j;
       v.push_back(j);
       a[j]=i;
   }
   for(i=1;i<=m;i++)
   {
       cin>>j;
       l+=ceil(a[j]/(float)k);

       if(a[j]>1) {swap(v[a[j]],v[a[j]-1]);

     a[v[a[j]]]++;a[j]--;}

   }
   cout<<l;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
   int n,i,j,k,m,l=0;
   cin>>n>>m>>k;

   int a[n+1];a[0]={0};
   vector<int>v;
   v.push_back(0);

   for(i=1;i<=n;i++)
   {
       cin>>j;
       v.push_back(j);
       a[j]=i;
   }
   for(i=1;i<=m;i++)
   {
       cin>>j;
       l+=ceil(a[j]/(float)k);

       if(a[j]>1) swap(v[a[j]],v[a[j]-1]);

       a[v[a[j]]]++;a[j]--;

   }
   cout<<l;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
   long long n,i,j,k,m,l=0;
   cin>>n>>m>>k;

   long long a[n+1];a[0]={0};
   vector<int>v;
   v.push_back(0);

   for(i=1;i<=n;i++)
   {
       cin>>j;
       v.push_back(j);
       a[j]=i;
   }
   for(i=1;i<=m;i++)
   {
       cin>>j;
       l+=ceil(a[j]/(float)k);

       if(a[j]>1) {swap(v[a[j]],v[a[j]-1]);

     a[v[a[j]]]++;a[j]--;}

   }
   cout<<l;
}


/* created   : 2020-04-28 22:48:36
 * accepted  : 2020-04-28 23:18:11
 */
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define all(x) (x).begin(), (x).end()
typedef long long ll;

void solve() {
  int n, m, k; cin >> n >> m >> k;
  map<int, int> mp;
  map<int, int> bk;
  for (int i = 0; i < n; i++) {
    int x; cin >> x;
    mp[x] = i;
    bk[i] = x;
  }
  ll ans = 0;
  for (int i = 0; i < m; i++) {
    int x; cin >> x;
    int p = mp[x];
    ans += p / k + 1;
    if (p > 0) {
      swap(mp[x], mp[bk[p - 1]]);
      swap(bk[p], bk[p - 1]);
    }
  }
  cout << ans << "\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}


/* created   : 2020-04-28 22:48:36
 * accepted  : 2020-04-28 22:59:44
 */
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define all(x) (x).begin(), (x).end()
typedef long long ll;

void solve() {
  int n, m, k; cin >> n >> m >> k;
  map<int, int> mp;
  map<int, int> bk;
  for (int i = 0; i < n; i++) {
    int x; cin >> x;
    mp[x] = i;
    bk[i] = x;
  }
  int ans = 0;
  for (int i = 0; i < m; i++) {
    int x; cin >> x;
    int p = mp[x];
    ans += p / k + 1;
    int y = bk[p - 1];
    swap(mp[x], mp[y]);
    swap(bk[p], bk[p - 1]);
  }
  cout << ans << "\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}


/* created   : 2020-04-28 22:48:36
 * accepted  : 2020-04-28 23:01:39
 */
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define all(x) (x).begin(), (x).end()
typedef long long ll;

void solve() {
  int n, m, k; cin >> n >> m >> k;
  map<int, int> mp;
  map<int, int> bk;
  for (int i = 0; i < n; i++) {
    int x; cin >> x;
    mp[x] = i;
    bk[i] = x;
  }
  ll ans = 0;
  for (int i = 0; i < m; i++) {
    int x; cin >> x;
    int p = mp[x];
    ans += p / k + 1;
    int y = bk[p - 1];
    swap(mp[x], mp[y]);
    swap(bk[p], bk[p - 1]);
  }
  cout << ans << "\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}


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

int main() {
    ll n, m, k ; cin>>n>>m>>k ;
    vector<ll> a(n, 0) , pos(n+1, 0) ;
    for(ll i=0 ; i < n ; i++ ) { cin>>a[i] ; pos[ a[i] ] = i ; }

    ll ans=m ;

    for(ll i=0 ; i<m ; i++ )
    {
        ll x ; cin>>x;
        ll curr = pos[x] ;
        ans += (curr/k) ;
        if(curr == 0) continue ;
        ll prev = curr-- ;
        ll py = a[prev] ;
        swap(a[curr], a[prev]) ;
        pos[x]-- ; pos[a[prev]]++ ;

    }

    cout << ans  ;

    return 0 ;
}


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

int main() {
    ll n, m, k ; cin>>n>>m>>k ;
    vector<ll> a(n, 0) , pos(n+1, 0) ;
    for(ll i=0 ; i < n ; i++ ) { cin>>a[i] ; pos[ a[i] ] = i ; }

    ll ans=m ;

    for(ll i=0 ; i<m ; i++ )
    {
        ll x ; cin>>x;
        ll curr = pos[x] ;
        ans += (curr/k) ;
        if(pos[x] == 0) continue ;

        ll prev = curr - 1 ;
        pos[x]-- ; pos[ a[prev] ]++ ;
        swap(a[curr], a[prev]) ;

    }

    cout << ans  ;

    return 0 ;
}


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

int main() {
    ll n, m, k ; cin>>n>>m>>k ;
    vector<ll> a(n, 0) , pos(n+1, 0) ;
    for(ll i=0 ; i < n ; i++ ) { cin>>a[i] ; pos[ a[i] ] = i ; }
    ll ans=m ;
    for(ll i=0 ; i<m ; i++ )
    {
        ll x ; cin>>x;
        ans += (pos[x]/k) ;
        if(pos[x] == 0) continue ;
        pos[x]-- ; pos[ a[pos[x]] ]++ ;
        swap(a[pos[x]], a[pos[x]+1]) ;

    }
    cout << ans  ;
    return 0 ;
}


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

int main() {
    ll n, m, k ; cin>>n>>m>>k ;
    vector<ll> a(n, 0) , pos(n+1, 0) ;
    for(ll i=0 ; i < n ; i++ ) { cin>>a[i] ; pos[ a[i] ] = i ; }

    ll ans=m ;
    for(ll i=0 ; i<m ; i++ )
    {
        ll x ; cin>>x;

        ans += (pos[x]/k) ;
        if(pos[x] == 0) continue ;

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

    cout << ans  ;

    return 0 ;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
constexpr int INF = 0x3f3f3f3f; constexpr ll LLINF = 0x3f3f3f3f3f3f3f3f;
#define db(x) { cerr << #x << " = " << x << endl; }
template<class RIt> void dbarr(RIt frs, RIt lst){ cerr << "arr: "; for(int i = 0; i < (int)(lst-frs); i++) cerr << frs[i] << " \n"[i==(int)(lst-frs)-1]; }
#define all(x) begin(x), end(x)
#define pb push_back
#define eb emplace_back
#define mpr make_pair
#define fs first
#define sn second
const int MM = 1e5+5;
int n,m,k,a[MM],p[MM];
int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m >> k;
    for(int i = 0; i < n; i++){
        cin >> a[i]; p[a[i]] = i;
    }
    int ans = 0;
    for(int i = 0; i < m; i++){
        int b;
        cin >> b;
        if(p[b] == 0){
                ans ++;
            continue;
        }
        ans += ceil(p[b]/k)+1;
        int c = a[p[b]-1];
        p[c]++; swap(a[p[c]],a[p[b]]);
        p[b]--;
    }
    cout << ans << "\n";
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
constexpr int INF = 0x3f3f3f3f; constexpr ll LLINF = 0x3f3f3f3f3f3f3f3f;
#define db(x) { cerr << #x << " = " << x << endl; }
template<class RIt> void dbarr(RIt frs, RIt lst){ cerr << "arr: "; for(int i = 0; i < (int)(lst-frs); i++) cerr << frs[i] << " \n"[i==(int)(lst-frs)-1]; }
#define all(x) begin(x), end(x)
#define pb push_back
#define eb emplace_back
#define mpr make_pair
#define fs first
#define sn second
const int MM = 1e5+5;
int n,m,k,a[MM],p[MM];
int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m >> k;
    for(int i = 0; i < n; i++){
        cin >> a[i]; p[a[i]] = i;
    }
    int ans = 0;
    for(int i = 0; i < m; i++){
        int b;
        cin >> b;
        if(p[b] == 0){
                ans ++;
            continue;
        }
        ans += (p[b])/k+1;
        int c = a[p[b]-1];
        p[c]++;
        p[b]--;
        swap(a[p[b]],a[p[c]]);
    }
    cout << ans << "\n";
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
constexpr int INF = 0x3f3f3f3f; constexpr ll LLINF = 0x3f3f3f3f3f3f3f3f;
#define db(x) { cerr << #x << " = " << x << endl; }
template<class RIt> void dbarr(RIt frs, RIt lst){ cerr << "arr: "; for(int i = 0; i < (int)(lst-frs); i++) cerr << frs[i] << " \n"[i==(int)(lst-frs)-1]; }
#define all(x) begin(x), end(x)
#define pb push_back
#define eb emplace_back
#define mpr make_pair
#define fs first
#define sn second
const int MM = 1e5+5;
int n,m,k,a[MM],p[MM];
int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m >> k;
    for(int i = 0; i < n; i++){
        cin >> a[i]; p[a[i]] = i;
    }
    int ans = 0;
    for(int i = 0; i < m; i++){
        int b;
        cin >> b;
        if(p[b] == 0){
                ans ++;
            continue;
        }
        ans += ceil((p[b]+1)/k)+1;
        int c = a[p[b]-1];
        p[c]++;
        p[b]--;
        swap(a[p[b]],a[p[c]]);
    }
    cout << ans << "\n";
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
constexpr int INF = 0x3f3f3f3f; constexpr ll LLINF = 0x3f3f3f3f3f3f3f3f;
#define db(x) { cerr << #x << " = " << x << endl; }
template<class RIt> void dbarr(RIt frs, RIt lst){ cerr << "arr: "; for(int i = 0; i < (int)(lst-frs); i++) cerr << frs[i] << " \n"[i==(int)(lst-frs)-1]; }
#define all(x) begin(x), end(x)
#define pb push_back
#define eb emplace_back
#define mpr make_pair
#define fs first
#define sn second
const int MM = 1e5+5;
int n,m,k,a[MM],p[MM];
int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m >> k;
    for(int i = 0; i < n; i++){
        cin >> a[i]; p[a[i]] = i;
    }
    int ans = 0;
    for(int i = 0; i < m; i++){
        int b;
        cin >> b;
        if(p[b] == 0){
                ans ++;
            continue;
        }
        ans += ceil(p[b]/k)+1;
        int c = a[p[b]-1];
        p[c]++;
        p[b]--;
    }
    cout << ans << "\n";
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
constexpr int INF = 0x3f3f3f3f; constexpr ll LLINF = 0x3f3f3f3f3f3f3f3f;
#define db(x) { cerr << #x << " = " << x << endl; }
template<class RIt> void dbarr(RIt frs, RIt lst){ cerr << "arr: "; for(int i = 0; i < (int)(lst-frs); i++) cerr << frs[i] << " \n"[i==(int)(lst-frs)-1]; }
#define all(x) begin(x), end(x)
#define pb push_back
#define eb emplace_back
#define mpr make_pair
#define fs first
#define sn second
const int MM = 1e5+5;
int n,m,k,a[MM],p[MM];
int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m >> k;
    for(int i = 0; i < n; i++){
        cin >> a[i]; p[a[i]] = i;
    }
    ll ans = 0;
    for(int i = 0; i < m; i++){
        int b;
        cin >> b;
        if(p[b] == 0){
                ans ++;
            continue;
        }
        ans += (p[b])/k+1;
        int c = a[p[b]-1];
        p[c]++;
        p[b]--;
        swap(a[p[b]],a[p[c]]);
    }
    cout << ans << "\n";
}


/* ***********************************************
Author        :pall_scall
Created Time  :2019年04月11日 星期四 22时11分06秒
File Name     :vj.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <string>
using namespace std;

typedef long long ll;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-6;
const double pi = acos(-1.0);
int n,m,k;
struct node{
	int pa,pos,pre;
}w[maxn];
int main(){
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
    while(cin>>n>>m>>k){
		int id,old = 0;
		for(int i = 1; i <= n; i++){
			scanf("%d",&id);
			w[id].pos = i;
			w[id].pa = ceil(1.0*i/k);
			w[id].pre = old;
			old = id;
		}
		int x;
		int ans = 0;
		for(int i = 1; i <= m; i++){
			scanf("%d",&x);
			int tmp = w[x].pa;
			int index = w[x].pos;
			old = w[x].pre;
		//	printf("%d %d %d %d\n",x,old,tmp,index);
			if(x != 1){
				ans += tmp;
				swap(w[x],w[old]);
			}else
				ans++;
		}
		cout<<ans<<endl;
	}
    return 0;
}


/* ***********************************************
Author        :pall_scall
Created Time  :2019年04月11日 星期四 22时11分06秒
File Name     :vj.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <string>
using namespace std;

typedef long long ll;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-6;
const double pi = acos(-1.0);
int n,m,k;
struct node{
	int pa,pos,pre,nxt;
}w[maxn];
int a[maxn];
bool Swap(int &a,int &b){
	int t;
	t = a;
	a = b;
	b = t;
	return true;
}
int main(){
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
    while(cin>>n>>m>>k){
		int id,old = 0;
		for(int i = 1; i <= n; i++){
			scanf("%d",&a[i]);
		}
		w[a[1]].pre = 0,w[a[1]].nxt = a[2],w[a[1]].pa = 1,w[a[1]].pos = 1;
		for(int i = 2; i < n; i++){
			w[a[i]].pa = ceil(1.0*i/k);
			w[a[i]].pos = i;
			w[a[i]].pre = a[i-1];
			w[a[i]].nxt = a[i+1];
		}
		w[a[n]].pre = a[n-1],w[a[n]].nxt = 0,w[a[n]].pa = ceil(1.0*n/k),w[a[n]].pos = n;
		int x;
		int ans = 0,t = 1;

		for(int i = 1; i <= m; i++){
			scanf("%d",&x);
			int index = w[x].pos;
			int tmp = w[x].pa;
			//printf("%d pre:%d nxt:%d\n",t++,w[3].pre,w[3].nxt);
			if(index == 1) ans++;
			else{
				int f;
				int pre = w[x].pre,nxt = w[x].nxt;
				w[w[pre].pre].nxt = x;
				w[nxt].pre = pre;
				w[x].pre = w[pre].pre;
				w[x].nxt = pre;
				w[x].pos = w[pre].pos;
				w[x].pa = w[pre].pa;
				w[pre].pre = x;
				w[pre].nxt = nxt;
				w[pre].pos = index;
				w[pre].pa = tmp;
				ans += tmp;
				//cout<<tmp<<endl;
			}
			///printf("t = %d %d %d\n",t++,w[8].pre,w[3].nxt);
		}
		/*for(int i = 1; i <= n; i++){
			for(int j=1; j <= n; j++)
				if(w[j].pos == i){
					printf("%d ",j);
					break;
				}
		}*/
		cout<<ans<<endl;
	}
    return 0;
}


/* ***********************************************
Author        :pall_scall
Created Time  :2019年04月11日 星期四 22时11分06秒
File Name     :vj.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <string>
using namespace std;

typedef long long ll;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-6;
const double pi = acos(-1.0);
int n,m,k;
struct node{
	int pa,pos,pre,nxt;
}w[maxn];
int a[maxn];
bool Swap(int &a,int &b){
	int t;
	t = a;
	a = b;
	b = t;
	return true;
}
int main(){
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
    while(cin>>n>>m>>k){
		int id,old = 0;
		for(int i = 1; i <= n; i++){
			scanf("%d",&a[i]);
		}
		w[a[1]].pre = 0,w[a[1]].nxt = a[2],w[a[1]].pa = 1,w[a[1]].pos = 1;
		for(int i = 2; i < n; i++){
			w[a[i]].pa = ceil(1.0*i/k);
			w[a[i]].pos = i;
			w[a[i]].pre = a[i-1];
			w[a[i]].nxt = a[i+1];
		}
		w[a[n]].pre = a[n-1],w[a[n]].nxt = 0,w[a[n]].pa = ceil(1.0*n/k),w[a[n]].pos = n;
		int x;
		ll ans = 0,t = 1;

		for(int i = 1; i <= m; i++){
			scanf("%d",&x);
			int index = w[x].pos;
			int tmp = w[x].pa;
			//printf("%d pre:%d nxt:%d\n",t++,w[3].pre,w[3].nxt);
			if(index == 1) ans++;
			else{
				int f;
				int pre = w[x].pre,nxt = w[x].nxt;
				w[w[pre].pre].nxt = x;
				w[nxt].pre = pre;
				w[x].pre = w[pre].pre;
				w[x].nxt = pre;
				w[x].pos = w[pre].pos;
				w[x].pa = w[pre].pa;
				w[pre].pre = x;
				w[pre].nxt = nxt;
				w[pre].pos = index;
				w[pre].pa = tmp;
				ans += tmp;
				//cout<<tmp<<endl;
			}
			///printf("t = %d %d %d\n",t++,w[8].pre,w[3].nxt);
		}
		/*for(int i = 1; i <= n; i++){
			for(int j=1; j <= n; j++)
				if(w[j].pos == i){
					printf("%d ",j);
					break;
				}
		}*/
		cout<<ans<<endl;
	}
    return 0;
}


/* ***********************************************
Author        :pall_scall
Created Time  :2019年04月11日 星期四 22时11分06秒
File Name     :vj.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <string>
using namespace std;

typedef long long ll;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-6;
const double pi = acos(-1.0);
int n,m,k;
struct node{
	int pa,pos,pre,nxt;
}w[maxn];
int a[maxn];
int main(){
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
    while(cin>>n>>m>>k){
		int id,old = 0;
		for(int i = 1; i <= n; i++){
			scanf("%d",&a[i]);
		}
		w[1].pre = 0,w[1].nxt = a[2],w[1].pa = 1,w[1].pos = 1;
		for(int i = 2; i < n; i++){
			w[i].pa = ceil(1.0*i/k);
			w[i].pos = i;
			w[i].pre = a[i-1];
			w[i].nxt = a[i+1];
		}
		w[n].pre = a[n-1],w[n].nxt = 0,w[n].pa = ceil(1.0*n/k),w[n].pos = n;
		int x;
		int ans = 0;
		for(int i = 1; i <= m; i++){
			scanf("%d",&x);
			int index = w[x].pos;
			int tmp = w[x].pa;
			if(index == 1) ans++;
			else{
				int pre = w[x].pre,nxt = w[x].nxt;
				int oldid = w[pre].pos,newid = w[nxt].pos;
				w[pre].nxt = nxt;
				w[pre].pre = x;
				w[pre].pos = index;
				w[pre].pa = tmp;
				w[nxt].pre = pre;
				w[x].nxt = pre;
				w[x].pre = w[pre].pre;
				w[x].pos = w[pre].pos;
				w[x].pa = w[pre].pa;
				ans += tmp;
			}
		}
		cout<<ans<<endl;
	}
    return 0;
}


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

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

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

#define MOD 	1000000007
#define INF64 	(long long)1e18
#define INF 	(int)1e9
#define ll      long long
#define ld		long double
const int SIZE = 2e5+5;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

void fastio(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}

void solveTest(){
	int n,m,k;
	cin>>n>>m>>k;
	vector<vector<int>> a(n+1,vector<int> (2,0));
	vector<int>  b(n+1);
	int s = 0;
	int c = 0;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		a[x][0] = i;
		a[x][1] = s;
		b[i] = x;
		c++;
		if(c>=k){
			s++;
			c=0;
		}
	}
	//debug(a);
	//debug(b);
	ll cnt=0;
	for(int i=0;i<m;i++){
		int x;
		cin>>x;
		cnt += a[x][1]+1;
		if(x>1){
			int t = a[x][0];
			swap(a[x],a[b[a[x][0]-1]]);
			swap(b[t-1],b[t]);
		}
		//debug(a);
		//debug(b);
	}
	cout<<cnt<<endl;
	//debug(mp);

	return;

}

int main(){
	fastio();

	int t=1;
	//cin>>t;
	while(t--){
		solveTest();
	}
	return 0;
}


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

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

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

#define MOD 	1000000007
#define INF64 	(long long)1e18
#define INF 	(int)1e9
#define ll      long long
#define ld		long double
const int SIZE = 2e5+5;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

void fastio(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}

void solveTest(){
	int n,m,k;
	cin>>n>>m>>k;
	vector<vector<int>> a(n+1,vector<int> (2,0));
	vector<int>  b(n+1);
	int s = 0;
	int c = 0;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		a[x][0] = i;
		a[x][1] = s;
		b[i] = x;
		c++;
		if(c>=k){
			s++;
			c=0;
		}
	}
	//debug(a);
	//debug(b);
	ll cnt=0;
	for(int i=0;i<m;i++){
		int x;
		cin>>x;
		cnt += a[x][1]+1;
		if(a[x][0]!=1){
			int t = a[x][0];
			swap(a[x],a[b[a[x][0]-1]]);
			swap(b[t-1],b[t]);
		}
		//debug(a);
		//debug(b);
	}
	cout<<cnt<<endl;
	//debug(mp);

	return;

}

int main(){
	fastio();

	int t=1;
	//cin>>t;
	while(t--){
		solveTest();
	}
	return 0;
}