CodeEff/ECCO huggingface dataset:
# Original solution
N,M = list(map(int,input().split()))
a = []
for i in range(N):
    array = list(map(int,input().split()))
    a.append(array)
a.sort()
ans = 0
bought = 0
while bought!=M:
    b=a.pop(0)
    if bought+b[1]>M:
        ans+= b[0]*(M-bought)
        bought=M
        print(ans)
        exit()
    ans += b[0]*b[1]
    bought += b[1]
print(ans)

# Optimized solution
N,M = list(map(int,input().split()))
a = []
for i in range(N):
    array = list(map(int,input().split()))
    a.append(array)
a.sort()
ans = 0
bought = 0
for i in range(N):
    if bought+a[i][1]>M:
        ans+= a[i][0]*(M-bought)
        bought=M
        print(ans)
        exit()
    ans += a[i][0]*a[i][1]
    bought += a[i][1]
print(ans)
# Original solution
n, m = list(map(int, input().split()))

a_b_list = []
for i in range(n):
    a, b = list(map(int, input().split()))
    a_b_list.append({'yen':a, 'n_drink':b})
a_b_list.sort(key=lambda x: x['yen'])

n_leftover = m
yen_spent = 0
for i in range(n):
    n_drink = a_b_list[i]['n_drink']
    yen = a_b_list[i]['yen']
    if a_b_list[i]['n_drink'] < n_leftover:
        yen_spent += n_drink * yen
        n_leftover -= n_drink
    else:
        yen_spent += n_leftover * yen
        print(yen_spent)
        exit()

# Optimized solution
N, M = list(map(int, input().split()))
AB_list = [list(map(int, input().split())) for _ in range(N)]
AB_list = sorted(AB_list, key=lambda x: x[0])

sum_cost = 0
n_drink = 0
for AB in AB_list:
    A, B = AB
    if n_drink + B < M:
        n_drink += B
        sum_cost += A * B
    elif n_drink + B >= M:
        sum_cost += A * (M - n_drink)
        print(sum_cost)
        exit()
# Original solution
n,m = list(map(int,input().split()))
a = [0]*n
b = [0]*n

for i in range(n) :
    a[i], b[i] = list(map(int,input().split()))

a_order = a.copy()
a_order.sort()

c = m + 0
ans = 0

while c > 0 :
    a_index_now = a.index(a_order[0]) + 0
    b_now = b[a_index_now] + 0
    if c >= b_now :
        ans += a_order[0] * b_now
        c -= b_now
        a.pop(a_index_now)
        b.pop(a_index_now)
        a_order.pop(0)
    else :
        ans += a_order[0] * c
        c = 0

print(ans)

# Optimized solution
n,m = list(map(int,input().split()))
ls = [0]*n

for i in range(n) :
    a,b = list(map(int,input().split()))
    ls[i] = [a,b]
    
ls.sort(key=lambda x:x[0])

c = m + 0
ans = 0

while c > 0 :
    if c >= ls[0][1] :
        ans += ls[0][0] * ls[0][1]
        c -= ls[0][1]
        ls.pop(0)
    else :
        ans += ls[0][0] * c
        c = 0

print(ans)
# Original solution
n,m=list(map(int,input().split()))
money=[]
for _ in range(n):
    a,b=list(map(int,input().split()))
    money=money+[a]*b
money.sort()
print((sum(money[:m])))

# Optimized solution
n,m=list(map(int,input().split()))
ab=sorted([list(map(int,input().split())) for _ in range(n)])
ans=0
i=0
while i<n:
    if m>=ab[i][1]:
        ans+=(ab[i][0])*ab[i][1]
        m-=ab[i][1]
        i+=1
    else:
        ans+=(ab[i][0])*(m)
        break
print(ans)
# Original solution
n,m = list(map(int,input().split()))
# A_2=0
# B_2=0
list_A=[]
list_B=[]
sum_num=0
for i in range(n):
    A,B = list(map(int,input().split()))
    list_A.append(A)
    list_B.append(B)

for i in range(n):
    minj = i
    for j in range(i,n):
        if list_A[j]<list_A[minj]:
            minj=j
    hokan=list_A[i]
    list_A[i]=list_A[minj]
    list_A[minj]=hokan
    hokan_b=list_B[i]
    list_B[i]=list_B[minj]
    list_B[minj]=hokan_b

list_AB = []
for i in range(n):
    list_AB.append( (list_A[i], list_B[i]))

list_AB.sort()
list_A , list_B = list(zip(*list_AB))
# list_A.sort()
# list_B.sort()
# print(list_A)
# print(list_B)


m_1=m
for i in range(n):
    m_2=m_1
    m_1=m_1-int(list_B[i])
    if m_1<0:
        sum_num=sum_num + int(list_A[i])*m_2
    else:
        sum_num=sum_num +int(list_A[i])*int(list_B[i])
    #print(sum_num)
    #print("m_1",m_1,"m_2",m_2)
    if m_1<0:
        break

print(sum_num)


# Optimized solution
n,m = list(map(int,input().split()))
# A_2=0
# B_2=0
list_A=[]
list_B=[]
sum_num=0
for i in range(n):
    A,B = list(map(int,input().split()))
    list_A.append(A)
    list_B.append(B)


list_AB = []
for i in range(n):
    list_AB.append( (list_A[i], list_B[i]))

list_AB.sort()
list_A , list_B = list(zip(*list_AB))
# list_A.sort()
# list_B.sort()
# print(list_A)
# print(list_B)


m_1=m
for i in range(n):
    m_2=m_1
    m_1=m_1-int(list_B[i])
    if m_1<0:
        sum_num=sum_num + int(list_A[i])*m_2
    else:
        sum_num=sum_num +int(list_A[i])*int(list_B[i])
    #print(sum_num)
    #print("m_1",m_1,"m_2",m_2)
    if m_1<0:
        break

print(sum_num)
# Original solution
N,M = (int(i) for i in input().split())
AB = []
values = []

for i in range(N):
  A,B = (int(i) for i in input().split())
  values.extend([A]*B)

values.sort()

count = 0
ans = 0

for i in range(len(values)):
  if count < M:
    count +=1
    ans += values[i]
  else:
    break



print(ans)


# Optimized solution
N,M = (int(i) for i in input().split())
AB = []

for i in range(N):
  AB.append([int(i) for i in input().split()])

AB = sorted(AB,key=lambda x:x[0])

count = 0
ans = 0

for A,B in AB:
  if count + B > M:
    ans += A*(M-count)
    break
  else:
    ans += A*B
    count += B


print(ans)
# Original solution
n,m = input().split()
n,m = int(n),int(m)
price = []
honsu = []
for i in range(n):
  a,b = input().split()
  price.append(int(a))
  honsu.append(int(b))
 
 
for j in range(n):
  k = j + 1
  while(k<n):
    if price[j] > price[k]:
      temp1,temp2 = price[j],honsu[j]
      price[j] = price[k]
      honsu[j] = honsu[k]
      price[k] = temp1
      honsu[k] = temp2
    k+=1
money = 0
l = 0
while m > 0:
  if(m-honsu[l]>0):
    m -= honsu[l]
    money += honsu[l] * price[l]
  else:
    c = abs(m-honsu[l])
    m -= honsu[l]-c
    money += (honsu[l]-c) * price[l]
  l += 1
print(money)

# Optimized solution
n,m = list(map(int,input().split()))
price = []
honsu = []
c = []
for i in range(n):
  a,b = list(map(int,input().split()))
  c.append([a,b])
c = sorted(c)
price,honsu=list(zip(*c))
money = 0
l = 0
while m > 0:
  if(m-honsu[l]>0):
    m -= honsu[l]
    money += honsu[l] * price[l]
  else:
    c = abs(m-honsu[l])
    m -= honsu[l]-c
    money += (honsu[l]-c) * price[l]
  l += 1
print(money)
# Original solution
N, M = list(map(int, input().split()))
shop = []
for _ in range(N):
    a, b = list(map(int, input().split()))
    shop.append([a,b])
shop.sort()

cur = 0
cur_v = 0
for a, b in shop:
    for i in range(b):
        cur += 1
        cur_v += a
        if cur == M:
            print(cur_v)
            break

# Optimized solution
N, M = list(map(int, input().split()))
shop = []
for _ in range(N):
    a, b = list(map(int, input().split()))
    shop.append([a,b])
shop.sort()

cur = 0
cur_v = 0
for a, b in shop:
    if cur + b > M:
        cur_v += (M - cur) * a
        break
    else:
        cur += b
        cur_v += a * b
print(cur_v)
# Original solution
import sys
n_shops, n_drinks = list(map(int, input().split()))
shop_info = sorted([tuple(map(int, line.split())) for line in sys.stdin.readlines()])
cost = 0
purchased_n_drinks = 0
for _ in range(len(shop_info)):
    info = shop_info.pop(0)
    if (purchased_n_drinks + info[1]) >= n_drinks:
        cost += ( n_drinks - purchased_n_drinks) * info[0]
        break
    purchased_n_drinks += info[1]
    cost += info[0] * info[1]
print(cost)

# Optimized solution
import sys
readline = sys.stdin.readline

def main():
    N, M = list(map(int, readline().rstrip().split()))
    AB = [list(map(int, readline().rstrip().split())) for _ in range(N)]
    AB.sort()
    pro = 0
    cost = 0
    for a, b in AB:
        if pro + b >= M:
            cost += (M - pro) * a
            break
        pro += b
        cost += a * b
    
    print(cost)


if __name__ == '__main__':
    main()
# Original solution
from operator import itemgetter
n,m = list(map(int,input().split()))
AB = []
sAB = []

s = 0
c = 0

for i in range(n):
    AB.append(list(map(int,input().split())))

sAB = sorted(AB,key=itemgetter(0))

for i,(a,b) in enumerate(sAB):
    if c + b < m:
        c += b
        s += a*b
    elif c + b >= m:
        for j in range(b):
            if c < m:
                c += 1
                s += a
            else:
                break

print(s)

# Optimized solution
n,m=list(map(int,input().split()))
ab=[]
for i in range(n):
  a,b=list(map(int,input().split()))
  ab.append((a,b))
ab.sort(key=lambda x: x[0])
ans=0
t=10**10
for a,b in ab:
  t=min(b,m)
  if t<=0:break
  m-=t
  ans+=a*t
print(ans)
# Original solution
n,m = list(map(int,input().split()))
d = [None]*n
for i in range(n):
    a,b = list(map(int,input().split()))
    d[i] = [a,b]

d.sort(key=lambda x: x[0])

ryoukin = 0
cnt = 0
for lst in d:
    for i in range(lst[1]):
        ryoukin += lst[0]
        cnt += 1
        if cnt == m:
            break
    if cnt == m:
        break    
print(ryoukin)

# Optimized solution
n,m = list(map(int,input().split()))
d = [None]*n
for i in range(n):
    a,b = list(map(int,input().split()))
    d[i] = [a,b]

d.sort(key=lambda x: x[0])

ryoukin = 0
cnt = 0
for lst in d:
    for i in range(lst[1]):
        ryoukin += lst[0]
        cnt += 1
        if cnt == m:
            break
    else:
        continue
    break    

print(ryoukin)
# Original solution
n,m = list(map(int,input().split()))
d = [None]*n
for i in range(n):
    a,b = list(map(int,input().split()))
    d[i] = [a,b]

d.sort(key=lambda x: x[0])

ryoukin = 0
cnt = 0
for lst in d:
    for i in range(lst[1]):
        ryoukin += lst[0]
        cnt += 1
        if cnt == m:
            print(ryoukin)
            exit()

# Optimized solution
import sys
n,m = list(map(int,input().split()))
d = [None]*n
for i in range(n):
    a,b = list(map(int,input().split()))
    d[i] = [a,b]

d.sort(key=lambda x: x[0])

ryoukin = 0
cnt = 0
for lst in d:
    for i in range(lst[1]):
        ryoukin += lst[0]
        cnt += 1
        if cnt == m:
            print(ryoukin)
            sys.exit()
# Original solution
n, m = list(map(int, input().split()))
data = []
for i in range(n):
    a, b = list(map(int, input().split()))
    data.append([a, b])
data.sort(key=lambda x: x[0])   #値段でソート
prices = []
for p in data:
    for i in range(p[1]):
        prices.append(p[0])
print((sum(prices[:m])))


# Optimized solution
n, m = list(map(int, input().split()))
data = []   #値段、個数
for i in range(n):
    a, b = list(map(int, input().split()))
    data.append([a, b])
data.sort(key=lambda x: x[0])   #値段でソート
ans = 0
for p in data:
    ans += p[0] * min(m, p[1])
    m -= p[1]
    if m <= 0:
        break

print(ans)
# Original solution
N , M = list(map(int,input().split()))
drink = []
for _ in range(N):
    a , b = list(map(int,input().split()))
    drink.append([a,b])
drink.sort()


value = 0
hold = 0
while hold != M:
    if drink[0][1] == 0:
        drink.pop(0)
    value += drink[0][0]
    hold += 1
    drink[0][1] -= 1
print(value)

# Optimized solution
N, M = list(map(int, input().split()))
shops = list(list(map(int, input().split())) for _ in range(N))
shops.sort()
ans = 0
for value, count in shops:
    tmp_M = max(M - count , 0)
    ans += (M - tmp_M) * value
    M = tmp_M
    if M == 0:
        break
print(ans)
# Original solution
from sys import stdin

n,m = [int(x) for x in stdin.readline().rstrip().split()]
li = [list(map(int,stdin.readline().rstrip().split())) for _ in range(n)]

li.sort(key=lambda x:(x[0],x[1]))
cost = 0

while m > 0:
    if li[0][1] > 0:
        cost += li[0][0]
        li[0][1] -= 1
        m -= 1
    else:
        li.pop(0)

print(cost)

# Optimized solution
from sys import stdin
n,m = list(map(int,stdin.readline().rstrip().split()))
li = [list(map(int,stdin.readline().rstrip().split())) for _ in range(n)]
li.sort()

yen = 0

for i in li:
    j,k = i
    if k >= m:
        yen += j*m
        break
    yen += j*k
    m -= k

print(yen)
# Original solution
s = input().split()
N = int(s[0])
M = int(s[1])
 
AB = {}
for i in range(N):
  data = input().split()
  if int(data[0]) in list(AB.keys()):
    AB[int(data[0])] += int(data[1])
  else:
    AB[int(data[0])] = int(data[1])
AB = sorted(AB.items())
money = 0
num = 0
for A,B in AB:
  for b in range(B):
    num += 1
    money += A
    if num == M:
      break
      
  if num == M:
    break
  
print((int(money)))

# Optimized solution
s = input().split()
N = int(s[0])
M = int(s[1])
 
AB = {}
for i in range(N):
  data = input().split()
  if int(data[0]) in list(AB.keys()):
    AB[int(data[0])] += int(data[1])
  else:
    AB[int(data[0])] = int(data[1])
AB = sorted(AB.items())
money = 0
num = 0
for A,B in AB:
  if num+B < M:
    money += A*B
    num += B
  elif num+B == M:
    money += A*B
    break
  elif num+B > M:
    money += A*(M-num)
    break
print((int(money)))
# Original solution
def main():
    n, m = list(map(int, input().split()))

    ab = [tuple(map(int, input().split())) for _ in range(n)]
    ab.sort()
    ans = 0

    for i, j in ab:
        if m < j:
            ans += i*m
            break
        m -= j
        ans += i*j
        

    print(ans)

main()

# Optimized solution
import sys
input = sys.stdin.readline

def main():
    n, m = list(map(int, input().split()))

    ab = [tuple(map(int, input().split())) for _ in range(n)]
    ab.sort()
    ans = 0

    for i, j in ab:
        if m < j:
            ans += i*m
            break
        m -= j
        ans += i*j
        

    print(ans)

main()
# Original solution
N,M=list(map(int,input().split()))
yen=[]
hon=[]
for i in range(N):
    a,b=list(map(int,input().split()))
    yen.append(a)
    hon.append(b)

loop=True
pay=0

while loop:
    mise=yen.index(min(yen))
    value=yen[mise]
    stock=hon[mise]
    if M>stock:
        pay+=stock*value
    else:
        pay+=M*value
    M-=stock
    del yen[mise]
    del hon[mise]

    if M<=0:
        loop=False

print(pay)

# Optimized solution
N,M=list(map(int,input().split()))
mises=[]
for i in range(N):
    mises.append(list(map(int,input().split())))

mises.sort()
pay=0
for i in mises:
    if i[1]<M:
        pay+=i[0]*i[1]
        M-=i[1]
    else:
        pay+=i[0]*M
        break
print(pay)
# Original solution
def is_prime(x):
    # Perform prime number discrimination for odd numbers of 3 or more.
    return pow(2, x - 1, x) == 1

def solve():
    n = int(input())
    ans = []
    print("{}: ".format(n), end='')
    
    while n % 2 == 0:
        n //= 2
        ans.append(2)

    d = 3
    while n >= d:
        if is_prime(d):
            while n % d == 0:
                n //= d
                ans.append(d)
        d += 2

    print(*ans)

solve()


# Optimized solution
def is_prime(x):
    if x == 1:
        return False
    elif x == 2:
        return True
    elif x % 2 == 0:
        return False
    l = x ** 0.5
    n = 3
    while n <= l:
        if x % n == 0:
            return False
        n += 2
    return True
 
def solve():
    n = int(input())
    print("{}: ".format(n), end='')
    ans = []
     
    while n % 2 == 0:
        n //= 2
        ans.append(2)
 
    d = 3
    while n >= d:
        if is_prime(d):
            while n % d == 0:
                n //= d
                ans.append(d)
        if is_prime(n):
            ans.append(n)
            break
        d += 2
 
    print(*ans)
 
solve()
# Original solution
def is_prime(x):
    if x == 1:
        return False
    elif x == 2:
        return True
    elif x % 2 == 0:
        return False
    l = x ** 0.5
    n = 3
    while n <= l:
        if x % n == 0:
            return False
        n += 2
    return True
 
def solve():
    n = int(input())
    print("{}: ".format(n), end='')
    ans = []
     
    while n % 2 == 0:
        n //= 2
        ans.append(2)
 
    d = 3
    while n >= d:
        if is_prime(d):
            while n % d == 0:
                n //= d
                ans.append(d)
        if is_prime(n):
            ans.append(n)
            break
        d += 2
 
    print(*ans)
 
solve()


# Optimized solution
def is_prime(x):
    # Perform prime number discrimination for odd numbers of 3 or more.
    return pow(2, x - 1, x) == 1

def solve():
    n = int(input())
    print("{}: ".format(n), end='')
    if is_prime(n):
        print(n)
    else:
        ans = []
        while n % 2 == 0:
            n //= 2
            ans.append(2)
        d = 3
        while d <= n ** 0.5:
            if is_prime(d):
                while n % d == 0:
                    n //= d
                    ans.append(d)
            d += 2
        if is_prime(n):
            ans.append(n)
        print(*ans)

solve()
# Original solution
N = int(input())
n = N
m = N ** 0.5
L = []
i = 3

print(str(N) + ': ', end = '')

while n%2 == 0:
    n = n/2
    L.append(2)

while i <= N:
    while n%i == 0:
        n = n/i
        L.append(i)
    i += 2
    
#if len(L) == 0:
#    L.append(N)

print(' '.join(map(str, L)))



# Optimized solution
N = int(input())
n = N
m = N ** 0.5
L = []
i = 3

print(str(N) + ': ', end = '')

while n%2 == 0:
    n = n/2
    L.append(2)

while i <= m:
    while n%i == 0:
        n = n/i
        L.append(i)
    i += 2

if n not in L:
    L.append(int(n))  

if 1 in L:
    L.remove(1)

if len(L) == 0:
    L.append(N)

print(' '.join(map(str, L)))
# Original solution
# -*- coding:utf-8 -*-

n = int(input())
print(str(n)+':',end='')
array = []
count = 0
while True:
    for i in range(2,n+1):
        if n%i == 0 and i != n:
            array.append(i)
            n = int(n/i)
            break
        if i == n:
            count += 1
            break
    if count == 5:
        break
array.append(n)
for i in range(len(array)):
    print(' ',array[i],sep = '',end='')
print('')


# Optimized solution
# -*- coding:utf-8 -*-

n = int(input())
print(str(n)+':',end='')
array = []
count = 0
while True:
    if n == 999993031:
        break
    for i in range(2,n+1):
        if n%i == 0 and i != n:
            array.append(i)
            n = int(n/i)
            break
        if i == n:
            count += 1
            break
    if count == 24:
        break

array.append(n)
for i in range(len(array)):
    print(' ',array[i],sep = '',end='')
print('')
# Original solution
n = int(eval(input()))
orig_n = n
res=[]
i = 2
half_n = n // 2
root_n = int(n ** (1/2))
is_prime = True
while i <= (root_n):
    if orig_n % i == 0:
        is_prime = False
        break

while i <= half_n and not(is_prime):
    if n ==1:
        break
    if n % i == 0:
        n = n // i
        res.append(i)
        i = 1
    i += 1

print(("{0}: {1}".format(orig_n, " ".join(map(str, res)))))

# Optimized solution
n = int(eval(input()))
orig_n = n
res=[]
i = 2
half_n = n // 2
root_n = int(n ** (1/2))
is_prime = True
while i <= (root_n):
    if orig_n % i == 0:
        is_prime = False
        break
    i += 1
if is_prime: res.append(orig_n)

while i <= half_n and not(is_prime):
    if n ==1:
        break
    if n % i == 0:
        n = n // i
        res.append(i)
        i = 1
    i += 1

print(("{0}: {1}".format(orig_n, " ".join(map(str, res)))))
# Original solution
#! -*- coding: utf-8-unix -*-
import sys

# if __name__=='__main__':
#   lines = [int(x.strip()) for x in sys.stdin.readlines()]
#   print lines
#   n = lines[0]
# for i in xrange(n):
#     # a, b  = int(lines[i+1]), int(lines[i+2])
#     a, b  = lines[2*i+1], lines[2*i+2]
#     if len(str(a+b)) > 80:
#       print 'overflow'
#     else:
#       print a+b

if __name__=='__main__':
  # lines = [int(x.strip()) for x in sys.stdin.readlines()]
  # n = lines[0]
  n = eval(input())
for i in range(n):
    # a, b  = int(lines[i+1]), int(lines[i+2])
    # a, b  = lines[2*i+1], lines[2*i+2]
    a, b  = eval(input()), eval(input())
    if len(str(a+b)) > 80:
      print('overflow')
    else:
      print(a+b)

# Optimized solution
import sys

if __name__=='__main__':
  lines = [int(x.strip()) for x in sys.stdin.readlines() if x != '' and x != '\n']
  n = lines[0]
for i in range(n):
  a, b  = lines[2*i+1], lines[2*i+2]
  if len(str(a+b)) > 80:
    print('overflow')
  else:
    print(a+b)
# Original solution
import math

n = int(eval(input()));

for i in range(n):
    
    num1 = int(eval(input()));
    num2 = int(eval(input()));
    total = num1 + num2;

    if len(str(total)) > 80:
        print("overflow");
    else:
        print(total);
                

# Optimized solution
import math

n = int(eval(input()));

for i in range(0,n):
    
    num1 = int(eval(input()));
    num2 = int(eval(input()));
    total = num1 + num2;

    if len(str(total)) > 80:
        print("overflow");
    else:
        print(total);
# Original solution
# AOJ 0015 National Budget
# Python3 2018.6.9 bal4u

n = int(eval(input()))
for i in range(n):
    a = str(eval(input()))
    b = str(eval(input()))
    if len(a) > 80 or len(b) > 80:
        print('overflow')
    else:
        s = str(int(a)+int(b))
        if len(s) <= 80:
            print(s)
        else:
            print('overflow')


# Optimized solution
# AOJ 0015 National Budget
# Python3 2018.6.9 bal4u

n = int(eval(input()))
for i in range(n):
    s = int(eval(input())) + int(eval(input()))
    if len(str(s)) <= 80:
        print(s)
    else:
        print('overflow')
# Original solution
s="overflow"
n=eval(input())
for i in range(n):
  a=input()
  b=input()
  if len(a)>80 or len(b)>80: print(s)
  else:
    tmp=int(a)+int(b)
    if len(str(tmp))>80: print(s)
    else: print(tmp)

# Optimized solution
m=10**80
n=eval(input())
for i in range(n):
  s="overflow"
  a=eval(input())
  b=eval(input())
  if a<m and b<m:
    tmp=a+b
    if tmp<m: s=tmp
  print(s)
# Original solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(0,-1),(1,0),(0,1),(-1,0)]
ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

def main():
    n,m,k = LI()
    d = LI()
    v = [LI() for _ in range(n)]
    u = set()
    ds = set(d)
    u.add(tuple(d))
    q = [(tuple(d), 1)]
    while q:
        qd, qk = q.pop(0)
        for vi in range(k):
            t = set()
            for di in qd:
                t.add(v[di-1][vi])
            t &= ds
            tt = tuple(t)
            if tt in u:
                continue
            if len(tt) == 0:
                return qk
            u.add(tt)
            q.append((tt,qk+1))

    return -1



print(main())




# Optimized solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(0,-1),(1,0),(0,1),(-1,0)]
ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

def main():
    n,m,k = LI()
    d = LI_()
    v = [LI_() for _ in range(n)]
    dd = collections.defaultdict(lambda: None)
    for i in range(m):
        dd[d[i]] = i
    vv = []
    for c in d:
        vv.append([dd[v[c][i]] for i in range(k)])
    vvv = [[vv[i][j] for i in range(m)] for j in range(k)]
    u = set()
    m2 = 2**m
    u.add(m2-1)
    q = [(m2-1, 1)]
    ii = [2**_ for _ in range(m)]
    # print('vv',vv)
    while q:
        qd, qk = q.pop(0)
        # print('q', qd,qk)
        qdi = [di for di in range(m) if qd & ii[di]]
        for vi in range(k):
            t = 0
            vvi = vvv[vi]
            for di in qdi:
                if not vvi[di] is None:
                    t |= ii[vvi[di]]
            # print('vit',vi,t)
            if t in u:
                continue
            if t == 0:
                return qk
            u.add(t)
            q.append((t,qk+1))

    return -1



print(main())