## WC '18 Contest 2 J1 - Alternate Access

View as PDF

Points: 3 (partial)
Time limit: 1.0s
Memory limit: 16M

Author:
Problem type
##### Woburn Challenge 2018-19 Round 2 - Junior Division

The IMF (Impossible Mission Force) has gotten wind of a vile plot by escaped convict Solomon Lane to poison Toronto's entire supply of maple syrup! They've dispatched their best agent, Ethan Hunt, to put a stop to this.

Ethan has learned that Solomon will be meeting with a poison supplier somewhere in the CN Tower, which has floors, numbered from bottom to top. Ethan has already infiltrated the tower by disguising his identity behind a hockey mask, and made his way to floor . Unfortunately, Solomon is on a different floor . Simply taking the stairs or elevator at this point won't do, as those routes are sure to be heavily guarded. As such, Ethan will need to use creative thinking to gain alternate access to floor .

Fortunately, he's got the high-tech gadgets for the job. Without a second thought, Ethan has found himself cutting through a window and clinging to the exterior of the tower at floor using supermagnetic gloves. What remains is painstakingly making his way to floor , ideally before the gloves run out of energy. Ethan is able to climb upwards by floor (in other words, increase his current floor by ) by using up joules of energy. Similarly, he's able to climb downwards by floor (decreasing his current floor by ) by using up joules of energy. Upon arriving at floor , it'll be a simple matter of cutting through a window, barging inside, and taking out Solomon's guards (there can't be more than a couple dozen of them).

What's the minimum amount of glove energy required for Ethan to gain access to floor ?

#### Input Specification

The first line of input consists of a single integer, .
The second line consists of a single integer, .
The third line consists of a single integer, .
The fourth line consists of a single integer, .

#### Output Specification

Output a single integer, the minimum amount of energy (in joules) required for Ethan to climb from floor to floor .

#### Sample Input 1

40
39
4
5

#### Sample Output 1

5

#### Sample Input 2

142
147
9
12

#### Sample Output 2

45

#### Sample Explanation

In the first case, Ethan must climb downwards by floor (from to ), which requires joules of energy.

In the second case, Ethan must climb upwards by floors (from to ), which requires a total of joules of energy.