Dima overslept alarm clock suppose raise schoolDima wonder time come lesson to need know minimum time home schoolthe city Dima live rectangular field n time m size each cell j field denote number a_ij the number 1 mean passage cell prohibit the number 0 mean cell free Dima walk the number x 1 le x le 109 mean cell contain portal cost x a cell portal consider free from portal Dima portal time move portal j portal x y correspond sum cost a_ij a_xyin addition move portal Dima unoccupied cell adjacent time w in particular enter cell portal use itinitially Dima upperleft cell 1 1 school low right cell n m