Gremlins are small, funny, fuzzy creatures. In times long gone they used
to cause quite a ruckus, but now most of them live decent, honest family
lives.
There are
different types of gremlins, coded with numbers
to
for your convenience. Legend has it that
years ago a laboratory
accident occurred causing
gremlins, one of each type to be born.
As you all know, in order for gremlins to reproduce, no mating rituals
are required. Just add a dash of water and you instantly get a few new
cocoons.
Gremlins of type
need exactly
years to mature and spawn
cocoons. For each cocoon you know how many years it takes to
hatch, and which gremlin type is contained within. The original gremlin
unfortunately dies while producing cocoons.
Now,
years after the original accident, scientists wonder what is the
longest ancestral chain whose genome is currently represented. They are
only interested in ancestors of hatched gremlins, not those still
cocooned. You are safe to assume that all gremlins that were supposed to
hatch this year did so.
Input Specification
The first line of input contains two integers
and
, the number of gremlin types and
the number of years after the original accident.
The next
lines contain gremlin type descriptions, three lines per
type:
- The first line contains integers
and
, the number of cocoons formed
when this gremlin type spawns and number of years this gremlin type
needs to reach maturity.
- The second line contains
integers between
and
inclusive, the types of gremlins hatched from cocoons formed by this
gremlin type.
- The third line contains
integers between
and
,
representing hatching time for each cocoon, in years.
Output Specification
The first and only line of output should contain the length of the
currently longest ancestral chain.
Sample Input 1
Copy
1 42
1 10
1
5
Sample Output 1
Copy
2
Sample Input 2
Copy
2 42
1 10
1
5
1 5
1
5
Sample Output 2
Copy
3
Sample Input 3
Copy
3 8
4 5
1 2 3 2
1 2 1 3
1 1
3
1
2 1
1 2
2 1
Sample Output 3
Copy
4
Explanation of Sample 2
The original gremlin hatched in the accident after
years spawns a
single cocoon and dies.
years after the accident, a new gremlin hatches from the cocoon. His
ancestral chain contains one gremlin.
years after the accident this
gremlin spawns a new cocoon and dies.
years after the accident, a new gremlin hatches from the cocoon. His
ancestral chain contains two gremlins.
years after the accident this
gremlin spawns a new cocoon and dies.
years after the accident, the current cocoon is not yet hatched, so
the longest ancestral chain ever recorded is two gremlins in length.
Comments