CF555A.Case of Matryoshkas

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 nn matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 11 to nn . 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, 12451→2→4→5 .

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

  • Having a matryoshka aa that isn't nested in any other matryoshka and a matryoshka bb , such that bb doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put aa in bb ;
  • Having a matryoshka aa directly contained in matryoshka bb , such that bb is not nested in any other matryoshka, you may get aa out of bb .

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 ( 12...n1→2→...→n ). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.

输入格式

The first line contains integers nn ( 1<=n<=1051<=n<=10^{5} ) and kk ( 1<=k<=1051<=k<=10^{5} ) — the number of matryoshkas and matryoshka chains in the initial configuration.

The next kk lines contain the descriptions of the chains: the ii -th line first contains number mim_{i} ( 1<=mi<=n1<=m_{i}<=n ), and then mim_{i} numbers ai1,ai2,...,aimia_{i1},a_{i2},...,a_{imi} — the numbers of matryoshkas in the chain (matryoshka ai1a_{i1} is nested into matryoshka ai2a_{i2} , that is nested into matryoshka ai3a_{i3} , and so on till the matryoshka aimia_{imi} that isn't nested into any other matryoshka).

It is guaranteed that m1+m2+...+mk=nm_{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.

输出格式

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

输入输出样例

  • 输入#1

    3 2
    2 1 2
    1 3
    

    输出#1

    1
    
  • 输入#2

    7 3
    3 1 3 7
    2 2 5
    2 4 6
    

    输出#2

    10
    

说明/提示

In the first sample test there are two chains: 121→2 and 33 . In one second you can nest the first chain into the second one and get 1231→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.

首页