Giyun likes a game about escaping from the army. To finish the game he has to cross the barracks and get out. For the sake of discipline the barracks is always a rectangle of height n and width m.
Giyun starts on block (0,0) in the top left corner and has to reach block (n−1,m−1) in the bottom right corner, moving only up, down, left, and right. He may never leave the barracks while moving, and he must step on block (0,0) and on block (n−1,m−1).
Every block has a level requirement. If the number written on a block is 3, Giyun needs level 3 or higher to step on that block.
Giyun can use the air force's special equipment once per game. The equipment carries him from the block he is standing on over one block in a single direction, and he lands on the block two steps away in that direction. He does not step on the block he passes over, so its level requirement does not apply. Two conditions hold.
- He cannot change direction in the middle of the jump.
- The block he lands on has to be inside the barracks.
Find the lowest level Giyun needs in order to escape the barracks.