传送门:
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
#define ll long long
#define re register
const int N=1e6+10;
inline void read(int &a)
{
a=0;
int d=1;
char ch;
while(ch=getchar(),ch>'9'||ch<'0')
if(ch=='-')
d=-1;
a=ch^48;
while(ch=getchar(),ch>='0'&&ch<='9')
a=(a<<3)+(a<<1)+(ch^48);
a*=d;
}
struct note
{
int x,y;
}q[N];
int cnt;
inline bool cmp(note x,note y){return x.y<y.y;}
int p[65];
int main()
{
int n,m;
read(n);
read(m);
for(re int i=1;i<=m;i++)
{
int t;
read(t);
for(re int j=1;j<=t;j++)
{
read(q[++cnt].y);
q[cnt].x=i;
}
}
sort(q+1,q+1+n,cmp);
int h,t;
h=t=1;
cnt=0;
int ans=q[n].y-q[1].y;
for(re int i=1;i<=n;i++)
{
if(!p[q[i].x])
cnt++;
p[q[i].x]=q[i].y;
while(h<i&&q[h].y!=p[q[h].x])
h++;
if(cnt==m)
ans=min(ans,q[i].y-q[h].y);
}
printf("%d",ans);
return 0;
}