import heapq

from collections import defaultdict

v_num = int(eval(input()))

edges = defaultdict(list)

for i in range(v_num):

    data = [int(n) for n in input().split(" ")[1:]]

    for j in range(i + 1, v_num):

        if data[j] != -1:

            edges[i].append([data[j], j])

            edges[j].append([data[j], i])

queue = []

heapq.heapify(queue)

for c in edges[0]:

    heapq.heappush(queue, c)

visited = {0}

answer = 0

while len(visited) < v_num:

    q = heapq.heappop(queue)

    weight, one = q[0], q[1]

    if one in visited:

        continue

    answer += weight

    for e in edges[one]:

        heapq.heappush(queue, e)

    visited |= {one}

print(answer)
import heapq

from collections import defaultdict

v_num = int(eval(input()))

edges = defaultdict(list)

for i in range(v_num):

    data = [int(n) for n in input().split(" ")[1:]]

    for j in range(i + 1, v_num):

        if data[j] != -1:

            edges[i].append([data[j], j])

            edges[j].append([data[j], i])

queue = []

heapq.heapify(queue)

for c in edges[0]:

    heapq.heappush(queue, c)

visited = {0}

answer = 0

while len(visited) < v_num:

    q = heapq.heappop(queue)

    weight, one = q[0], q[1]

    if one in visited:

        continue

    answer += weight

    for e in edges[one]:

        if e[1] not in visited:

            heapq.heappush(queue, e)

    visited |= {one}

print(answer)
import heapq
from collections import defaultdict

v_num = int(eval(input()))
edges = defaultdict(list)
for i in range(v_num):
    data = [int(n) for n in input().split(" ")[1:]]
    for j in range(i + 1, v_num):
        if data[j] != -1:
            edges[i].append([data[j], j])
            edges[j].append([data[j], i])
queue = []
heapq.heapify(queue)
for c in edges[0]:
    heapq.heappush(queue, c)
visited = {0}
answer = 0
while len(visited) < v_num:
    q = heapq.heappop(queue)
    weight, one = q[0], q[1]
    if one in visited:
        continue
    answer += weight
    for e in edges[one]:
        heapq.heappush(queue, e)
    visited |= {one}
print(answer)

import heapq
from collections import defaultdict

v_num = int(eval(input()))
edges = defaultdict(list)
for i in range(v_num):
    data = [int(n) for n in input().split(" ")[1:]]
    for j in range(i + 1, v_num):
        if data[j] != -1:
            edges[i].append([data[j], j])
            edges[j].append([data[j], i])
queue = []
heapq.heapify(queue)
for c in edges[0]:
    heapq.heappush(queue, c)
visited = {0}
answer = 0
while len(visited) < v_num:
    q = heapq.heappop(queue)
    weight, one = q[0], q[1]
    if one in visited:
        continue
    answer += weight
    for e in edges[one]:
        if e[1] not in visited:
            heapq.heappush(queue, e)
    visited |= {one}
print(answer)

-        heapq.heappush(queue, e) +        if e[1] not in visited: +            heapq.heappush(queue, e)