You met a strange lady, she made you nervous, she took you in and gave you breakfast, and she said:
You're given integers n,x,y and , you can use the following two operations:
M-operations: Multiply by either or .
The cost for this operation is h+1 where is the number of M-operations done so far (so the first M-operation costs and the second costs .... and the -th one costs ).
S-operations: Subtract from .
The cost for this operation is .
Output the minimum total cost to make equal to or output if it's impossible to make reach .
The input consists of integers n,x,y and (0 \le |n| \le 2 \cdot 10^9 , 1 \le x,y,z \le 2 \cdot 10^9).
The output consists of a single integer, the minimum total cost to make equal to .
Example 1
Example 2
In the first test. To achieve a cost of we do:
Subtract from for a cost of , now n=1.
Multiply by for a cost of , now n=2.
Multiply by for a cost of , now n=4.
Subtract from for a cost of , now n=0.
The total cost is 1+1+2+1=5, it can be shown that there's no way to get a total cost lower than , so is the answer.
In the second test. No matter what operations we make we can't make equal to , so the answer is .
Subtask ( points) : (|n|,x,y,z \le 5)
Subtask ( points) : (|n|,x,y,z \le 1000)
Subtask ( points) : (|n|,x,y,z \le 10^6)
Subtask ( points) : (z \le 10^6)
Subtask ( points) : x = y
Subtask ( points) : no additional constraints.