Milk Pails (Silver)#10585

Milk Pails (Silver)

1 second
128 megabytes
Medium

Farmer John has received an order for exactly units of milk that he needs to fill immediately. Unfortunately, his fancy milking machine has just broken, and all he has are two milk pails with integer capacities and that he can use to measure milk. Both pails are initially empty.

Using these two pails, he can perform at most operations of the following types:

Although FJ realizes that he may not be able to end up with exactly units of milk in the two pails, please help him determine the minimum possible error between and the total amount of milk in the two pails. In other words, compute the minimum value of |m - m'| such that FJ can obtain a total of m' units of milk in the two pails.

Input

One line contains four integers x, y, k, and m~(1 \le x, y \le 100, 1 \le k \le 100, 1 \le m \le 200).

Output

Print the minimum possible difference between and the total amount of milk FJ can produce.

Examples

In two steps, FJ can obtain the following amounts of milk in his pails:

(0, 0) = 0 units
(14, 0) = 14 units
(0, 50) = 50 units
(0, 14) = 14 units
(14, 36) = 50 units
(14, 50) = 64 units

The closest FJ can get to units is units, for a difference of . Note that it would require an additional step to empty the first pail and obtain .

Example 1

Loading
Loading