555A.md (3001B)
1 Source: https://codeforces.com/problemset/problem/555/A 2 3 A. Case of Matryoshkas 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art. 22 23 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. 24 25 In one second, you can perform one of the two following operations: 26 27 * 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*; 28 * 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*. 29 30 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. 31 32 Input 33 34 The first line contains integers *n* (1 ≤ *n* ≤ 105) and *k* (1 ≤ *k* ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration. 35 36 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). 37 38 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. 39 40 Output 41 42 In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration. 43 44 Examples 45 46 Input 47 48 ``` 49 3 2 50 2 1 2 51 1 3 52 ``` 53 54 Output 55 56 ``` 57 1 58 ``` 59 60 Input 61 62 ``` 63 7 3 64 3 1 3 7 65 2 2 5 66 2 4 6 67 ``` 68 69 Output 70 71 ``` 72 10 73 ``` 74 75 Note 76 77 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. 78 79 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.