Codeforces Round 310 (Div. 2)
C
Case of Matryoshkas
Codeforces
未评级
原题
← BD →
翻译中…

Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.

翻译中…

The main exhibit is a construction of $*n*$ matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from $1$ to $*n*$. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, $1 → 2 → 4 → 5$.

翻译中…

In one second, you can perform one of the two following operations:

翻译中…
  • Having a matryoshka $*a*$ that isn't nested in any other matryoshka and a matryoshka $*b*$, such that $*b*$ doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put $*a*$ in $*b*$;
  • Having a matryoshka $*a*$ directly contained in matryoshka $*b*$, such that $*b*$ is not nested in any other matryoshka, you may get $*a*$ out of $*b*$.
翻译中…

According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain ($1 → 2 → ... → *n*$). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.

翻译中…

Input

翻译中…

The first line contains integers $*n*$ ($1 ≤ *n* ≤ 10^{5}$) and $*k*$ ($1 ≤ *k* ≤ 10^{5}$) — the number of matryoshkas and matryoshka chains in the initial configuration.

翻译中…

The next $*k*$ lines contain the descriptions of the chains: the $*i*$-th line first contains number $*m*_{*i*}$ ($1 ≤ *m*_{*i*} ≤ *n*$), and then $*m*_{*i*}$ numbers $*a*_{*i*1}, *a*_{*i*2}, ..., *a*_{*im*_{*i*}}$ — the numbers of matryoshkas in the chain (matryoshka $*a*_{*i*1}$ is nested into matryoshka $*a*_{*i*2}$, that is nested into matryoshka $*a*_{*i*3}$, and so on till the matryoshka $*a*_{*im*_{*i*}}$ that isn't nested into any other matryoshka).

翻译中…

It is guaranteed that $*m*_{1} + *m*_{2} + ... + *m*_{*k*} = *n*$, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.

翻译中…

Output

翻译中…

In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.

翻译中…

Note

翻译中…

In the first sample test there are two chains: $1 → 2$ and $3$. In one second you can nest the first chain into the second one and get $1 → 2 → 3$.

翻译中…

In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.

翻译中…

样例

Input #1
3 2
2 1 2
1 3
Output #1
1
InputCopy OutputCopy
Input #2
7 3
3 1 3 7
2 2 5
2 4 6
Output #2
10
InputCopy OutputCopy
题目信息
题号
556C
时间限制
2 s
内存限制
256 MB

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。