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

题目大意:

给你1..n这n个数构成的k个序列。每次操作可以把长度为k(k>=2)的序列分为 前k-1 和 最后一个数 两个序列,或者将一个数的序列缀在另一个序列之后。

求最少多少次操作,使得序列变为123...n?

找出最大的i,使得1..i在原始某一序列前缀处。

#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm> using namespace std; const int maxn=; int mat[maxn+];
int m[maxn+][]; int main()
{
int n,k;
scanf("%d%d",&n,&k);
m[][]=;
for(int i=,mi;i<=k;i++)
{
scanf("%d",&mi);
m[i][]=m[i-][]+;
m[i][]=m[i][]+mi-;
for(int j=m[i][];j<=m[i][];j++)
scanf("%d",mat+j);
} int a=,b=;
for(int i=;i<=k;i++)
{
int st=m[i][];
int en=m[i][];
int j=st;
for(;j<=en;j++)
{
if(mat[j]==j-st+)
a++;
else
break;
}
for(;j<=en;j++)
b++;
if(mat[st]!=)
b--;
} printf("%d\n",b+n-a);
return ;
}

CodeForces - 556C Case of Matryoshkas (水题)的更多相关文章

  1. Codeforces Round #310 (Div. 1) A. Case of Matryoshkas 水题

    C. String Manipulation 1.0 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  2. CodeForces - 556C Case of Matryoshkas

    //////////////////////////////////////////////////////////////////////////////////////////////////// ...

  3. codeforces 556C. Case of Matryoshkas 解题报告

    题目链接:http://codeforces.com/contest/556/problem/C 题目意思:有 n 个数(1,2,...,n)组成 k 条链.第 i 条链由 mi 个数组成.每一秒只可 ...

  4. CodeForces 489B BerSU Ball (水题 双指针)

    B. BerSU Ball time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. codeforces 577B B. Modulo Sum(水题)

    题目链接: B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树

    A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...

  7. codeforces 696A Lorenzo Von Matterhorn 水题

    这题一眼看就是水题,map随便计 然后我之所以发这个题解,是因为我用了log2()这个函数判断在哪一层 我只能说我真是太傻逼了,这个函数以前听人说有精度问题,还慢,为了图快用的,没想到被坑惨了,以后尽 ...

  8. CodeForces 589I Lottery (暴力,水题)

    题意:给定 n 和 k,然后是 n 个数,表示1-k的一个值,问你修改最少的数,使得所有的1-k的数目都等于n/k. 析:水题,只要用每个数减去n/k,然后取模,加起来除以2,就ok了. 代码如下: ...

  9. Codeforces Gym 100286G Giant Screen 水题

    Problem G.Giant ScreenTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/con ...

随机推荐

  1. python selenium框架的Xpath定位元素

    我们工作中经常用到的定位方式有八大种:id name class_name tag_name link_text partial_link_text xpath css_selector 本篇内容主要 ...

  2. 线程池ThreadPoolExecutor的使用方法

    方法我们通过继承Thread类和实现runnable接口或者callable接口三种方式实现. 继承Thread类实际上也是实现了runnable接口,被继承的类主要是实现run()方法,通过star ...

  3. vue通过控制boolean值来决定是否添加class类名

    vue通过控制boolean值来决定是否添加class类名

  4. 【前端】 在前端利用数学函数知识+box-shadow解波浪图形

    序 今天正在刷数学函数相关题目,刷到了下面这篇文章,哇哦-有意思. 利用cos和sin实现复杂的曲线.传送门在下面. CSS 技巧一则 -- 在 CSS 中使用三角函数绘制曲线图形及展示动画 正巧在复 ...

  5. C# VS2019 WebService创建与发布,并部署到Windows Server 2012R

    前言 上一次数据库灾备和性能优化后,数据库专家建议,在不扩容的情况下,客户端不能再频繁的扫描数据库了!一句惊醒梦中人,因为我也发现数据库越来越卡了,自从上个项目上线后,就出现了这个情况.后来分析其原因 ...

  6. Python用正则表达式匹配汉字

    Python用正则表达式匹配汉字 匹配多个汉字,不包括空格 import re res = re.match(r'[\u4E00-\u9FA5]+', '我是 汉字') print(res) # &l ...

  7. Ztree树增删改查菜单,遇到的问题总结

    一.引言 我今天做了一个Ztree树增删改查菜单的功能.其中遇到了很多坑爹的问题,和大家讲述一下. 二.代码展示 1.Ztree树前台代码 <%@ page language="jav ...

  8. NER

    写在前面:在初学nlp时的第一个任务——NER,尝试了几种方法,cnn+crf.lstm+crf.bert+lstm+crf,毫无疑问,最后结果时Bert下效果最好. 1.关于NER: NER即命名实 ...

  9. 关于ios沙盒

    沙盒下主要有四个文件夹:document,caches,tmp,library document 的路径  程序运行时生成的文件,这个文件不要存比较放大的文件,比如音频,视频类,因为这里的东西会被上传 ...

  10. 《Windows内核安全与驱动开发》 7.1&7.2&7.3 串口的过滤

    <Windows内核安全与驱动开发>阅读笔记 -- 索引目录 <Windows内核安全与驱动开发> 7.1&7.2&7.3 串口的过滤 一.设备绑定的内核API ...