#C3466. Minimum Path Cost in a Grid

    ID: 46896 Type: Default 1000ms 256MiB

Minimum Path Cost in a Grid

Minimum Path Cost in a Grid

You are given a grid with n rows and m columns. Each cell contains an integer. You start at the top-left cell and want to reach the bottom-right cell. You can move in four directions: up, down, left and right. The cost to move from one cell to another is the absolute difference between their values, i.e., \( |grid[i][j] - grid[k][l]| \).

Your task is to compute the minimum total cost required to travel from the cell \( (0,0) \) to \( (n-1, m-1) \) using any valid path. It is guaranteed that a valid path always exists.

Note: The cost at the starting cell is considered \(0\), and only movements add to the cost.

inputFormat

The first line of input contains two integers \(n\) and \(m\) \( (1 \leq n, m \leq 1000) \), representing the number of rows and columns in the grid.

Each of the next \(n\) lines contains \(m\) integers separated by spaces, representing the grid values.

outputFormat

Output a single integer, the minimum cost to travel from the top-left to the bottom-right of the grid.

## sample
3 3
1 3 5
2 8 2
4 6 1
8