#include using namespace std; typedef long long ll; typedef pair P; typedef pair P1; typedef pair 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;iz; for(int i=1;i<=m;i++){ scanf("%d%d",&x[i],&d[i]); mn=min(mn,x[i]); mn2=min(mn2,x[i]); mx=max(mx,x[i]); mx2=max(mx2,x[i]); int MN=INF,MX=-INF; for(int j=0;j=x[i]); else{ CL[MN] = max(CL[MN],x[i]); } if(MN>=x[i]){ AR[MX] = min(AR[MX],x[i]); } else if(MX<=x[i]); else{ CR[MX] = min(CR[MX],x[i]); } mn=min(mn,MN); mx=max(mx,MX); } int R = 0,ans = min(mx-mn+mx-mn2,mx-mn+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])); } SORT(z); int cur,ans2=0; for(int i=0;i=z[i+1].fi) continue; else { ans2+=abs(cur-z[i+1].fi); cur=z[i+1].fi;} } ans = min(ans,ans2); cout<