#P2421. [NOI2002] 荒岛野人

[NOI2002] 荒岛野人

Description

The island of Crete is known for its tribes of savages. There are mm caves arranged in a ring on the island. These caves are numbered clockwise as 1,2,,m1, 2, \dots, m. There are nn savages living on the island, initially residing in caves C1,C2,,CnC_1, C_2, \dots, C_n in order. Afterward, each year, the ii-th savage moves forward clockwise by PiP_i caves and settles there.

Each savage ii has a lifespan value LiL_i, i.e., the number of years they live.

The following four figures describe the first four years on an island with 66 caves and three savages. The initial cave numbers of the three savages are 1,2,31, 2, 3; the number of caves they move each year are 3,7,23, 7, 2; and their lifespan values are 4,3,14, 3, 1.

Strangely, although there are many savages, no two savages ever occupy the same cave during their lifetimes, so the island remains peaceful and tranquil. Scientists are curious: what is the minimum number of caves needed to maintain peace on the island?

Input Format

The first line contains an integer nn (1n151 \leq n \leq 15), the number of savages.

Lines 22 to n+1n+1 each contain three integers Ci,Pi,LiC_i, P_i, L_i (1Ci,Pi1001 \leq C_i, P_i \leq 100, 0Li1060 \leq L_i \leq 10^6), representing the initial cave number, the number of caves moved each year, and the lifespan value for each savage.

Output Format

Output a single number MM, the minimum possible number of caves. It is guaranteed that the input has a solution and M106M \leq 10^6.

3
1 3 4
2 7 3
3 2 1
6

Hint

1n151 \leq n \leq 15, 1Ci,Pi1001 \leq C_i, P_i \leq 100, 0Li1060 \leq L_i \leq 10^6.
It is guaranteed that M106M \leq 10^6.

Translated by ChatGPT 5