#include<cstdio>
#include<algorithm>
#define N 1919810
using namespace std;
int n,k,len,las,cnt[N],tot,all,ans=2147483647;
struct cow{
int ps,typ;
bool operator<(const cow &a)const{return ps<a.ps;}
}a[N];
void add(int x){
if(all==k)ans=min(ans,len);
len+=a[x].ps-a[x-1].ps;
cnt[a[x].typ]++;
if(cnt[a[x].typ]==1)all++;
if(all==k)ans=min(ans,len);
}
void del(int x){
if(all==k)ans=min(ans,len);
len-=a[x+1].ps-a[x].ps;
cnt[a[x].typ]--;
if(cnt[a[x].typ]==0)all--;
if(all==k)ans=min(ans,len);
}
signed main(){
scanf("%d%d",&n,&k);
for(int i=1;i<=k;i++){
int fifi;
scanf("%d",&fifi);
for(int j=1;j<=fifi;j++)scanf("%d",&a[++tot].ps);a[tot].typ=i;
}
sort(a+1,a+n+1);
a[0].ps=a[1].ps;
int l=1,r=0;
while(r<n){
add(++r);
while(l<=r&&all==k)del(l++);
}
printf("%d",ans);
return 0;
}