human
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 ≤ 105) and k (1 ≤ k ≤ 105) — 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 mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).

It is guaranteed that m1 + m2 + ... + mk = 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...
 ... Here minimum time it is possible to perform this action?
assistant
#Here minimum time it is possible to perform this action.

#!/usr/bin/env python
# 556C_matr.py - Codeforces.com 556C Matr quiz
#
# Copyright (C) 2015 Sergey
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Input

The first line contains integers n and k the number of matryoshkas and
matryoshka chains in the initial configuration.

Output

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

# Standard libraries
import unittest
import sys
import re

# Additional libraries

###############################################################################
# Matr Class
###############################################################################

class Matr:
    """ Matr representation """

    def __init__(self, args):
        """ Default constructor """

        self.n = args[0]
        self.k = args[1]
        self.list = args[2]

    def turns(self):
        result = 0
        for l in self.list:
            result += len(l) - 1
        result += self.n - 1
        return result

    def reduce(self):
        result = 0
        for l in self.list:
            max = 0
            for i in range(len(l)):
                if l[i] == i + 1:
                    max = i
            result += max * 2
        return result

    def calculate(self):
        """ Main calcualtion function of the class """
        turns = self.turns()
        reduce = self.reduce()
        result = turns - reduce

        return str(result)

###############################################################################
#...