The year is 2060. Marcus has retired from computer science and has become a gardener. In his garden, he is growing flowers along a number line, where the flower is at position . Each flower needs unit of water to survive the next day.
He also has a watering can that can hold up to units of water, and it can be refilled by the hose at position . As Marcus walks along his garden, he will only be able to water the flowers that are at the same position as him.
Marcus begins at position with an empty watering can. Since he is very lazy, he would like to know the minimum distance he must walk to water all of his plants, and return to position . Can you help him?
Constraints
For all subtasks,
Subtask 1 [50%]
Subtask 2 [50%]
No additional constraints.
Input Specification
The first line of input contains one integer, , the number of flowers in Marcus' garden.
The second line of input contains one integer, , the maximum capacity of his watering can.
Output Specification
Output one line containing one integer, the minimum distance Marcus has to walk to water all his plants and return to position .
Note that this value may not fit within a 32-bit integer type.
Sample Input 1
4
2
Sample Output 1
12
Explanation for Sample 1
One possible solution is as follows:
- Marcus is at position and fills his watering can. It now holds units of water.
- Marcus walks unit right to flower , and waters it. His watering can now holds unit of water.
- Marcus walks more unit right to flower , and waters it. His watering can is now empty.
- Marcus walks units left back to position and refills his watering can. It now holds units of water.
- Marcus walks units right to flower , and waters it. His watering can now holds unit of water.
- Marcus walks more unit right to flower , and waters it. His watering can is now empty.
- Marcus walks units left to return to position .
Marcus walks a total of units. It can be proven that this is minimal.
Sample Input 2
5
7
Sample Output 2
10
Explanation for Sample 2
Marcus' watering can holds more than enough water for all of the flowers in one trip! Thus, Marcus can simply fill his watering can once, walk right units and water the flowers along the way, and finally walk units left to return to position . He walks a total of units, which is minimal.
Comments