Inha Suit
Time limit1sMemory limit128 MB
Starting at height 1, choose one of five moves before each tree so the height lands on a hole, minimizing teleport (T) uses within limit K.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Graph, BFS
- Solved
- No attempts yet
Problem
Gyuhwan built the Inha Suit after seeing the Iron Man suit. Wearing it, he wants to cross a forest where every tree is 20 m tall and reach Inha University. The Inha Suit moves only up and down, and it climbs no higher than 20 m. The suit can sit only at an integer height between 1 and 20.
The suit has five movement functions.
- O: stay at the current height.
- A: rise by 1 m.
- B: rise by the current height. If the current height is 10 m or more, the suit always moves to 20 m.
- C: descend by 1 m.
- T: teleport to any height between 1 and 20.
Function T harms the wearer, so it may be used at most times and should be used as rarely as possible.
A strong wind blows from the right while he crosses the forest, so he can hit a tree. Every tree has holes drilled through it, and passing at the height of a hole avoids the collision. Between two neighboring trees there is room for exactly one movement function.
Gyuhwan starts at height 1 m, and he also uses exactly one movement function before he passes the first tree. So right before the -th tree he uses one movement function, and the resulting height must equal one of the hole heights of the -th tree. Otherwise he hits that tree.
For example, suppose function T may be used at most twice, there are 5 trees, and trees 1 to 5 each have a single hole, at heights 1, 2, 4, 6 and 5.

Figure 1. The example
Starting from height 1, functions O, A and B give heights 1, 2 and 4, which passes the first three trees. From height 4 the hole of the fourth tree sits at height 6, but O gives 4, A gives 5, B gives 8 and C gives 3, so those four functions do not reach it. Using T once to move to height 6 and then C gives height 5, which passes the last tree. One use of T is enough.
When a tree has several holes, T can send him to any one of them and the rest of the route changes with that choice, which makes the decision harder. Help Gyuhwan cross the forest safely with the fewest uses of T.
Input
The first line contains the number of trees ().
The second line contains the limit () on the number of T uses.
Each of the next lines contains the hole count () of one tree followed by the heights of its holes, separated by spaces. Every hole height is an integer between 1 and 20, the heights within one tree are distinct, and they are not necessarily sorted. The trees are passed in the order given.
Output
Print on one line the minimum number of T uses needed to pass every tree safely. If no route uses T at most times, print -1.