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开始连续的数字不用拆。一旦遇到不连续,则从这个不连续的娃娃开始后面的全部都要拆。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+5;
typedef long long ll;
using namespace std;
int a[maxn];
int main()
{
int n,k;
cin>>n>>k;
int m;
ll sum1=0,sum2=0;
for(int t=0;t<k;t++)
{
scanf("%d",&m);
int s=1;
int x,x1;
scanf("%d",&a[0]);
int flag=0;
if(a[0]==1)
{
flag=1;
}
for(int k=1;k<m;k++)
{
scanf("%d",&a[k]);
if(flag==1&&(a[k]-a[k-1]==1))
{
continue;
}
else
{
flag=0;
s++;
sum2++;
}
}
sum1+=s;
}
sum1=sum1-1+sum2;
if(sum1<0)
{
cout<<0<<endl;
}
else
cout<<sum1<<endl;
return 0;
}

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

  1. CodeForces - 556C Case of Matryoshkas

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

  2. CodeForces - 556C Case of Matryoshkas (水题)

    Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at ...

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

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

  4. codeforces 555a//Case of Matryoshkas// Codeforces Round #310(Div. 1)

    题意:1-n的数字,大的在小的后面,以这种规则已经形成的几个串,现在要转为一个串,可用的操作是在末尾拆或添加,问要操作几次? 模拟了很久还是失败,看题解才知道是数学.看来这种只要结果的题,模拟很不合算 ...

  5. 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

    题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...

  6. 「日常训练」Case of Matryoshkas(Codeforces Round #310 Div. 2 C)

    题意与分析(CodeForces 556C) 为了将所有\(n\)个娃娃编号递增地串在一起(原先是若干个串,每个串是递增的), 我们有两种操作: 拆出当前串中最大编号的娃娃(且一定是最右边的娃娃). ...

  7. [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)

    [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...

  8. 【35.37%】【codeforces 556C】Case of Matryoshkas

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

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

随机推荐

  1. using namespace cv

    在OpenCV中使用 using namespace cv 的作用: 在使用#include语句包含相应头文件后,使用下面语句即可包含相应的Opencv命名空间 using namespace cv; ...

  2. Java字符串split分割星号*等特殊字符问题(转)

    Java的split()方法分割字符串比较常用(见[Java]字符串以某特殊字符分割处理 ),但在有的时候,会遇到星号*等正则表达式中的特殊字符而无法分割的问题. 比如某需求,用户输入产品规格:厚*宽 ...

  3. 使用WCF实现消息推送

    1.协议 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...

  4. 1、Docker介绍

      Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化.容器完全使用沙箱机制,相互之间不会有任何接口 ...

  5. 12.1.2: How to Modify and Enable The Configurable Home Page Delivered Via 12.1.2 (Doc ID 1061482.1)

    In this Document   Goal Solution References       APPLIES TO:    Oracle Applications Framework - Ver ...

  6. 20145233《网络对抗》Exp9 Web安全基础实践

    20145233<网络对抗>Exp9 Web安全基础实践 实验问题思考 SQL注入攻击原理,如何防御? SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符 ...

  7. 溢出文本省略号表示的css实现及polyfill

    需求经常有需要对文字溢出进行处理,通常是在文字显示部分的末尾添加“...”等.如下:

  8. 朋友,请待你的朋友——BUG好一点!

    程序猿嘛,难免会被BUG缠身,我相信,没有一个程序猿在被BUG缠身时是感觉轻松的,消灭BUG一定是你最大的愿望.本周,我们团队的项目进入调试阶段,各种BUG层出不穷,眼看下个周就要进行项目答辩会,所以 ...

  9. (一)ElasticSearch-入门

    目录:一.前言二.安装三.索引四.搜索五.聚合六.分布式的特性 一.前言Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎.无论在开源还是专有领域,Lucene可以被 ...

  10. Devexpress treelist 控件属性大全

    属性列表 1.OptionsSelection: EnableAppearanceForcusedCell:选中的Cell的Appearance设置是否可用.默认为True: EnableAppear ...