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.


-----Examples-----
Input
3 2
2 1 2
1 3

Output
1

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

Output
10



-----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.

Source: codeforces | Difficulty: MEDIUM
Tags: Implementation


Solutions:
(n, k) = map(int, input().split())
p = [0 for i in range(n + 1)]
d = p.copy()
for i in range(k):
	a = list(map(int, input().split()))
	l = len(a)
	for j in range(1, l - 1):
		p[a[j]] = a[j + 1]
		d[a[j + 1]] = 1
c = 1
while p[c] == c + 1:
	c = p[c]
ans = 0
for i in range(c + 1, n + 1):
	ans += 1 + d[i]
print(ans)


(num_dolls, num_chains) = map(int, input().split())
total = num_dolls - 1
for i in range(num_chains):
	chain = list(map(int, input().split()))
	chain.pop(0)
	total += len(chain) - 1
	if 1 in chain:
		pos = chain.index(1) + 1
		while pos < len(chain) and chain[pos] == chain[pos - 1] + 1:
			total -= 2
			pos += 1
print(total)


__author__ = 'artyom'

def main():
	(n, k) = read(3)
	c = 0
	l = 1
	for _ in range(k):
		a = read(3)
		if a[1] == 1:
			while l < a[0] and a[l + 1] - a[l] == 1:
				l += 1
			c += a[0] - l
		else:
			c += a[0] - 1
	return c + n - l

def read(mode=1, size=None):
	if mode == 0:
		return input().strip()
	if mode == 1:
		return int(input().strip())
	if mode == 2:
		return input().strip().split()
	if mode == 3:
		return list(map(int, input().strip().split()))
	a = []
	for _ in range(size):
		a.append(read(3))
	return a

def write(s='\n'):
	if s is None:
		s = ''
	if isinstance(s, tuple) or isinstance(s, list):
		s = ' '.join(map(str, s))
	s = str(s)
	print(s, end='\n')
write(main())


(n, k) = map(int, input().split())
time = 0
ntd = 0
for i in range(k):
	mtr = list(map(int, input().split()))[1:]
	if mtr[0] == 1:
		ln = 0
		dlt = mtr[-1]
		mtr.reverse()
		for (j, val) in enumerate(mtr[:-1]):
			if val - mtr[j + 1] != 1:
				time += j - ln + 1
				ln = j + 1
				dlt = mtr[j + 1]
	else:
		time += len(mtr) - 1
print(time + n - dlt)


(n, k) = map(int, input().split())
M = [list(map(int, input().split()))[1:] for i in range(k)]
matryoshkas = 0
for i in range(k):
	if M[i][0] == 1:
		for j in range(len(M[i]) - 1):
			if M[i][j] + 1 != M[i][j + 1]:
				matryoshkas += len(M[i]) - j - 1
				break
	else:
		matryoshkas += len(M[i]) - 1
print(2 * matryoshkas + k - 1)


(n, k) = list(map(int, input().split()))
for _ in range(k):
	a = list(map(int, input().split()))[1:] + [0]
	if a[0] != 1:
		continue
	i = 0
	while a[i + 1] == i + 2:
		i += 1
print(2 * n - k - 1 - 2 * i)


(n, k) = map(int, input().split())
s = 0
l = 0
for i in range(k):
	a = list(map(int, input().split()))
	m = a[0]
	a = a[1:]
	valid = True
	for j in range(m):
		if a[j] != j + 1:
			valid = False
			break
		else:
			l += 1
print(2 * n - 2 * l - k + 1)


(n, k) = map(int, input().split())
m = [0] * k
f = 0
for y in range(k):
	m = list(map(int, input().split()))[1:]
	i = 0
	while i < len(m):
		if m[i] == 1:
			if i > 0:
				f += 1
			j = i + 1
			while j < len(m):
				if m[j] - m[j - 1] != 1:
					f += 1
					break
				j += 1
			i = j
		elif i > 0:
			f += 1
		i += 1
print(2 * f + k - 1)


[n, k] = [int(i) for i in input().split()]
unnest = 0
chains = k
for mats in range(0, k):
	arr = [int(i) for i in input().split()]
	m = arr[0]
	if m == 1:
		continue
	for i in range(2, m + 1):
		if arr[i] != arr[i - 1] + 1 or arr[1] != 1:
			unnest += m - i + 1
			chains += m - i + 1
			break
print(unnest + chains - 1)


def solve(n, seq):
	found = 0
	res = 0
	for s in seq:
		if s[1] == 1:
			found = 1
			while len(s) > found and s[found] == found:
				found += 1
			found -= 1
			rest = s[0] - found
			res += rest
		else:
			res += s[0] - 1
	return res + n - found
(n, k) = map(int, input().split())
seq = [list(map(int, input().split())) for _ in range(k)]
print(solve(n, seq))


import unittest
import sys
import re

class Matr:

	def __init__(self, args):
		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):
		turns = self.turns()
		reduce = self.reduce()
		result = turns - reduce
		return str(result)

def get_inputs(test_inputs=None):
	it = iter(test_inputs.split('\n')) if test_inputs else None

	def uinput():
		if it:
			return next(it)
		else:
			return input()
	nums = [int(s) for s in uinput().split()]
	strs = []
	for i in range(nums[1]):
		list = [int(s) for s in uinput().split()]
		list.pop(0)
		strs.append(list)
	inputs = nums
	inputs.append(strs)
	return inputs

def calculate(test_inputs=None):
	return Matr(get_inputs(test_inputs)).calculate()

class unitTests(unittest.TestCase):

	def test_sample_tests(self):
		self.assertEqual(calculate('3 2\n2 1 2\n1 3'), '1')
		self.assertEqual(calculate('7 3\n3 1 3 7\n2 2 5\n2 4 6'), '10')

	def test_get_inputs(self):
		self.assertEqual(get_inputs('3 2\n2 1 2\n1 3'), [3, 2, [[1, 2], [3]]])

	def test_Matr_class__basic_functions(self):
		d = Matr([3, 2, [[1, 2], [3]]])
		self.assertEqual(d.turns(), 3)
		self.assertEqual(d.reduce(), 2)
sys.setrecursionlimit(100000)
if sys.argv[-1] == '-ut':
	unittest.main(argv=[' '])
print(calculate())


import sys
(n, k) = list(map(int, sys.stdin.readline().split()))
(ans, pref) = (0, 0)
for i in range(k):
	(m, *a) = list(map(int, sys.stdin.readline().split()))
	j = 0
	while j < m and a[j] == j + 1:
		j += 1
	if j:
		pref = j
		ans += m - j
	else:
		ans += m - 1
print(ans + n - pref)


(n, m) = list(map(int, input().split()))
a = [[0] for i in range(m)]
for i in range(m):
	a[i] = list(map(int, input().split()))
b = [0] * (n + 1)
for i in range(m):
	for j in range(1, a[i][0]):
		b[a[i][j]] = a[i][j + 1]
	b[a[i][a[i][0]]] = -2
for i in range(1, n + 1):
	if b[i] != i + 1:
		k = i
		break
print(2 * n - m - 2 * k + 1)


def check(b):
	for i in range(1, len(b)):
		if b[i] == 1:
			return (True, i)
	else:
		return (False, -1)

def find_long(a):
	cost = 0
	for i in range(len(a)):
		(flag, idx) = check(a[i])
		if flag == True:
			k = idx + 1
			prev = a[i][idx]
			l = 1
			while k < len(a[i]):
				if prev + 1 != a[i][k]:
					break
				l += 1
				prev = a[i][k]
				k += 1
			if idx - 1 != 0:
				cost += 1 + idx - 2
			if len(a[i]) - (idx + l) != 0:
				cost += len(a[i]) - (idx + l)
			break
	return (cost, i, l)
(n, k) = map(int, input().split())
a = []
for _ in range(k):
	a.append([])
for j in range(k):
	a[j] = list(map(int, input().split()))
(cost, idx, l) = find_long(a)
for i in range(len(a)):
	if i != idx:
		cost += len(a[i]) - 2
print(cost + n - l)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy
from itertools import chain, dropwhile, permutations, combinations
from collections import defaultdict, deque

def VI():
	return list(map(int, input().split()))

def I():
	return int(input())

def LIST(n, m=None):
	return [0] * n if m is None else [[0] * m for i in range(n)]

def ELIST(n):
	return [[] for i in range(n)]

def MI(n=None, m=None):
	if n is None:
		(n, m) = VI()
	arr = LIST(n)
	for i in range(n):
		arr[i] = VI()
	return arr

def MS(n=None, m=None):
	if n is None:
		(n, m) = VI()
	arr = LIST(n)
	for i in range(n):
		arr[i] = input()
	return arr

def MIT(n=None, m=None):
	if n is None:
		(n, m) = VI()
	a = MI(n, m)
	arr = LIST(m, n)
	for (i, l) in enumerate(a):
		for (j, x) in enumerate(l):
			arr[j][i] = x
	return arr

def main(info=0):
	(n, k) = VI()
	ans = k - 1
	z = LIST(n + 2)
	lasts = []
	for iii in range(k):
		x = VI()
		above = 0
		lasts.append(x[x[0]])
		for i in range(x[0], 1, -1):
			if x[i] - 1 != x[i - 1]:
				ans += 2
				above = 0
				lasts.append(x[i - 1])
			else:
				above += 1
				z[x[i - 1]] = above
	lasts = sorted(lasts)
	for x in lasts:
		if z[x + 1] != 0:
			ans += 2 * z[x + 1]
			for i in range(x + 1, x + 1 + z[x + 1]):
				z[i] = 0
	print(ans)
main()


I = lambda : map(int, input().split())
(n, k) = I()
r = k - 1
y = 0
for _ in range(k):
	t = list(I())
	for i in range(1, t[0]):
		if t[i] == y + 1:
			y += 1
			if t[i + 1] != t[i] + 1:
				r += 2
		else:
			if y:
				y = -1
			r += 2
print(r)


def main():
	(n, k) = list(map(int, input().split()))
	for i in range(k):
		tmp = input().split()
		if tmp[1] == '1':
			tmp.append('0')
			tmp[0] = '0'
			for (i, m) in enumerate(map(int, tmp)):
				if i != m:
					print((n - i) * 2 - k + 3)
					return

def __starting_point():
	main()
__starting_point()


(n, k) = map(int, input().split())
u = 0
for i in range(k):
	a = list(map(int, input().split()))[1:] + [0]
	if a[0] == 1:
		while a[u] == u + 1:
			u += 1
print(n + n - u - u - k + 1)


(n, k) = (int(x) for x in input().split())
l = [None for _ in range(k)]
for i in range(k):
	l[i] = [int(x) for x in input().split()[1:]]
for m in l:
	if m[0] == 1:
		i = 0
		while i < len(m) and m[i] == i + 1:
			i += 1
		print(n * 2 - i * 2 - k + 1)


(n, m) = (int(x) for x in input().split())
ans = 2 * n - m - 1
for i in range(m):
	a = [int(x) for x in input().split()]
	for j in range(1, a[0]):
		if a[j + 1] == j + 1 and a[j] == j:
			ans -= 2
		else:
			break
print(ans)


flag1 = True
count1 = sec = 0
(n, k) = map(int, input().split())
for i in range(k):
	(m, *a) = [int(x) for x in input().split()]
	if flag1:
		check = 0
		for j in range(m):
			if a[j] == 1:
				t = j
				check = 1
				while t + 1 < m:
					if a[t + 1] - a[t] == 1:
						count1 += 1
						t += 1
					else:
						break
		if check:
			flag1 = False
			sec += 2 * (m - (count1 + 1) + 1) - 1
		else:
			sec += 2 * m - 1
	else:
		sec += 2 * m - 1
print(sec - 1)


import sys
sys.setrecursionlimit(1000000000)
(n, k) = map(int, input().split())
t = []
x = 0
for i in range(k):
	s = input().split()
	h = []
	for j in range(len(s) - 1):
		h += [int(s[j + 1])]
		if int(s[j + 1]) == 1:
			x = i
	t += [h]
v = 1
bb = True
while bb and len(t[x]) > v:
	if t[x][v] == t[x][v - 1] + 1:
		v += 1
	else:
		bb = False
m = len(t[x]) - v + (n - len(t[x])) - (k - 1) + (n - v)
print(m)


inp = lambda : map(int, input().split())
(n, m) = inp()
ans = 0
for i in range(m):
	(num, *a) = inp()
	for j in range(1, num):
		if a[j] != j + 1:
			ans += 2
	if a[0] != 1:
		ans += 1
print(ans)


(N, K) = map(int, input().split())
ans = 0
group = 0
for _ in range(K):
	mat = [int(x) for x in input().split()]
	(n_mat, mat) = (mat[0], mat[1:])
	if mat[0] == 1:
		for i in range(n_mat - 1):
			if mat[i] + 1 != mat[i + 1]:
				ans += n_mat - i - 1
				group += n_mat - i - 1
				break
		group += 1
	else:
		ans += n_mat - 1
		group += n_mat
ans += group - 1
print(ans)


two = input().split()
n = int(two[0])
k = int(two[1])
if n == 1 or k == 1:
	print('0')
else:
	seta = []
	whichset = [0 for i in range(n + 1)]
	for i in range(k):
		therow = input().split()
		num = int(therow[0])
		theset = set()
		for j in range(num):
			nextval = int(therow[j + 1])
			theset.add(nextval)
			whichset[nextval] = i
		seta.append(theset)
	answer = 0
	for i in range(k):
		if n in seta[i]:
			if len(seta[i]) > 1:
				answer = 1
			else:
				answer = 0
			seta[i].remove(n)
			n = n - 1
			break
	while n >= 1:
		setnum = whichset[n]
		if len(seta[setnum]) == 1:
			answer = answer + 1
			seta[setnum].remove(n)
			n = n - 1
		else:
			if len(seta[setnum]) == n:
				answer = answer + 1
				n = 0
				break
			else:
				seta[setnum].remove(n)
				n = n - 1
			if len(seta[setnum]) == 0:
				answer = answer + 1
			else:
				answer = answer + 2
	print(answer)


(n, k) = [int(c) for c in input().split()]
a = []
for i in range(k):
	ak = [int(c) for c in input().split()]
	a.append(ak[1:])
total = k - 1
for ak in a:
	if ak[0] == 1:
		j = 1
		while j <= len(ak) - 1:
			if ak[j] != ak[j - 1] + 1:
				break
			j += 1
		total += 2 * (len(ak) - j)
	else:
		total += 2 * (len(ak) - 1)
print(total)


def open_doll(i):
	global opened, time
	opened[doll_num[i]] = i + 1
	time += doll_size[doll_num[i]] - 1 - doll_ind[i]
(n, k) = [int(i) for i in input().split(' ')]
(doll_num, doll_ind, doll_size) = ([0] * n, [0] * n, [0] * k)
opened = [0] * k
time = 0
for i in range(k):
	s_doll = [int(i) for i in input().split(' ')]
	doll_size[i] = s_doll[0]
	for j in range(s_doll[0]):
		doll_num[s_doll[j + 1] - 1] = i
		doll_ind[s_doll[j + 1] - 1] = j
for i in range(n - 1):
	if doll_num[i] != doll_num[i + 1]:
		if not opened[doll_num[i]]:
			open_doll(i)
		if not opened[doll_num[i + 1]]:
			open_doll(i + 1)
		if opened[doll_num[i + 1]] == i + 2 and doll_ind[i + 1] != 0:
			opened[doll_num[i + 1]] = i + 1
			time += 1
		time += 1
	elif doll_num[i] == doll_num[i + 1] and opened[doll_num[i]] > 0 and (opened[doll_num[i]] < i + 2):
		time += 1
print(time)


(n, k) = map(int, input().split())
for i in range(k):
	a = list(map(int, input().split()))
	if a[1] == 1:
		have = len(a) - 1
		for j in range(1, len(a)):
			if a[j] != j:
				have = j - 1
				break
		want = n - 1 - (have - 1)
		print(want - (k - 1) + want)
		break


(n, m) = map(int, input().split(' '))
luck = 0
total = 2 * n - 1 - m
for j in range(m):
	arr = input().split(' ')
	for i in range(1, int(arr[0]) + 1):
		arr[i] = int(arr[i])
	arr.pop(0)
	if arr[0] == 1:
		prev = 0
		potential = -1
		for i in arr:
			if i == prev + 1:
				potential += 1
				prev = i
			else:
				break
		luck += potential
print(total - luck * 2)


(n, k) = [int(x) for x in input().split()]
parents = [i for i in range(n + 1)]
children = [i for i in range(n + 1)]
for i in range(k):
	temp = [int(x) for x in input().split()]
	if temp[0] > 1:
		for j in range(1, temp[0]):
			children[temp[j + 1]] = temp[j]
			parents[temp[j]] = temp[j + 1]
sc = 0
uc = 0
if n == 1:
	print(0)
	exit(0)

def find_root(i):
	while parents[i] != i:
		i = parents[i]
	return i

def split(root, nil):
	sc = 0
	i = root
	while i != nil:
		parents[children[i]] = children[i]
		(children[i], i) = (i, children[i])
		sc += 1
	return sc
start = 1
if parents[1] == 2:
	i = 2
	while parents[i] == i + 1:
		i += 1
	start = i
	if parents[i] != i:
		root = find_root(i)
		sc += split(root, i)
for i in range(start, n + 1):
	if parents[i] != i:
		root = find_root(i)
		sc += split(root, i)
for i in range(1, n):
	if parents[i] == i:
		uc += 1
print(sc + uc)


def judge(_list: list) -> int:
	number = -1
	for i in range(len(_list)):
		if int(_list[i]) == i + 1:
			number += 1
		else:
			pass
	return number
(mat_num, chain_num) = input().split(' ')
(mat_num, chain_num) = (int(mat_num), int(chain_num))
chain = []
for i in range(chain_num):
	chain.append([])
	temp_list = input().split(' ')
	for j in range(int(temp_list[0])):
		chain[-1].append(temp_list[1])
		del temp_list[1]
for i in range(chain_num):
	if judge(chain[i]) < 1:
		pass
	else:
		temp_count = len(chain[i]) - judge(chain[i])
		chain[i] = []
		for j in range(temp_count):
			chain[i].append(1)
time = 0
count = 0
for i in range(chain_num):
	time += len(chain[i]) - 1
	count += len(chain[i])
print(time + count - 1)


(n, k) = list(map(int, input().split(' ')))
for i in range(k):
	x = input().split(' ')
	if x[1] == '1':
		y = [int(j) for j in x[1:]] + [0]
		z = 0
		while y[z + 1] == z + 2:
			z += 1
print(2 * n - k - 1 - 2 * z)


(n, m) = map(int, input().split())
chains = [[int(x) for x in input().split()][1:] for i in range(m)]
for chain in chains:
	for i in range(len(chain)):
		if chain[i] == i + 1:
			if i != 0:
				n -= 2
		else:
			break
	n += len(chain) - 1
print(n - 1)


from io import BytesIO, IOBase
from math import sqrt
import sys, os
from os import path
inpl = lambda : list(map(int, input().split()))
inpm = lambda : map(int, input().split())
inpi = lambda : int(input())
inp = lambda : input()
(rev, ra, l) = (reversed, range, len)
P = print
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def B(n):
	return bin(n).replace('0b', '')

def factors(n):
	arr = []
	for i in ra(2, int(sqrt(n)) + 1):
		if n % i == 0:
			arr.append(i)
			if i * i != n:
				arr.append(int(n / i))
	return arr

def dfs(arr, n):
	pairs = [[i + 1] for i in ra(n)]
	vis = [0] * (n + 1)
	for i in ra(l(arr)):
		pairs[arr[i][0] - 1].append(arr[i][1])
	pairs[arr[i][1] - 1].append(arr[i][0])
	comp = []
	for i in ra(n):
		stack = [pairs[i]]
		temp = []
		if vis[stack[-1][0]] == 0:
			while len(stack) > 0:
				if vis[stack[-1][0]] == 0:
					temp.append(stack[-1][0])
					vis[stack[-1][0]] = 1
					s = stack.pop()
					for j in s[1:]:
						if vis[j] == 0:
							stack.append(pairs[j - 1])
				else:
					stack.pop()
			comp.append(temp)
	return comp
from bisect import bisect_left as bl
from bisect import bisect_right as br
import sys, operator, math, operator
from collections import Counter
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
import random
(n, k) = inpm()
li = []
for i in ra(k):
	li.append(inpl()[1:])
(part, tot) = (0, 0)
for i in ra(k):
	temp = li[i]
	if temp[0] == 1:
		(take, c) = (0, 1)
		while c < l(temp) and temp[c] == 1 + temp[c - 1]:
			c += 1
			take += 1
		part += l(temp) - (take + 1)
	else:
		part += l(temp) - 1
P(n - (take + 1) + part)


(n, k) = map(int, input().split())
time = 0
part = 0
for i in range(k):
	m = list(map(int, input().split()))
	p = m[0]
	d = set(m[1:])
	cou = 0
	if 1 in d:
		for i in range(1, p):
			if m[i] == m[i + 1] - 1:
				cou += 1
			else:
				break
		part += p - cou
		time += p - cou - 1
	else:
		part += p
		time += p - 1
time += part - 1
print(time)


(n, k) = map(int, input().split())
actions = 0
for chain_idx in ' ' * k:
	chain = [int(x) for x in input().split()[1:]]
	if 1 not in chain:
		actions += len(chain) * 2 - 1
	else:
		pops = len(chain)
		for (i, v) in enumerate(chain):
			if v == i + 1:
				pops -= 1
			else:
				break
		actions += pops * 2
if k == 1:
	print(0)
else:
	print(actions)


def read_data():
	(n, k) = map(int, input().split())
	As = []
	m = 0
	for i in range(k):
		ma = list(map(int, input().split()))
		As.append(ma[1:])
		m += ma[0] - 1
	return (n, k, As, m)

def solve(n, k, As, m):
	As.sort()
	steps = n + 1 + m
	mat1 = As[0]
	for (i, mi) in enumerate(mat1, 1):
		if i == mi:
			steps -= 2
		else:
			break
	return steps
(n, k, As, m) = read_data()
print(solve(n, k, As, m))


(n, k) = map(int, input().split())
a = 0
for _ in ' ' * k:
	(*l,) = map(int, input().split())
	for i in range(1, l[0]):
		if l[i] == i and l[i + 1] == i + 1:
			a += 2
print(2 * n - k - 1 - a)


(n, m) = map(int, input().split())
res = 2 * n - m + 1
for i in range(m):
	arr = list(map(int, input().split()))
	for j in range(1, len(arr)):
		if arr[j] == j:
			res -= 2
print(res)


def check(a):
	s = 0
	for i in range(1, len(a)):
		if a[i] - 1 == a[i - 1]:
			s += 1
		else:
			return s * 2
	return s * 2
inp = list(map(int, input().split()))
(n, k) = (inp[0], inp[1])
s = 0
for line in range(k):
	inp = list(map(int, input().split()))
	if inp[1] == 1:
		s -= check(inp[1:])
	s += inp[0] - 1
s += n - 1
print(s)


(n, k) = map(int, input().split())
a = 2 * n - k + 1
for _ in range(k):
	x = list(map(int, input().split()))[1:]
	for i in range(len(x)):
		if i + 1 == x[i]:
			a -= 2
print(a)


from collections import Counter, defaultdict, deque
from math import *
import sys
input = sys.stdin.readline

def solve():
	(n, k) = [int(x) for x in input().split()]
	res = n - k + n - 1
	for m in range(k):
		p = 0
		cur = [int(x) for x in input().split()]
		if cur[1] == 1:
			i = 1
			while i < cur[0]:
				if cur[i] == cur[i + 1] - 1:
					res -= 2
				else:
					break
				i += 1
	print(res)
tt = 1
for test in range(tt):
	solve()


(n, m) = map(int, input().split())
for i in range(m):
	a = list(map(int, input().split()))
	if a[1] == 1:
		(cnt, j) = (0, 2)
		while j <= a[0] and a[j] == a[j - 1] + 1:
			cnt += 1
			j += 1
print((n - m - cnt) * 2 + (m - 1))


inp = input().split()
n = int(inp[0])
k = int(inp[1])
out = 0
for i in range(0, k):
	inp = list(map(int, input().split()))
	for j in range(len(inp) - 1, 0, -1):
		out += 1
		if inp[j] == j:
			break
		out += 1 if j > 1 else 0
out -= 1
print(out)


(n, k) = map(int, input().split())
y = 0
for i in range(k):
	xs = list(map(int, input().split()))
	(m, xs) = (xs[0], xs[1:])
	if xs[0] == 1:
		j = 0
		while j < m and xs[j] == j + 1:
			j += 1
		m -= j
		y += m
		y += m
	else:
		y += m - 1
		y += m
print(y)


(N, K) = map(int, input().split())
ret = 0
for k in range(K):
	nums = list(map(int, input().split()))[1:]
	group = len(nums)
	if nums[0] == 1:
		for j in range(len(nums) - 1):
			if nums[j] + 1 == nums[j + 1]:
				group -= 1
			else:
				break
	ret += group - 1
ret += K + ret - 1
print(ret)


parts = 0
ans = 0
(a, b) = map(int, input().split(' '))
for i in range(b):
	shw = list(map(int, input().split(' ')))
	ded = shw[0]
	pat = shw[1:]
	if pat[0] == 1:
		ones = 0
		for i in range(ded):
			if pat[i] == i + 1:
				ones += 1
	parts += ded - 1
print(parts + a - 1 - (ones - 1) * 2)


(n, k) = map(int, input().split())
arr = []
for i in range(k):
	arr.append([int(x) for x in input().split()][1:])
ans = 0
rozobrane = []
for row in arr:
	tmp = 0
	for i in range(len(row) - 1):
		if row[0] == 1 and row[i] == row[i + 1] - 1:
			tmp += 1
		else:
			break
	rozobrane.append(len(row) - tmp - 1)
print(sum(rozobrane) * 2 + k - 1)


(n, k) = map(int, input().split(' '))
i = 0
res = k
sh = 0
while i < k:
	i += 1
	mas = list(map(int, input().split(' ')))[1:]
	if mas[0] == 1:
		j = 0
		while j < len(mas) - 1:
			if mas[j] + 1 != mas[j + 1]:
				break
			else:
				j += 1
		sh += len(mas) - j - 1
		res += len(mas) - j - 1
	else:
		res += len(mas) - 1
		sh += len(mas) - 1
print(sh + res - 1)


def main():
	(n, k) = list(map(int, input().split()))
	res = 0
	for _ in range(k):
		tmp = list(map(int, input().split()))
		if tmp[1] == 1:
			tmp[0] = 0
			for (i, x) in enumerate(tmp):
				if i != x:
					res += (len(tmp) - i) * 2
					break
		else:
			res += len(tmp) * 2 - 3
	print(res)

def __starting_point():
	main()
__starting_point()


n1 = []
n1 = input('').split(' ')
n = int(n1[0])
k = int(n1[1])
x = int(0)
y = int(-1)
z = int(0)
breakc = int(0)
addc = int(0)
x = []
f = int(0)
for i in range(0, k):
	x = input('').split(' ')
	y = -1
	if f != 1:
		for j in range(1, int(x[0]) + 1):
			z = int(x[j])
			if z == 1:
				y = 1
				for k in range(j + 1, int(x[0]) + 1):
					z = int(x[k])
					if z == y + 1:
						addc += 1
						y = z
					else:
						f = 1
					if k == int(x[0]):
						f = 1
					if f == 1:
						x[0] = int(x[0]) - addc
						break
	breakc += int(x[0]) - 1
print(breakc + (n - addc) - 1)


(n, k) = map(int, input().split())
m = [list(map(int, input().split())) for _ in range(k)]
for row in m:
	if row[1] == 1:
		for idx in range(1, row[0] + 1):
			if row[idx] != idx:
				idx -= 1
				break
		break
ans = n - 1 + (n - k) - 2 * (idx - 1)
print(ans)


(n, k) = list(map(int, input().split()))
time = n - 1
chains = []
for i in range(k):
	chain = list(map(int, input().split()))
	time += chain[0] - 1
	chain = chain[1:]
	if chain[0] == 1:
		for y in range(1, len(chain)):
			if chain[y] != chain[y - 1] + 1:
				time -= y - 1 + (y - 1)
				break
			elif y == len(chain) - 1 and chain[y] == chain[y - 1] + 1:
				time -= y + y
	chains.append(chain)
print(time)


(n, k) = input().split()
n = int(n)
k = int(k)
A = [[]]
b = []
for i in range(k):
	b = input().split()
	b.append(0)
	b = [int(x) for x in b]
	A.append(b)
ans = 0
f = 0
for i in range(1, k + 1):
	lenA = 0
	for h in A[i]:
		if h > 0:
			lenA += 1
	lenA -= 1
	lenODD = 0
	if lenA % 2:
		lenODD = 1
	else:
		lenODD = 0
	if A[i][1] != 1:
		ans += lenA - 1
	else:
		j = 2
		if lenA >= 2:
			while A[i][j] - A[i][j - 1] == 1 and A[i][j]:
				j += 1
			ans += lenA - (j - 2) - 1
			f = j - 2
ans += n - f - 1
print(ans)


d = input().split()
n = int(d[0])
m = int(d[1])
r = 0
q = 0
for i in range(m):
	s = [int(x) for x in input().split()][1:]
	if s[0] != 1:
		r += len(s) - 1
		q += len(s)
	elif len(s) <= 1:
		q += 1
	else:
		w = 0
		while w + 1 < len(s) and s[w] == s[w + 1] - 1:
			w += 1
		q += len(s) - w
		r += len(s) - w - 1
print(r + q - 1)


def count_continuous(a):
	total = 0
	for i in range(len(a) - 1):
		if a[i] + 1 == a[i + 1]:
			total += 1
		else:
			break
	return total

def main():
	(n, k) = [int(t) for t in input().split()]
	done = 0
	for _ in range(k):
		a = [int(t) for t in input().split()]
		if a[1] == 1:
			done = count_continuous(a[1:])
	attach = n - 1 - done
	detach = n - k - done
	cost = attach + detach
	print(cost)
main()


(n, m) = [int(x) for x in input().split()]
a = []
for i in range(m):
	a.append([int(x) for x in input().split()][1:])
b = []
curt = 0
for i in a:
	j = 0
	b.append([])
	while j < len(i) and i[j] == j + 1:
		j += 1
	if j != 0:
		b[-1] = [j]
	b[-1] += [1] * (len(i) - j)
	curt += len(b[-1])
fin = curt - 1
for i in b:
	fin += len(i) - 1
print(fin)


(n, m) = (int(x) for x in input().split())
coolLen = 1
res = 0
for x in range(m):
	cur = [int(x) for x in input().split()]
	k = cur[0]
	f = cur[1]
	if cur[1] != 1:
		res += len(cur) - 2
	else:
		cur = cur[1:]
		for i in range(len(cur) - 1):
			if cur[i] + 1 != cur[i + 1]:
				coolLen = i + 1
				break
		else:
			coolLen = len(cur)
		res += len(cur) - coolLen
res += n - 1 - (coolLen - 1)
print(res)


def main():
	ts = input().split()
	n = int(ts[0])
	k = int(ts[1])
	ans = 0
	b = None
	for _ in range(k):
		ts = [int(t) for t in input().split()]
		ts = ts[1:]
		i = 0
		while i < len(ts) and ts[i] == i + 1:
			i += 1
		if i == 0:
			ans += len(ts) - 1
		else:
			assert b == None
			b = i
			ans += len(ts) - i
	print(ans + n - b)
main()


__author__ = 'Andrey'
(n, k) = map(int, input().split())
ans = 0
for q in range(k):
	chain = list(map(int, input().split()))[1:]
	if chain[0] != 1:
		ans += len(chain) - 1
	else:
		k = 1
		while k < len(chain) and chain[k] == chain[k - 1] + 1:
			k += 1
		ans += len(chain[k - 1:]) - 1
print(ans + n - 1 - k + 1)


(n, m) = map(int, input().split())
c = {}
r = 0
for _ in range(m):
	t = list(map(int, input().split()))
	c[t[1]] = t[2:]
d = list(c.keys())
for v in d:
	if v == 1:
		if c[v]:
			x = 0
			for i in range(len(c[v])):
				if c[v][i] != i + 2:
					break
				x += 1
			r += len(c[v]) - x
	else:
		r += len(c[v])
print(len(d) + r * 2 - 1)


(n, k) = map(int, input().split())
arr = []
gc = 0
cus = 0
for i in range(k):
	arr = list(map(int, input().split())) + [10 ** 5 + 2]
	c = 0
	if arr[1] == 1:
		for j in range(1, arr[0] + 1):
			if arr[j] + 1 == arr[j + 1]:
				c += 1
			else:
				break
	cus += arr[0] - c
	gc += arr[0] - c - 1
print(gc + cus - 1)


def arr_inp(n):
	if n == 1:
		return [int(x) for x in stdin.readline().split()]
	elif n == 2:
		return [float(x) for x in stdin.readline().split()]
	else:
		return [str(x) for x in stdin.readline().split()]
from sys import stdin
(n, k) = arr_inp(1)
(arr, ans) = ([arr_inp(1) for i in range(k)], 0)
for i in range(k):
	for j in range(2, arr[i][0] + 1):
		if arr[i][j] == j and arr[i][j - 1] == j - 1:
			continue
		else:
			ans += arr[i][0] - (j - 2) - 1
			break
print(ans + ans + (k - 1))


(n, k) = map(int, input().split())
matr = []
for _ in range(k):
	matr.append(list(map(int, input().split()))[1:])
maxSeq = 0
for i in matr:
	if i[0] == 1:
		for j in range(1, len(i)):
			if i[j] != j + 1:
				maxSeq = j
				break
		else:
			maxSeq = len(i)
		break
m = n - maxSeq + 1
print(m - 1 + m - k)


(n, k) = map(int, input().split())
cnt = 0
ans = 0
for i in range(k):
	a = list(map(int, input().split()))
	if a[1] == 1:
		for j in range(2, len(a)):
			if a[j] > a[j - 1] + 1:
				ind = j
				break
		else:
			cnt += 1
			continue
		cnt += 1 + len(a) - ind
		ans += len(a) - ind
	else:
		cnt += len(a) - 1
		ans += len(a) - 2
print(ans + cnt - 1)


(N, K) = tuple(map(int, input().split(' ')))
chains = []
for i in range(K):
	line = input().split(' ')[1:]
	chains += [list(map(int, line))]
num_small_chains = K

def len_prefix(l):
	if len(l) == 0:
		return 1
	for i in range(len(l)):
		if l[i] != i + 1:
			return i
	return len(l)
cost = 0
total_components = 0
for chain in chains:
	components = len(chain)
	if chain[0] == 1:
		components -= len_prefix(chain) - 1
	cuts = components - 1
	cost += cuts
	total_components += components
cost += total_components - 1
print(cost)


(n, k) = map(int, input().split())
m = [list(map(int, input().split())) for _ in range(k)]
x = [0] * (n + 1)
y = [0] * (n + 1)
res = 0
for (i, row) in enumerate(m):
	for (j, v) in enumerate(row):
		if j == 0:
			continue
		x[v] = j
		y[v] = i
no = 1
for (i, v) in enumerate(m[y[1]]):
	if i < 2:
		continue
	if v != m[y[1]][i - 1] + 1:
		no = i - 1
		break
	no = i
res += n - no
for (i, row) in enumerate(m):
	if i != y[1]:
		res += row[0] - 1
	else:
		res += row[0] - x[no]
print(res)


(n, k) = list(map(int, input().split(' ')))
cost = 0
for i in range(k):
	inp = list(map(int, input().split(' ')))
	m = inp[0]
	a = inp[1:]
	if a[0] != 1:
		cost += m * 2 - 1
	else:
		for j in range(m):
			if a[j] == j + 1:
				continue
			j -= 1
			break
		cost += (m - j) * 2 - 1
print(cost - 1)


def func(mas, n, m):
	flag1 = 0
	flag2 = n - 1
	i = 0
	j = 1
	while i < m:
		if mas[i][1] == 1:
			tmp = mas[i][0] - 1
			while j < mas[i][0]:
				if mas[i][j] == mas[i][j + 1] - 1:
					flag2 -= 1
					tmp -= 1
				else:
					break
				j += 1
			flag1 += tmp
		else:
			flag1 = flag1 + mas[i][0] - 1
		i += 1
	return flag1 + flag2
(n, m) = map(int, input().split())
mas = [[] for i in range(m)]
h = 0
for i in mas:
	s = input()
	p = s.split()
	while h < len(p):
		i.append(int(p[h]))
		h += 1
	h = 0
print(func(mas, n, m))


(n, m) = map(int, input().split())
d = []
for i in range(m):
	l = list(map(int, input().split()))
	l = l[1:]
	d.append(l)
time = 0
kusok = 0
flag = False
for i in range(m):
	if d[i][0] == 1:
		for j in range(1, len(d[i])):
			if d[i][j] - 1 == d[i][j - 1]:
				kusok += 1
				flag = True
			else:
				break
	if flag:
		break
kusok1 = 0
for i in range(m):
	time += len(d[i]) - 1
	kusok1 += len(d[i])
print(time + (kusok1 - 1) - kusok * 2)


(n, k) = map(int, input().split())
l = 0
g = k
for i in range(k):
	(s, *x) = map(int, input().split())
	if x[0] == 1:
		for i in range(s):
			if x[i] != i + 1:
				i -= 1
				break
		i += 1
		break
ans = n - 1 + (n - k) - 2 * (i - 1)
print(ans)


(n, k) = map(int, input().split())
for i in range(k):
	b = list(map(int, input().split()))[1:]
	b.append(0)
	if b[0] != 1:
		continue
	x = 0
	while b[x + 1] == x + 2:
		x += 1
print(2 * n - k - 2 * x - 1)