The island of Rectos is always threatened by floods and pirates. Therefore, the king of Rectos wants to protect all villages on his island by a single huge wall.
Rectos has rectangular shape. So, the architect of the wall models the island as a grid of squares. Each village is located within one of the squares; the capital village is located in the very north-west, i.e. within the upper left square.
The wall must be built in such a way that from the outside (i.e., from the area outside the grid) it is impossible to reach any village without crossing the wall.
The architect plans to build the wall along grid lines only, in the following way: He puts the first wall segment on one of the two grid line segments that start in the upper left corner.
Then, the next wall segment is always linked to the previous one, i.e. built on an adjacent grid line segment (that starts where the previous one ends). The architect continues this way, until the upper left corner is reached again. This procedure may cause that more than one wall segments are put on the same grid line segment. That is, the wall is built along a contiguous closed path of grid line segments.
Due to topography, for every grid line segment there is a certain cost of building a segment of the wall there. The total cost of building the wall is given by the sum of the costs for all segments of the wall. If $t$ wall segments are built on the same grid line segment, the cost of this grid line segment are counted $t$ times.
The king wants to spend as little money as possible for building the wall. Help the king and write a program that, given the location of villages on the island and the building cost for all grid line segments, computes the minimum cost of building the wall.