#include using namespace std; using Int = long long; template inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template inline void chmax(T1 &a,T2 b){if(a struct PrimalDual{ struct edge{ Int to; T cap,cost; Int rev; edge(){} edge(Int to,T cap,T cost,Int rev):to(to),cap(cap),cost(cost),rev(rev){} }; T INF; vector > G; vector h,dist; vector prevv,preve; PrimalDual(){} PrimalDual(Int n,T INF):INF(INF),G(n),h(n),dist(n),prevv(n),preve(n){} void add_edge(Int from,Int to,T cap,T cost){ G[from].emplace_back(to,cap,cost,G[to].size()); G[to].emplace_back(from,0,-cost,G[from].size()-1); } T flow(Int s,Int t,T f,Int &ok){ T res=0; fill(h.begin(),h.end(),0); while(f>0){ using P = pair; priority_queue,greater

> que; fill(dist.begin(),dist.end(),INF); dist[s]=0; que.emplace(dist[s],s); while(!que.empty()){ P p=que.top();que.pop(); Int v=p.second; if(dist[v]dist[v]+e.cost+h[v]-h[e.to]){ dist[e.to]=dist[v]+e.cost+h[v]-h[e.to]; prevv[e.to]=v; preve[e.to]=i; que.emplace(dist[e.to],e.to); } } } if(dist[t]==INF) return ok=0; for(Int v=0;v<(Int)h.size();v++) h[v]+=dist[v]; T d=f; for(Int v=t;v!=s;v=prevv[v]) d=min(d,G[prevv[v]][preve[v]].cap); f-=d; res+=d*h[t]; for(Int v=t;v!=s;v=prevv[v]){ edge &e=G[prevv[v]][preve[v]]; e.cap-=d; G[v][e.rev].cap+=d; } } ok=1; return res; } }; template vector compress(vector v){ sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); return v; } template map dict(const vector &v){ map res; for(Int i=0;i<(Int)v.size();i++) res[v[i]]=i; return res; } //INSERT ABOVE HERE signed main(){ Int n,m; scanf("%lld %lld",&n,&m); vector ls(m),rs(m); for(Int i=0;i vs; for(Int x:ls) vs.emplace_back(x); for(Int x:rs) vs.emplace_back(x+1); vs.emplace_back(0); vs.emplace_back(1e8); vs=compress(vs); auto dc=dict(vs); Int d=vs.size(); const Int INF = 1e8; PrimalDual G(d,INF); for(Int i=0;i+1