#arc109a. [arc109_a]Hands
[arc109_a]Hands
Problem Statement
There are two -story buildings, called A
and B
. (In this problem, the ground floor is called the first floor.)
For each , the -th floor of A
and that of B
are connected by a corridor. Also, for each , there is a corridor that connects the -th floor of A
and the -th floor of B
. You can traverse each of those corridors in both directions, and it takes you minutes to get to the other end.
Additionally, both of the buildings have staircases. For each , a staircase connects the -th and -th floors of a building, and you need minutes to get to an adjacent floor by taking the stairs.
Find the minimum time needed to reach the -th floor of B
from the -th floor of A
.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum time needed to reach the -th floor of B
from the -th floor of A
.
Sample Input 1
2 1 1 5
Sample Output 1
1
There is a corridor that directly connects the -nd floor of A
and the -st floor of B
, so you can travel between them in minute. This is the fastest way to get there, since taking the stairs just once costs you minutes.
Sample Input 2
1 2 100 1
Sample Output 2
101
For example, if you take the stairs to get to the -nd floor of A
and then use the corridor to reach the -nd floor of B
, you can get there in minutes.
Sample Input 3
1 100 1 100
Sample Output 3
199
Using just corridors to travel is the fastest way to get there.