Task Processing

Each task has a day window and a list of durations by start day; pick a subset and an order to finish as many tasks in their windows as possible.

Medium7Dynamic programmingBit manipulationSortingNo attempts yetTime limit2sMemory limit512 MB

Problem

Yeongseon works today, and she works tomorrow and the day after as well.

Yeongseon has been assigned nn tasks. Each task can be done only inside a fixed period, and the time it takes depends on the day she starts it.

Suppose a task has period [a,b][a, b]. If she starts it on day dd and it takes TT days, she works from day dd through day d+T1d + T - 1 and finishes on day d+T1d + T - 1. So she can start that task on day dd only when ada \le d and d+T1bd + T - 1 \le b.

For example, take a task with period [1,5][1, 5] that takes 4 days when started on day 1, 2 days on day 2, 3 days on day 3, 5 days on day 4, and 2 days on day 5. Starting on day 4 or on day 5 runs past day 5, so the task cannot finish inside its period. Starting on day 2 finishes it on day 3, which is the earliest possible.

Yeongseon handles one task at a time. If the previous task finished on day ee, the next task may start on that same day ee.

She does not have to do every task. Given the task information, find the largest number of tasks she can finish.

Input

The first line contains the number of tasks nn. (1n151 \le n \le 15)

Each of the next nn lines describes one task. The line starts with two integers aa and bb, followed by ba+1b - a + 1 integers. Those integers are the times TiT_i the task takes when it is started on day aa, day a+1a + 1, \dots, day bb, in that order. (1ab1001 \le a \le b \le 100, 1Ti1001 \le T_i \le 100)

Output

Print the largest number of tasks that can be finished.