An Arm and a Leg
Tom has decided she would like to buy the brand new iTone Duo, but to even buy the base model it might cost an arm and a leg, maybe even a kidney or liver. Luckily, due to medical advancements, Tom is able to quickly and easily sell such body parts and have them repeatedly regenerate, each time taking the same amount of time. However, each body part may only regenerate a certain amount of times. Initially, Tom starts off with all sellable body parts.
Each of Tom's body parts has a certain monetary value, and a certain amount of time required to regenerate. Every time a body part regenerates, Tom can sell it again for its stated value. Selling a body part takes no time. How long will it take Tom to be able to afford the new iTone?
Input
The first line consists of two integers (
), and
(
), the number of body parts that Tom can sell, and the cost of the iTone Duo.
The next lines each contain three integers
,
, and
. The
-th body part can be sold for
dollars whenever it is available. It is initially available at time
. After being sold, it regenerates and becomes available after
seconds. It can regenerate at most
times, so it can be sold at most
times.
).
Output
Output the minimum non-negative number of seconds after which Tom can afford the iTone Duo. If it is impossible for Tom to afford it no matter the amount of time, output -1.
Examples
Input 1
2 20
4 1 1
4 3 4
Output 1
8
At , Tom can instantly sell both intially available body parts for a total of
dollars.
At , the first sold body part has been regenerated, and can be sold again instantly. This brings Tom's total to
dollars. Note that this body part won't regenerate again at
because it has already regenerated the maximum number of times.
At , the second sold body part has been regenerated, and can be sold again instantly. This brings Tom's total to
dollars.
At , the second sold body part has been regenerated, and can be sold again instantly. This brings Tom's total to
dollars, able to afford the iTone Duo.
Input 2
2 20
1 1 1
1 1 4
Output 2
-1
Tom cannot regenerate enough body parts to make dollars.
Comments