Under construction

Still being built. Things may change

Pesky Heroes

Time limit1sMemory limit128 MB

Summary
Given a tree-like cave structure with orc traversal rules based on left/right turns and one allowed right-turn override, find the minimum number of gateways needed to reach all trap-free dead ends.
Level

Hard8 of 10

Topics
Tree, Simulation, Graph
Solved
No attempts yet

Problem

The evil mage storms into his throne room. The heroes have raided his cave again, and they picked the week the traps were switched off for maintenance. Worse, they have pitched camp inside. The traps are live again, and the mage wants his trained orcs teleported to the right places right now. You saw what happened to the last servant who spoke, so you grab the map of the cave and get to work.

The map shows nn strategic key points, numbered 11 to nn. Every passage connects two key points, every trap sits on a key point, and every dead end is a key point. No key point has more than three passages. Key point 11 is the entrance to the outside world and has exactly one passage. From every key point there is exactly one path to the entrance. A key point with exactly one passage is a dead end, with key point 11 as the only exception.

Your master opens a magic gateway in the middle of a passage, never at a key point, and an orc walks out of it. The orc first heads in the direction that leads away from the light of the entrance. From there it walks by these rules.

  • At a dead end it turns around.
  • At a key point with two passages it leaves through the other passage.
  • At an intersection, a key point with three passages, it takes the passage on its left. It does this every time it stands at an intersection, on the way in and while walking back alike.

Left and right are seen from the orc as it walks, which is why the map lists the passages of a key point in clockwise order.

You may give the orc one number tt before it walks in. The tt-th time it stands at an intersection it takes the passage on its right instead of the left one. An orc remembers a single number until it comes back, so it turns right at most once per trip. You may send orcs through the same gateway one after another, each with its own number, and a gateway stays open as long as you need it.

An orc that steps on a key point with a trap is lost, and your master will not forgive that. Every orc also has to come back to its gateway, so choose tt accordingly.

The heroes camp in a dead end, so the orcs have to search every dead end that reaches the entrance without passing a trap. A dead end has to be searched when no key point on the path from it to the entrance holds a trap, the dead end itself included. Your master should open as few gateways as possible.

Input

The input consists of several data sets. Each set starts with a line holding two integers nn and mm, where 2≤n≤500002 \le n \le 50000 is the number of key points and 0≤m≤5000 \le m \le 500 is the number of traps.

The next nn lines describe the key points. Line ii holds an integer nin_i with 1≤ni≤31 \le n_i \le 3, the number of passages at key point ii, followed by the nin_i key points those passages lead to, listed in clockwise order.

The next mm lines hold one integer each, a key point that has a trap.

Key point 11 is the entrance and always has exactly one passage. The last data set is followed by a line holding 00 00, which is not processed.

Output

For each data set, print the minimum number of gateways on a line by itself.

Examples1

  1. Example 1

    Input
    5 1
    1 2
    3 1 4 3
    1 2
    2 2 5
    1 4
    3
    4 1
    1 2
    2 1 3
    2 2 4
    1 3
    4
    0 0
    
    Expected output
    1
    0