Ayush is a cashier at the shopping center. Recently his department has started a ''click and collect" service which allows users to shop online. 

The store contains k items. n customers have already used the above service. Each user paid for m items. Let a_{ij} denote the j-th item in the i-th person's order.

Due to the space limitations all the items are arranged in one single row. When Ayush receives the i-th order he will find one by one all the items a_{ij} (1 ≤ j ≤ m) in the row. Let pos(x) denote the position of the item x in the row at the moment of its collection. Then Ayush takes time equal to pos(a_{i}1) + pos(a_{i}2) + ... + pos(a_{im}) for the i-th customer.

When Ayush accesses the x-th element he keeps a new stock in the front of the row and takes away the x-th element. Thus the values are updating.

Your task is to calculate the total time it takes for Ayush to process all the orders.

You can assume that the market has endless stock.


-----Input-----

The first line contains three integers n, m and k (1 ≤ n, k ≤ 100, 1 ≤ m ≤ k) — the number of users, the number of items each user wants to buy and the total number of items at the market.

The next line contains k distinct integers p_{l} (1 ≤ p_{l} ≤ k) denoting the initial positions of the items in the store. The items are numbered with integers from 1 to k.

Each of the next n lines contains m distinct integers a_{ij} (1 ≤ a_{ij} ≤ k) — the order of the i-th person.


-----Output-----

Print the only integer t — the total time needed for Ayush to process all the orders.


-----Example-----
Input
2 2 5
3 4 1 2 5
1 5
3 1

Output
14



-----Note-----

Customer 1 wants the items 1 and 5.

pos(1) = 3, so the new positions are: [1, 3, 4, 2, 5].

pos(5) = 5, so the new positions are: [5, 1, 3, 4, 2].

Time taken for the first customer is 3 + 5 = 8.

Customer 2 wants the items 3 and 1.

pos(3) = 3, so the new positions are: [3, 5, 1, 4, 2].

pos(1) = 3, so the new positions are: [1, 3, 5, 4, 2].

Time taken for the second customer is 3 + 3 = 6.

Total time is 8 + 6 = 14.

Formally pos(x) is the index of x in the current row.

Difficulty: interview

Starter code:

Solutions:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
	orders = map(int, input().split())
	for order in orders:
		ans += a.index(order) + 1
		a.remove(order)
		a.insert(0, order)
print(ans)

n, m, k = list(map(int, input().split()))
pos = list(map(int, input().split()))
d = 0
for i in range(n):
    for a in list(map(int, input().split())):
        for j in range(k):
            if pos[j] == a:
                del(pos[j])
                pos = [a] + pos
                d += j + 1
print(d)
                


n,m,k=list(map(int,input().split()))
p=list(map(int,input().split()))
a=[list(map(int,input().split())) for i in range(n)]
st=0
for i in range(n):
	for j in range(m):
		for g in range(k):
			if a[i][j]==p[g]:
				p.insert(0,p[g])
				del(p[g+1])
				st+=g+1
				g-=1		
print(st)


def get_index(el, lst):
    for idx, element in enumerate(lst):
        if element == el:
            return idx

    return -1

def __starting_point():
    n, m, k = (int(cur_num) for cur_num in input().split())

    things = [int(cur_num) for cur_num in input().split()]

    ans = 0

    for _ in range(n):

        bought = [int(cur_num) for cur_num in input().split()]

        for purchase in bought:

            idx = get_index(purchase, things)

            ans += idx+1

            del things[idx]

            things = [purchase] + things

    print(ans)
__starting_point()

n, m, k = list(map(int, input().split()))
ps = list(map(int, input().split()))
xs = []
for _ in range(n):
    xs.append(list(map(int, input().split())))

def get(x):
    i = ps.index(x)
    ps[1:i + 1] = ps[0:i]
    ps[0] = x
    return i + 1

print(sum([sum(map(get, ys)) for ys in xs]))


n, m, k = map(int, input().split())

goods = list(map(int, input().split()))
total_time = 0

for i in range(n):
    goods_i = list(map(int, input().split()))
    for j in range(m):
        pos = goods.index(goods_i[j])
        total_time += pos + 1
        goods.remove(goods_i[j])
        goods = [goods_i[j]] + goods
        
print(total_time)

import sys
import math
import itertools as it
import operator as op
import fractions as fr


n,m,k = map(int, sys.stdin.readline().split())
P = list(map(int, sys.stdin.readline().split()))
P = P[::-1]

r = 0

for _ in range(n):
  Aj = map(int, sys.stdin.readline().split())
  for a in Aj:
    idx = P.index(a)
    r += (k-idx)
    del P[idx]
    P.append(a)

print(r)

from sys import stdin, stdout
from collections import deque
numbers, everyq, allq = map(int, stdin.readline().split())
items = deque(list(map(int, stdin.readline().split())))
cnt = 0
for i in range(numbers):
    current = map(int, stdin.readline().split())
    for n in current:
        ind = 0
        while items[ind] != n:
            ind += 1
        cnt += ind + 1
        value = items[ind]
        for j in range(ind, 0, -1):
            items[j] = items[j - 1]
        items[0] = value
        
stdout.write(str(cnt))

s = input().split()
n = int(s[0])
m = int(s[1])
k = int(s[2])
pos = 0
goods = [int (x) for x in input().split()]
for i in range(n):
	buy = input().split()
	for u in buy:
		x = int(u)
		pos = pos + goods.index(x) + 1
		goods.remove(x)
		goods.insert(0, x)
print (pos)

n, m, k = map(int, input().split())
p, v = input().split(), 0
for i in range(n):
    for x in input().split():
        c = p.index(x)
        v += c + 1
        p.pop(c)
        p.insert(0, x)
print(v)

#!/usr/bin/env python
n, m, k = list(map(int, input().split()))
l = list(map(int, input().split()))
t = 0

for i in range(n):
    for item in map(int, input().split()):
                t += l.index(item) + 1
                l.remove(item)
                l.insert(0, item)
print(t)


s = input().split(" ")
goods_count = int(s[0])
goods = input().split(" ")
t = 0
for i in range(goods_count):
    s = input().split(" ")
    for item in s:
        t += goods.index(item) + 1
        del goods[goods.index(item)]
        goods.insert(0, item)
print(t)

n, m, k = list([int(q) for q in input().split()])
k_lst = list([int(q) for q in input().split()])
order_lst = []
for i in range(n):
	lst = list([int(q) for q in input().split()])
	order_lst += lst

# def calctime(a, ta, b, tb, h, m):
# 	start_queue = list(range(300, 1439))[::b]
# 	start_m = 60 * h + m
# 	arrive_m = min(start_m + ta, END_M)
# 	total = 0
# 	for index, num in enumerate(start_queue):
# 		if num > start_m - tb and num < arrive_m:
# 			total += 1
# 	return total


def calcCost(n,m,k,k_lst):
	total_min = 0
	_index = 0
	for i in range(n*m):
		_item = order_lst[_index]
		for index, item in enumerate(k_lst):
			if item == _item:
				total_min += index + 1
				break
		k_lst = [k_lst[index]] + k_lst[0:index] + k_lst[index+1:]
		_index += 1
	return total_min

print(calcCost(n, m, k, k_lst))







n,m,k=list(map(int,input().split()))
p=list(map(int,input().split()))
p.reverse()
ans=0
for i in range(n):
    z=list(map(int,input().split()))
    for j in z:
        v=0
        for k in range(len(p)-1,-1,-1):
            v+=1
            if p[k]==j:
                ans+=v
                p.pop(k)
                p.append(j)
                break
print(ans)


tmp = input().split(' ')
n = int(tmp[0])
m = int(tmp[1])
k = int(tmp[2])
tmp = input().split(' ')
pos = []
need = []
for i in range(k):
	pos.append(int(tmp[i]))
time = 0
for i in range(n):
	tmp = input().split(' ')
	need.clear()
	for j in range(m):
		need.append(int(tmp[j]))
	for j in range(m):
		curpos = pos.index(need[j])
		time = time + curpos + 1
		del pos[curpos]
		pos.insert(0,need[j])

print(time)


n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))


time = 0


for i in range(n):
    c = list(map(int, input().split()))
    for j in c:
        for k in range(len(a)):
            if a[k] == j:
                p = k
                break
        time += p + 1
        a = [j] + a[:k] + a[k + 1:]

print(time)


from sys import stdin as fin
# fin = open("ecr12b.in", "r")

class node:
    def __init__(self, val=None):
        self.prev = self.next = None
        self.val = val

    @staticmethod
    def link(node1, node2):
        node1.next = node2
        node2.prev = node1
        return node2

    @staticmethod
    def move(node1, node2, inode1, inode2):
        if inode1.prev is not None:
            inode1.prev.next = inode2.next
        if inode2.next is not None:
            inode2.next.prev = inode1.prev
        node1 = node.link(node1, inode1)
        node2 = node.link(inode2, node2)
        return node1, node2


n, m, k = map(int, fin.readline().split())
arr = list(map(int, fin.readline().split()))

# p = [None] * k
# for i in range(k):
#     p[arr[i] - 1] = i

cur = line = node()
for num in arr:
    cur = node.link(cur, node(num))
res = 0
for i in range(n):
    buy = list(map(int, fin.readline().split()))
    for num in buy:
        cur = line
        cnt = 0
        while cur.val != num:
            cur = cur.next
            cnt += 1
        res += cnt
        if line.next != cur:
            line = node.move(line, line.next, cur, cur)[0].prev

print(res)

n, m, k = map(int, input().split())
s = list(map(int, input().split()))
d = 0
for i in range(n):
    for a in list(map(int, input().split())):
        for j in range(k):
            if s[j] == a:
                del(s[j])
                s = [a] + s
                d += j + 1
print(d)

# -*- coding: utf-8 -*-
"""
Created on Wed Apr 20 21:02:31 2016

@author: Kirill
"""

n, m, k = map(int, input().split())
order = list(map(int, input().split()))
items = []
for i in range(n):
    items.append(list(map(int, input().split())))

ans = 0
for i in range(n):
    for j in range(m):
        item = order.index(items[i][j])
        ans += item + 1
        order.insert(0, order.pop(item))
print(ans)

def main():
    n, m, k = list(map(int, input().split()))
    aa, t = list(map(int, input().split())), n * m * k
    aa.reverse()
    for _ in range(n):
        for a in map(int, input().split()):
            i = aa.index(a)
            t -= i
            del aa[i]
            aa.append(a)
    print(t)


def __starting_point():
    main()

__starting_point()

def main():
    n, m, k = list(map(int, input().split()))
    aa, t = list(map(int, input().split())), n * m * k
    aa.reverse()
    u, v, w = aa.index, aa.remove, aa.append
    for _ in range(n):
        for a in map(int, input().split()):
            t -= u(a)
            v(a)
            w(a)
    print(t)


def __starting_point():
    main()

__starting_point()

n, m, k = map(int, input().split())
p = list(map(int, input().split()))

r = 0

#p = {key: p.index(key) + 1 for key in p}

for i in range(0, n):
    a = list(map(int, input().split()))
    for j in range(0, len(a)):
        index = p.index(a[j])
        r += index + 1
        p.insert(0, p.pop(index))

print(r)

[n,m,k]=[int(i) for i in input().split()]
a=[0]*(k+1)
temp=[int(i) for i in input().split()]
for i in range(0,k):
    a[temp[i]]=i+1
answer=0
for z in range(0,n):
    temp=[int(i) for i in input().split()]
    for i in temp:
        answer+=a[i]
        for l in range(1,k+1):
            if a[l]<a[i]: a[l]+=1
        a[i]=1
        
print(answer)

user,item,total=list(map(int,input().split()))
items=list(map(int,input().split()))
to=0
for i in range(user):
    a=list(map(int,input().split()))
    for j in a:
        to+=items.index(j)+1
        items.pop(items.index(j))
        items=[j]+items
print(to)


l1 = list(map(int, input().split()))
n = l1[0]
m = l1[1]
k = l1[2]
a = list(map(int, input().split()))
tot = 0
for i in range(n):
	orders = list(map(int, input().split()))
	for i in range(m):
		b = []
		val = orders[i]
		pos = a.index(val)
		tot = tot + pos + 1
		a.pop(pos)
		b.append(val)
		b = b + a
		a = b
print(tot)