#P6007. [USACO20JAN] Springboards G
[USACO20JAN] Springboards G
Problem Description
Bessie is in a two-dimensional grid where she is only allowed to move in directions parallel to the coordinate axes. She starts at point and wants to reach (). To help her, there are () springboards in the grid. Each springboard has a fixed position , and if Bessie uses it, she will land at point .
Bessie is a process-oriented cow, so she is only allowed to walk up or right, never left or down. Similarly, each springboard is also set up so that it does not go left or down. What is the minimum distance Bessie needs to walk?
Input Format
The first line of input contains two space-separated integers and .
The next lines each contain four integers , where and .
All springboard positions and landing positions are distinct.
Output Format
Output one integer, the minimum distance Bessie needs to walk to reach point .
3 2
0 1 0 2
1 2 2 3
3
Hint
Sample Explanation
Bessie's best route is:
- Bessie walks from (0,0) to (0,1) (distance 1).
- Bessie jumps to (0,2).
- Bessie walks from (0,2) to (1,2) (distance 1).
- Bessie jumps to (2,3).
- Bessie walks from (2,3) to (3,3) (distance 1).
In total, Bessie walks a distance of 3.
Subtasks
- Test cases satisfy .
- Test cases have no additional constraints.
Translated by ChatGPT 5