QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 256 MB Total points: 10

#6048. Cellular explosion [B]

Statistics

The "Byte-cell" is a primitive organism inhabiting abandoned central processing units. It is an ordered sequence of cells, each of which can be one of $n$ types, numbered for simplicity from $1$ to $n$. A characteristic feature of the Byte-cell is its ability to replicate very rapidly.

In the first minute of its life, the Byte-cell consists of a single cell of type $1$. Every minute, cell replication occurs: each cell divides into a sequence of at least two cells. As a result of the division, cells of different types may be produced, but the division of a cell of type $k$ always results in the same sequence of cells $H(k) = h_{k,1}, h_{k,2}, \dots, h_{k,l_k}$. If in the $i$-th minute the Byte-cell consists of a sequence of cells $c_1, c_2, \dots, c_p$, then in the $(i+1)$-th minute it will consist of the sequence of cells formed by concatenating the sequences $H(c_1), H(c_2), \dots, H(c_p)$.

The Byte-cell reaches maturity when its sequence of cells contains a contiguous fragment that is a fixed sequence $S$ of consecutive cells of specific types.

Bytean scientists would like to study the life of the Byte-cell more closely, and in particular, to determine the time that elapses from the beginning of the Byte-cell's life until it reaches maturity.

Input

The first line of input contains two integers $n$ and $m$ ($1 \le n \le 500$, $1 \le m \le 1000$), representing the number of possible cell types and the length of the sequence of cells $S$ that must appear as a contiguous fragment of the Byte-cell's sequence for it to be considered mature, respectively.

This is followed by $n$ lines describing the cell replications: the $i$-th of these lines starts with an integer $l_i$ ($l_i \ge 2$), followed by $l_i$ integers $h_{i,1}, h_{i,2}, \dots, h_{i,l_i}$ ($1 \le h_{i,j} \le n$) representing the sequence $H(i)$. The sum of all values $l_i$ does not exceed $1000$.

The last line contains $m$ integers in the range from $1$ to $n$ representing the types of consecutive cells that make up the sequence $S$.

Output

Your program should output the number of the first minute of the Byte-cell's life in which it reaches maturity. If the Byte-cell never reaches maturity, output $-1$.

Examples

Input 1

3 2
2 2 3
3 1 3 3
2 1 2
3 1

Output 1

3

Note

In the second minute of its life, the Byte-cell consists of the sequence of cells $H(1) = 2, 3$. In the third minute, it takes the form $H(2), H(3) = 1, 3, 3, 1, 2$, and thus reaches maturity because it contains the fragment $S = 3, 1$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.