import sys



def debug(x, table):

    for name, val in table.items():

        if x is val:

            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)

            return None



def solve():

    n = int(input())

    a = []



    for i in range(n):

        a.append([int(j) for j in input().split()])



    checked = [False] * n

    tree = [0]

    checked[0] = True

    ans = 0



    for i in range(n - 1):

        min_w = float('inf')



        for u in tree:

            for j in range(n):

                if a[u][j] != -1 and not checked[j]:

                    if min_w > a[u][j]:

                        min_w = a[u][j]

                        min_n = j



        checked[min_n] = True

        tree.append(min_n)

        ans += min_w



    print(ans)







if __name__ == '__main__':

    solve()

import sys



def debug(x, table):

    for name, val in table.items():

        if x is val:

            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)

            return None



def solve():

    inf = float('inf')

    n = int(input())

    a = []



    for i in range(n):

        line = [int(i) for i in input().split()]

        line = [i if i != -1 else inf for i in line]

        a.append(line)



    

    checked = [False] * n

    d = [inf] * n

    checked[0] = True

    ans = 0

    nxt = 0



    for i in range(n - 1):

        for j in range(n):

            if a[nxt][j] < d[j]:

                d[j] = a[nxt][j]



        min_w = inf

        min_n = 0



        for j in range(n):

            if not checked[j] and d[j] < min_w:

                min_n = j

                min_w = d[j]



        checked[min_n] = True

        nxt = min_n

        ans += min_w



    # debug(d, locals())

    print(ans)







if __name__ == '__main__':

    solve()

import sys


def debug(x, table):
    for name, val in table.items():
        if x is val:
            print("DEBUG:{} -> {}".format(name, val), file=sys.stderr)
            return None


def solve():
    n = int(input())
    a = []
    for i in range(n):
        a.append([int(j) for j in input().split()])
    checked = [False] * n
    tree = [0]
    checked[0] = True
    ans = 0
    for i in range(n - 1):
        min_w = float("inf")
        for u in tree:
            for j in range(n):
                if a[u][j] != -1 and not checked[j]:
                    if min_w > a[u][j]:
                        min_w = a[u][j]
                        min_n = j
        checked[min_n] = True
        tree.append(min_n)
        ans += min_w
    print(ans)


if __name__ == "__main__":
    solve()

import sys


def debug(x, table):
    for name, val in table.items():
        if x is val:
            print("DEBUG:{} -> {}".format(name, val), file=sys.stderr)
            return None


def solve():
    inf = float("inf")
    n = int(input())
    a = []
    for i in range(n):
        line = [int(i) for i in input().split()]
        line = [i if i != -1 else inf for i in line]
        a.append(line)
    checked = [False] * n
    d = [inf] * n
    checked[0] = True
    ans = 0
    nxt = 0
    for i in range(n - 1):
        for j in range(n):
            if a[nxt][j] < d[j]:
                d[j] = a[nxt][j]
        min_w = inf
        min_n = 0
        for j in range(n):
            if not checked[j] and d[j] < min_w:
                min_n = j
                min_w = d[j]
        checked[min_n] = True
        nxt = min_n
        ans += min_w
    # debug(d, locals())
    print(ans)


if __name__ == "__main__":
    solve()

+    inf = float("inf") -        a.append([int(j) for j in input().split()]) +        line = [int(i) for i in input().split()] +        line = [i if i != -1 else inf for i in line] +        a.append(line) -    tree = [0] +    d = [inf] * n +    nxt = 0 -        min_w = float("inf") -        for u in tree: -            for j in range(n): -                if a[u][j] != -1 and not checked[j]: -                    if min_w > a[u][j]: -                        min_w = a[u][j] -                        min_n = j +        for j in range(n): +            if a[nxt][j] < d[j]: +                d[j] = a[nxt][j] +        min_w = inf +        min_n = 0 +        for j in range(n): +            if not checked[j] and d[j] < min_w: +                min_n = j +                min_w = d[j] -        tree.append(min_n) +        nxt = min_n +    # debug(d, locals())