# Haskellwhores sosnooley.
# No need to handle special cases like 'Nothing'.
inf = float('inf')


def yoba(map, start, end):
    # Haskellwhores sosnooley again.
    # 'bounds'? Hah.
    w = len(map)
    h = w and len(map[0])
    # And again.  This lambda was borrowed from the Reifag's code.
    value = lambda x, y: 0    if y == start \
                    else inf  if not x      \
                    else min(arr[x - 1][n] + map[n][y] for n in range(h))

    # Pythonwhores sosnooley.
    # Much more readable variant:
    #    arr = [[value(x, y) for y in range(h)] for x in range(w)]
    # Unfortunately, it does not work because value() tries to access
    # the arr list.
    arr = []
    for x in range(w): arr.append([value(x, y) for y in range(h)])
    return min(arr[x][end] for x in range(w))

rank = 10 ** 10000

if __name__ == '__main__':
    # Haskellwhores sosnooley again.
    toArray2D = lambda arr: list(zip(*arr))
    graph = [[inf,   7 * rank,   9 * rank, inf, inf,  14 * rank],
             [  7 * rank, inf,  10 * rank,  15 * rank, inf, inf],
             [  9 * rank,  10 * rank, inf,  11 * rank, inf,   2 * rank],
             [inf,  15 * rank,  11 * rank, inf,   6 * rank, inf],
             [inf, inf, inf,   6 * rank, inf,   9 * rank],
             [ 14 * rank, inf,   2 * rank, inf,   9 * rank, inf]]
    print(yoba(toArray2D(graph), 0, 4))
