This page is still under construction.

Parts of this page are still being built. What you see may change.

Tracking Robots

Time limit1sMemory limit128 MB

Summary
Count the fewest and most robots whose walks from region 1 interleave to produce the recorded region-label stream.
Level

Hard8 of 10

Topics
Graph
Solved
No attempts yet

Problem

Several robots move around inside an area and send their positions to a server. The server sees only the stream of positions and has to work out how many robots are in the area.

The area is a closed polygon cut into non overlapping regions labeled 1,…,N1, \dots, N. Every robot begins in region 11 and then starts moving around. A robot moves only into a region adjacent to the one it is in, and every time it enters a new region it sends that region's label to the server. A robot may enter and leave the same region many times.

The server receives one long stream of region labels and does not know which robot sent each label.

Assume every robot sent a region label at least once. Find the minimum and the maximum number of robots that could have produced the stream.

Input

The input holds several test cases. The first line of a test case has the number of regions NN (1≤N≤1001 \le N \le 100) and the length of the server's stream MM (1≤M≤2001 \le M \le 200). Each of the next NN lines describes one region, and the iith of them describes region ii. Such a line starts with cic_i, the number of regions adjacent to region ii, followed by the cic_i labels of those regions. The next line has the MM region labels of the stream, in the order the server received them. The input ends with a line containing 0 0.

The given stream is always one that some group of robots could really have produced.

Output

For each test case print one line with the minimum and the maximum possible number of robots in the area, separated by a space.

Examples1

  1. Example 1

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