B. Bocchi the Neural Network
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Unable to avoid the AI takeover, Bocchi has recently started studying neural networks! She has learned a special type of neural network that can be modeled as a specific type of graph.

The structure of this graph is special: it consists of $$$n + 2$$$ nodes, each with some value $$$v_i$$$. Bocchi also notices that all values from $$$0$$$ to $$$n + 1$$$ are present. The graph has a starting node, $$$d$$$ layers of nodes, and an ending node*. Nodes within each layer aren't connected, but each node in layer $$$i$$$ is connected to every node in layer $$$i + 1$$$ with a directed edge, with the starting node being connected to every node in layer $$$1$$$ and every node in layer $$$d$$$ being connected to the ending node. The starting node's value is always $$$0$$$, while the ending node's value is always $$$n + 1.$$$

After studying the neural network for some time, she deems the efficiency of a certain path from the starting node to the ending node to be the $$$\text{MEX}$$$** of the values on the path.

For example, suppose that there are two layers, the first consisting of nodes of values 4 and 1, and the second consisting of nodes of values 2 and 3. The starting node is 0 and the ending node is 5.

Perusing all of the options:

  • The path $$$0\rightarrow4\rightarrow2\rightarrow5$$$ has an efficiency of $$$1$$$.
  • The path $$$0\rightarrow4\rightarrow3\rightarrow5$$$ has a efficiency of $$$1$$$.
  • The path $$$0\rightarrow1\rightarrow2\rightarrow5$$$ has a efficiency of $$$3$$$.
  • The path $$$0\rightarrow1\rightarrow3\rightarrow5$$$ has a efficiency of $$$2$$$.

To see how well her neural network works, she gives her neural network an evaluation score. Specifically, the evaluation score is equal to the maximum efficiency over all paths from starting node to ending node. In the example neural network above, the path with the highest efficiency is the path $$$0\rightarrow1\rightarrow2\rightarrow5,$$$ so the evaluation score of the neural network is $$$3.$$$

After spending all day building her neural network, she is too tired to evaluate it. She now gives you her neural network and asks you to find the evaluation score of it!

Notes:

* For this problem, the starting and ending nodes do not count as layers.

** The $$$\text{MEX}$$$, or minimal excluded, of a sequence is the smallest non-negative integer not present in that sequence.

Input

The first line contains two positive integers: $$$n$$$ $$$(1 \le n \le 5 \cdot 10^5)$$$, the total number of nodes in the layers (that is, excluding the starting and ending nodes), and $$$d$$$ $$$(1 \le d \le n)$$$, the number of layers.

There are then $$$d$$$ lines: the first number contains $$$m$$$, the number of nodes in the layer, followed by $$$m$$$ integers, denoting the value of each node.

It is guaranteed that all values from $$$0$$$ to $$$n + 1$$$ are present.

Note: The starting and ending nodes are not included in the input.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-5$$$ satisfy $$$n \leq 10$$$.

Tests $$$6-10$$$ satisfy $$$n \leq 100$$$.

Tests $$$11-15$$$ satisfy $$$n \leq 1000$$$.

Tests $$$16-20$$$ satisfy no additional constraints.

Output

Output a single integer—the evaluation score of the neural network.

Example
Input
4 2
2 4 1
2 2 3
Output
3
Note

Problem Idea: otat

Problem Preparation: ThatRowletOwlet

Occurrences: Novice B