#include #define REP(x,y,z) for(int x=y;x<=z;x++) #define FORD(x,y,z) for(int x=y;x>=z;x--) #define MSET(x,y) memset(x,y,sizeof(x)) #define FOR(x,y) for(__typeof(y.begin()) x=y.begin();x!=y.end();x++) #define F first #define S second #define MP make_pair #define PB push_back #define SZ size() #define M 7000005 #define INF 1000000000 void RI(){} template void RI( int& head, T&... tail ) { scanf("%d",&head); RI(tail...); } using namespace std; typedef long long LL; int n,m,st,ed; int mnp[M],mxp[M]; int ll[M],rr[M]; void init() { REP(i,1,n) mnp[i]=INF, mxp[i]=-INF; REP(i,1,m) { int x,d,y; RI(x,d); while(d--) { RI(y); if(y<=x) mnp[x] = min(mnp[x], y); if(y>=x) mxp[x] = max(mxp[x], y); } } st=0, ed=n-1; while(st=0 && mnp[ed]==INF) ed--; } int calc() { int re = (n-1)+(n-1); FORD(i,ed,st) { if(i==ed) ll[i] = mnp[i]; else ll[i] = min(ll[i+1], mnp[i]); } REP(i,st,ed) { if(i==st) rr[i] = mxp[i]; else rr[i] = max(rr[i-1], mxp[i]); } //rl //re = min(re, ed-st + max(ed-ll[st], 0)); //lr //re = min(re, ed-st + max(rr[ed]-st, 0)); //lrl REP(i,st,ed) { int lll = min(st, ll[i]); int rrr = max(rr[ed],ed); int tmp = (i-lll) + (rrr-lll); if(i+1<=ed) tmp += max(rrr-ll[i+1], 0); re = min(re, tmp); } //rlr REP(i,st,ed) { int rrr = max(ed, rr[i]); int lll = min(st, ll[st]); int tmp = (rrr-i) + (rrr-lll); if(i-1>=st) tmp += max(rr[i-1]-lll, 0); re = min(re, tmp); } return re; } int main() { while(~scanf("%d %d",&n,&m)) { init(); printf("%d\n",calc()); } return 0; }