from scipy.sparse import*

*a,=list(map(int,open(0).read().split()))

c=csgraph.dijkstra(csr_matrix([a[i:i+10]for i in range(2,99,10)]))

print((int(sum(c[i][1]for i in a[102:]if i>=0))))
from scipy.sparse import*

_,*a=[list(map(int,t.split()))for t in open(0)]

c=csgraph.dijkstra(csr_matrix(a[:10]))

print((int(sum(c[j][1]for i in a[10:]for j in i if j>=0))))
from scipy.sparse import *

(*a,) = list(map(int, open(0).read().split()))
c = csgraph.dijkstra(csr_matrix([a[i : i + 10] for i in range(2, 99, 10)]))
print((int(sum(c[i][1] for i in a[102:] if i >= 0))))

from scipy.sparse import *

_, *a = [list(map(int, t.split())) for t in open(0)]
c = csgraph.dijkstra(csr_matrix(a[:10]))
print((int(sum(c[j][1] for i in a[10:] for j in i if j >= 0))))

-(*a,) = list(map(int, open(0).read().split())) -c = csgraph.dijkstra(csr_matrix([a[i : i + 10] for i in range(2, 99, 10)])) -print((int(sum(c[i][1] for i in a[102:] if i >= 0)))) +_, *a = [list(map(int, t.split())) for t in open(0)] +c = csgraph.dijkstra(csr_matrix(a[:10])) +print((int(sum(c[j][1] for i in a[10:] for j in i if j >= 0))))