void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, m; rd(n, m); auto xs=new int[](m), ls=new int[](m); auto ys=new int[][](m); foreach(i; 0..m){ rd(xs[i], ls[i]); ys[i]=readln.split.to!(int[]); } int mn=1_000_000_000; mn=min(mn, solve(n, m, xs, ls, ys)); foreach(i; 0..m){ xs[i]=(n-xs[i]-1); foreach(j; 0..ls[i]) ys[i][j]=(n-ys[i][j]-1); } mn=min(mn, solve(n, m, xs, ls, ys)); writeln(mn); } int solve(int n, int m, int[] xs, int[] ls, int[][] ys){ import std.algorithm; auto c=new int[](n); int l=n, r=-1; foreach(i; 0..m){ l=min(l, xs[i]); r=max(r, xs[i]); foreach(y; ys[i]){ l=min(l, y); r=max(r, y); if(yr) l=r; } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } } void wr(T...)(T x){ import std.stdio; foreach(e; x) write(e, " "); writeln(); }