Олимпиадный тренинг

Задача 31924. Elevator


Peter needs to get from floor A to floor B. To call the elevator on all floors of the office building, except for the first and last, there are two buttons – to move down and move up. At the moment when Peter pressed the desired call button, the elevator was on floor C and carried one passenger to floor D. If the elevator passes the floor on which the call button is pressed, and the elevator moves in the appropriate direction, then the elevator stops to put an additional passenger. The elevator moves between adjacent floors in one unit of time, and it also takes one unit of time for the elevator to stop on the floor for disembarking or boarding passengers.
Write a program to calculate how long it will take Peter to reach floor B, assuming no one else calls the elevator.
The first line of input contains four integers A, B, C, and D separated by a single space (1 ≤ A, B, C, D ≤ 20, A≠B, C≠D, A≠C).< /div>
Output one integer – the number of time units from the moment the elevator is called to the moment when Peter exits the elevator on floor B.
 
Input Output
3 9 2 5 10
3 9 5 2 13
Note:
Explanation for example 1
The elevator will reach the 3rd floor in 1 unit of time, stop for 1 unit of time for Peter to get into the elevator, then after 2 units of time it will reach the 5th floor and stop for 1 unit of time to disembark the previous passenger, after In 4 time units, the elevator will take Peter to the 9th floor, and after 1 time unit, Peter will exit the elevator.