#C13798. Minimum Grid Route Calculation

    ID: 43375 Type: Default 1000ms 256MiB

Minimum Grid Route Calculation

Minimum Grid Route Calculation

Given an ( m \times n ) rectangular grid, you are required to determine the minimum number of steps needed to travel from a starting point to a destination. Movement is restricted to the four cardinal directions (up, down, left, right). The coordinates are zero-indexed, meaning that a valid point ( (x, y) ) satisfies (0 \le x < m) and (0 \le y < n). If the destination cannot be reached or if any of the coordinates are out of bounds, output (-1).

inputFormat

A single line containing six integers: ( m ), ( n ), ( start_x ), ( start_y ), ( end_x ), ( end_y ), where ( m ) and ( n ) denote the grid dimensions, and ( (start_x, start_y) ) and ( (end_x, end_y) ) represent the starting and ending coordinates respectively.

outputFormat

A single integer representing the minimum number of steps required to reach the destination from the start, or (-1) if the destination is unreachable.## sample

4 4 0 0 0 0
0