You are given a matrix containing rows and columns and an integer .
Initially, all cells are assigned some value less or equal to . is the value of the row and column.
Each second all cell's value is increased by but it can increase maximum up to after that value of is unchanged.
On the second, you are at cell and want to go to cell.
At any point in time, you can jump to any adjacent cell. If you are at , then you can go to any of the adjacent cells, , , , and . You can move to the adjacent cells only on one condition :
You can move to any adjacent cell if and only if the value of the cell, where you are standing, is equal to the value of the adjacent cell and you can not go outside of the matrix
Note: Jump time is negligible
Your task is to determine the minimum time to reach from the cell.
Input format
Output format
Print single integer minimum time to reach cell.
Constraints
after one second matrix became
3 | 3 |
2 | 3 |
now there is a path from (1, 1) to (2, 2).