#Q217. 「一本通 6.4 练习 1」荒岛野人

    ID: 2299 Type: Default 1000ms 512MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>NOI扩展欧几里德算法早于 2010一本通提高

「一本通 6.4 练习 1」荒岛野人

Description

Original source: NOI 2002

The island of Crete is famous for its savage tribes. There are MM caves arranged in a circle on the island. These caves are numbered clockwise as 1,2,,M1,2,\cdots ,M.

The island is inhabited by NN savages. Initially, they live in caves C1,C2,,CNC_1,C_2,\cdots ,C_N respectively. Every year, the ii-th savage moves forward PiP_i caves clockwise to settle down. Each savage ii has a lifespan LiL_i, which is the number of years they survive. The following four diagrams illustrate the first four years of 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 lifespans are 4,3,14,3,1 respectively.

savage.png

Strangely, despite the large number of savages, no two savages ever occupy the same cave during their lifetimes, ensuring the island remains peaceful and tranquil. This has greatly intrigued the scientists. They want to know: what is the minimum number of caves required to maintain peace on the island?

Input Format

The first line contains an integer NN, the number of savages;

The next NN lines each contain three integers Ci,Pi,LiC_i, P_i, L_i, representing the initial cave number, the number of caves moved each year, and the lifespan of each savage.

Output Format

Output a single integer MM, the minimum possible number of caves. The input data guarantees a solution, and MM does not exceed 10610^6.

Sample 1

This sample corresponds to the example in the problem description.

3
1 3 4
2 7 3
3 2 1

6

Constraints & Hints

For all data, 1N15,1Ci,Pi100,0Li1061\le N\le 15,1\le C_i,P_i\le 100,0\le L_i\le 10^6.