#P1639. [USACO18FEB] Teleportation B

[USACO18FEB] Teleportation B

Problem 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 xx and yy: manure hauled to position xx can be instantly teleported to position yy, and vice versa.

Farmer John wants to transport manure from position aa to position bb. 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: aa and bb, representing the start and end positions, followed by xx and yy, representing the teleporter. All positions are integers in the range 01000\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.

3 10 8 2
3

Hint

In this sample, the best strategy is to haul the manure from position 33 to position 22, teleport to position 88, and then haul to position 1010. So the total tractor distance needed is 1+2=31 + 2 = 3.

Translated by ChatGPT 5