#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 llfr[M],rrfr[M],llbk[M],rrbk[M]; void init() { REP(i,1,n) mnp[i]=INF, mxp[i]=-INF; REP(i,1,m) { int x,d,y; RI(x,d); if(mnp[x]==INF) { mnp[x] = x; mxp[x] = x; } while(d--) { RI(y); mnp[x] = min(mnp[x], y); 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) { llbk[i] = mnp[i]; rrbk[i] = mxp[i]; } else { llbk[i] = min(llbk[i+1], mnp[i]); rrbk[i] = max(rrbk[i+1], mxp[i]); } } REP(i,st,ed) { if(i==st) { rrfr[i] = mxp[i]; llfr[i] = mnp[i]; } else { rrfr[i] = max(rrfr[i-1], mxp[i]); llfr[i] = min(llfr[i-1], mnp[i]); } } //lrl REP(i,st,ed) { int lll = llfr[i]; int rrr = rrbk[st]; int tmp = (i-lll) + (rrr-lll); if(i+1<=ed) tmp += max(rrr-llbk[i+1], 0); re = min(re, tmp); } //rlr REP(i,st,ed) { int rrr = rrbk[i]; int lll = llfr[ed]; int tmp = (rrr-i) + (rrr-lll); if(i-1>=st) tmp += max(rrfr[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; }