P1639 [USACO18FEB] Teleportation B
Description
Farmer John hates the farm chore of transporting cow manure the most. To make this process simpler, he invented a great device: a manure teleporter. Instead of using a tractor to haul a wagon full of manure from one place to another, he can use the teleporter to instantly move the manure from one place to another.
Farmer John’s farm is built along a long straight road, so every location on his farm can be represented by its position on the road (like a point on a number line). A teleporter can be described by two numbers $x$ and $y$: manure hauled to position $x$ can be instantly teleported to position $y$, and vice versa.
Farmer John wants to transport manure from position $a$ to position $b$. He built a teleporter that might help this process (of course, if it does not help, he can choose not to use it). Please help him find the minimum total distance that he needs to haul the manure using the tractor.
Input Format
The input contains only one line with four space-separated integers: $a$ and $b$, representing the start and end positions, followed by $x$ and $y$, representing the teleporter. All positions are integers in the range $0\ldots 100$, and they are not necessarily distinct.
Output Format
Output one integer, the minimum distance that Farmer John needs to haul the manure using the tractor.
Explanation/Hint
In this sample, the best strategy is to haul the manure from position $3$ to position $2$, teleport to position $8$, and then haul to position $10$. So the total tractor distance needed is $1 + 2 = 3$.
Translated by ChatGPT 5