#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; typedef pair<int,P> P1; typedef pair<P,P> P2; #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define mod 1000000007 #define fi first #define sc second #define rep(i,x) for(int i=0;i<x;i++) #define repn(i,x) for(int i=1;i<=x;i++) #define SORT(x) sort(x.begin(),x.end()) #define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end()) #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) int AL[7000005],BL[7000005],CL[7000005]; int AR[7000005],BR[7000005],CR[7000005]; int n,m,x[500005],d[500005]; int mn=INF,mx=-INF; int mn2=INF,mx2=-INF; int main(){ rep(i,7000005){ AR[i]=BR[i]=CR[i]=INF; } scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ scanf("%d%d",&x[i],&d[i]); mn=min(mn,x[i]); mx=max(mx,x[i]); int MN=INF,MX=-INF; for(int j=0;j<d[i];j++){ int a; scanf("%d",&a); MN=min(MN,a); MX=max(MX,a); } if(MN>=x[i]); else if(MX<=x[i]){ AL[MN] = max(AL[MN],x[i]); } else{ CL[MN] = max(CL[MN],x[i]); } if(MX>x[i]){ mn2 = min(mn2,x[i]); } if(MX<=x[i]); else if(MN>=x[i]){ AR[MX] = min(AR[MX],x[i]); } else{ CR[MX] = min(CR[MX],x[i]); } if(MN<x[i]){ mx2 = max(mx2,x[i]); } mn=min(mn,MN); mx=max(mx,MX); } int R = 0,ans = min(mx-mn+max(0,mx-mn2),mx-mn+max(0,mx2-mn)); for(int i=mn;i<=mx;i++){ ans = min(ans,mx-mn+mx-i+R); R = max(R,max(AL[i],CL[i])); } R = mx; for(int i=mx;i>=mn;i--){ ans = min(ans,i-mn+mx-mn+mx-R); R = min(R,min(AR[i],CR[i])); } cout<<ans<<endl; }