#Q217. 「一本通 6.4 练习 1」荒岛野人
「一本通 6.4 练习 1」荒岛野人
Description
Original source: NOI 2002
The island of Crete is famous for its savage tribes. There are caves arranged in a circle on the island. These caves are numbered clockwise as .
The island is inhabited by savages. Initially, they live in caves respectively. Every year, the -th savage moves forward caves clockwise to settle down. Each savage has a lifespan , which is the number of years they survive. The following four diagrams illustrate the first four years of an island with caves and three savages. The initial cave numbers of the three savages are ; the number of caves they move each year are ; and their lifespans are respectively.

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 , the number of savages;
The next lines each contain three integers , representing the initial cave number, the number of caves moved each year, and the lifespan of each savage.
Output Format
Output a single integer , the minimum possible number of caves. The input data guarantees a solution, and does not exceed .
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, .