#include <bits/stdc++.h>

#define mp make_pair
#define snd second
#define fst first

using namespace std;

const int inf = 1E9 + 10, N = 3E5 + 10;

int d[N], order[N], n, k, sum, ans;
set < pair <int, int> > Set;
bool inans[N];

int main() {
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
		for (int j = i + i; j <= n; j += i) d[i]++, d[j]++, sum++;
	for (int i = 2; i <= n; i++) Set.insert(mp(d[i], i));
	memset(inans, 1, sizeof inans), sum -= k;
	if (sum < 0) return puts("No"), 0;
	while (sum) {
		set < pair <int, int> > :: iterator it = --Set.upper_bound(mp(sum, inf));
		sum -= it -> fst, inans[it -> snd] = 0;
//		printf("%d\n", it -> snd);
		for (int i = 2; i * i <= it -> snd; i++)
			if (inans[i]&&it -> snd % i == 0)
				Set.erase(mp(d[i], i)), d[i]--, Set.insert(mp(d[i], i));
		for (int i = it -> snd * 2; i <= n; i += it -> snd)
			if (inans[i])
				Set.erase(mp(d[i], i)), d[i]--, Set.insert(mp(d[i], i));
		Set.erase(it);
	}
	if (sum) return puts("No"), 0;
	puts("Yes");
	for (int i = 1; i <= n; i++)
		ans += inans[i];
	printf("%d\n", ans);
	for (int i = 1; i <= n; i++)
		if (inans[i]) printf("%d ", i);
	return 0;
}


#include <bits/stdc++.h>

#define mp make_pair
#define snd second
#define fst first

using namespace std;

const int inf = 1E9 + 10, N = 3E5 + 10;

int d[N], order[N], n, k, sum, ans;
set < pair <int, int> > Set;
bool inans[N];

int main() {
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
		for (int j = i + i; j <= n; j += i) d[i]++, d[j]++;
	for (int i = 1; i <= n; i++)
		sum += d[i], Set.insert(mp(d[i], i));
	if (sum < k) return puts("No"), 0;
	memset(inans, 1, sizeof inans);
	sum = sum / 2 - k;
	while (sum) {
		set < pair <int, int> > :: iterator it = --Set.upper_bound(mp(sum, inf));
		sum -= it -> fst, inans[it -> snd] = 0;
		for (int i = 1; i * i <= it -> snd; i++)
			if (it -> snd % i == 0)
				Set.erase(mp(d[i], i)), d[i]--, Set.insert(mp(d[i], i));
		for (int i = it -> snd * 2; i <= n; i += it -> snd)
			Set.erase(mp(d[i], i)), d[i]--, Set.insert(mp(d[i], i));
	}
	if (sum) return puts("No"), 0;
	puts("Yes");
	for (int i = 1; i <= n; i++)
		ans += inans[i];
	printf("%d\n", ans);
	for (int i = 1; i <= n; i++)
		if (inans[i]) printf("%d ", i);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int contrib[312345] = {0};
bool in_set[312345] = {0};

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> ans;
  int cnt = 0;
  for(int i=1; i<=n/2; i++){
    if(cnt + contrib[i] <= k){
      ans.push_back(i);
      in_set[i] = true;
      cnt += contrib[i];
      for(int j = i+i; j <= n; j+=i) contrib[j]++;
    }
    else {
      while(cnt + contrib[i] > k){
        cnt -= contrib[ans.back()];
        for(int j = ans.back()+ans.back(); j <= n; j+=ans.back()) contrib[j]--;
        in_set[ans.back()] = false;
        ans.pop_back();
      }
      ans.push_back(i);
      in_set[i] = true;
      cnt += contrib[i];
      for(int j = i+i; j <= n; j+=i) contrib[j]++;
    }
    if(cnt == k) break;
  }
  //cout << cnt << endl;
  priority_queue< pair<int, int> >  pq;
  for(int i= n/2+1; i<=n; i++){
    if(in_set[i]) continue;
    pq.push(make_pair(contrib[i], i));
  }
  while(!pq.empty()){
    while(!pq.empty() && cnt + pq.top().first > k) pq.pop();
    if(pq.empty()) break;
    cnt += pq.top().first;
    ans.push_back(pq.top().second);
    pq.pop();
  }
  //cout << cnt << endl;
  if(cnt != k){
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  cout << ans.size() << endl;
  for(int i=0; i<ans.size(); i++){
    if(i) cout << " ";
    cout << ans[i];
  }
  cout << endl;
}


#include <bits/stdc++.h>

using namespace std;

int contrib[312345] = {0};
bool in_set[312345] = {0};

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> ans;
  int cnt = 0;
  for(int i=1; i<=n; i++){
    if(cnt + contrib[i] <= k){
      ans.push_back(i);
      in_set[i] = true;
      cnt += contrib[i];
      for(int j = i+i; j <= n; j+=i) contrib[j]++;
    }
    else {
      while(cnt + contrib[i] > k){
        cnt -= contrib[ans.back()];
        for(int j = ans.back()+ans.back(); j <= n; j+=ans.back()) contrib[j]--;
        in_set[ans.back()] = false;
        ans.pop_back();
      }
      ans.push_back(i);
      in_set[i] = true;
      cnt += contrib[i];
      for(int j = i+i; j <= n; j+=i) contrib[j]++;
    }
    if(cnt == k) break;
  }
  priority_queue< pair<int, int> >  pq;
  for(int i= n/2+1; i<=n; i++){
    if(in_set[i]) continue;
    pq.push(make_pair(contrib[i], i));
  }
  while(!pq.empty()){
    while(!pq.empty() && cnt + pq.top().first > k) pq.pop();
    if(pq.empty()) break;
    cnt += pq.top().first;
    ans.push_back(pq.top().second);
  }
  if(cnt != k){
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  cout << ans.size() << endl;
  for(int i=0; i<ans.size(); i++){
    if(i) cout << " ";
    cout << ans[i];
  }
  cout << endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
template<class _Tp>_Tp gcd(_Tp a,_Tp b)
{return (b==0)?(a):(gcd(b,a%b));}
#define CT printf("\n--------------------\n");
const int inf=1000000000;
const ll Inf=1000000000000000000ll;
char buf[1<<23],*p1=buf,*p2=buf;
int getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?EOF:*p1++;}
int read(){
	int x=0,f=0;char c=getc();
	while(!isdigit(c))(c=='-')&&(f=1),c=getc();
	while(isdigit(c))x=x*10+(c&15),c=getc();
	return f?-x:x;
}
#define fir first
#define sec second
typedef pair<ll,int> pli;
bool cmp(pli A,pli B){
	return A.fir>B.fir;
}
const int N=300005;
int n;
pli a[N];
bool useless[N];
ll k,cnt;
int main(){
	n=read();k=read();
	for(int i=1;i<=n;++i)
		a[i].fir=0,a[i].sec=i;
	int m=1;
	for(;m<=n;++m){
		for(int j=m*2;j<=n;j+=m)
			a[j].fir++;
		cnt+=a[m].fir;
		if(cnt>=k)break;
	}
	if(cnt<k){
		puts("No");
		return 0;
	}
	sort(a+1,a+m+1,cmp);
	int t=m;
	for(int i=1;i<=m;++i){
		if(a[i].sec>m/2&&cnt-a[i].fir>=k){
			cnt-=a[i].fir;
			useless[a[i].sec]=1;
			t--;
		}
		if(cnt==k)break;
	}
	puts("Yes");
	printf("%d\n",t);
	for(int i=1;i<=m;++i){
		if(!useless[i])printf("%d ",i);
	}
	puts("");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int deg[300005];
bitset<300005> np,bye;

int main(){
    int n,k; cin>>n>>k;
    for(int i=1;i<=n;++i){
        for(int j=i*2;j<=n;j+=i)++deg[j];
    }
    np[0]=np[1]=1;
    for(int i=2;i<=n;++i){
        for(int j=i*2;j<=n;j+=i)np[j]=1;
    }
    int now=0;
    for(int i=2;i<=n;++i){
        now+=deg[i];
        if(now>=k){
            n=i;
            break;
        }
    }
    // cout<<"new n is "<<n<<endl;
    if(now<k)exit((cout<<"No"<<endl,0));
    set<pair<int,int>> ho;
    for(int i=2;i<=n;++i)if(!np[i])ho.insert({n/i,i});
    // cout<<ho.size()<<endl;
    while(now>k){
        auto it=--ho.upper_bound({now-k,1<<30});
        bye[it->second]=1;
        now-=it->first;
        ho.erase(it);
    }
    cout<<"Yes"<<endl;
    cout<<n-bye.count()<<endl;
    for(int i=1;i<=n;++i)if(!bye[i])cout<<i<<" ";
}


#include<bits/stdc++.h>
using namespace std;

int deg[300005];
bitset<300005> np,bye;

int main(){
    int n,k; cin>>n>>k;
    for(int i=1;i<=n;++i){
        for(int j=i*2;j<=n;j+=i)++deg[j];
    }
    np[0]=np[1]=1;
    for(int i=2;i<=n;++i){
        for(int j=i*2;j<=n;j+=i)np[j]=1;
    }
    int now=0;
    for(int i=2;i<=n;++i){
        now+=deg[i];
        if(now>=k){
            n=i;
            break;
        }
    }
    // cout<<"new n is "<<n<<endl;
    if(now<k)exit((cout<<"No"<<endl,0));
    set<pair<int,int>> ho;
    for(int i=2;i<=n;++i)if(!np[i])ho.insert({n/i,i});
    cout<<ho.size()<<endl;
    while(now>k){
        auto it=--ho.upper_bound({now-k,1<<30});
        bye[it->second]=1;
        now-=it->first;
        ho.erase(it);
    }
    cout<<"Yes"<<endl;
    cout<<n-bye.count()<<endl;
    for(int i=1;i<=n;++i)if(!bye[i])cout<<i<<" ";
}


#include <bits/stdc++.h>
using namespace std;

const int N = 3e5 + 5;

int n, k;
int d[N];
int rem;
bool dp[N][210];
bool np[N], del[N];
int pr[N], cnt;

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n >> k;

	for (int i = 1; i <= n; ++i) {
		for (int j = i + i; j <= n; j += i) d[j]++;
	}

	for (int i = 1; i <= n; ++i) d[i] += d[i - 1];

	bool found = false;
	for (int i = 1; i <= n; ++i) {
		if (d[i] >= k) {
			n = i;
			rem = d[i] - k;
			found = true;
			break;
		}
	}

	if (!found) return cout << "No\n", 0;

	np[0] = np[1] = 1;
	for (int i = 2; i <= n; ++i) if (!np[i]) {
		for (int j = i + i; j <= n; ++j) np[j] = 1;
		pr[++cnt] = i;
	}

	dp[0][0] = 1;
	for (int i = 1; i <= cnt; ++i) {
		int cur = n / pr[i];
		for (int j = 0; j <= rem; ++j) {
			dp[i][j] = dp[i - 1][j];
			if (j - cur >= 0) dp[i][j] |= dp[i - 1][j - cur];
		}
	}

	while(cnt > 0) {
		if (dp[cnt - 1][rem]) --cnt;
		else rem -= n / pr[cnt], del[pr[cnt]] = true, --cnt;
	}

	vector<int> vres;
	for (int i = 1; i <= n; ++i) if (!del[i]) vres.push_back(i);
	cout << "Yes\n" << vres.size() << endl;
	for (int val : vres) cout << val << ' '; cout << endl;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 3e5 + 5;

int n, k;
int d[N];
int rem;
bool dp[N][210];
bool np[N], del[N];
int pr[N], cnt;

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n >> k;

	for (int i = 1; i <= n; ++i) {
		for (int j = i + i; j <= n; j += i) d[j]++;
	}

	for (int i = 1; i <= n; ++i) d[i] += d[i - 1];

	bool found = false;
	for (int i = 1; i <= n; ++i) {
		if (d[i] >= k) {
			n = i;
			rem = d[i] - k;
			found = true;
			break;
		}
	}

	if (!found) return cout << "No\n", 0;

	np[0] = np[1] = 1;
	for (int i = 2; i <= n; ++i) if (!np[i]) {
		for (int j = i + i; j <= n; j += i) np[j] = 1;
		pr[++cnt] = i;
	}

	dp[0][0] = 1;
	for (int i = 1; i <= cnt; ++i) {
		int cur = n / pr[i];
		for (int j = 0; j <= rem; ++j) {
			dp[i][j] = dp[i - 1][j];
			if (j - cur >= 0) dp[i][j] |= dp[i - 1][j - cur];
		}
	}

	while(cnt > 0) {
		if (dp[cnt - 1][rem]) --cnt;
		else rem -= n / pr[cnt], del[pr[cnt]] = true, --cnt;
	}

	vector<int> vres;
	for (int i = 1; i <= n; ++i) if (!del[i]) vres.push_back(i);
	cout << "Yes\n" << vres.size() << endl;
	for (int val : vres) cout << val << ' '; cout << endl;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <complex>

#define sz(v) ((int)((v).size()))
#define all(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

const int INF = 0x3c3c3c3c;
const ll LINF = 1ll*INF*INF*2;

const int N = 1000001;
int n,m,x,y;
vector<pii> v;

ll dp[100][100100];
int main(void){
	scanf("%d%d", &n, &m);
	for(int i=0; i<n; i++) {
		scanf("%d%d", &x, &y);
		v.push_back({x, y});
	}
	sort(all(v));
	dp[0][0]=0;
	for(int j=1; j<=m; j++) dp[0][j]=LINF;
	for(int i=0; i<n; i++) {
		x=v[i].first, y=v[i].second;
		for(int j=1; j<=m; j++) {
			dp[i+1][j]=dp[i][j];
		}
		for(int d=0; x+y+d<=m; d++) {
			dp[i+1][min(m,x+y+d)]=min(dp[i+1][min(m,x+y+d)], d+dp[i][max(0, x-y-d-1)]);
		}

		for(int j=m-1; j>=0; j--) dp[i+1][j]=min(dp[i+1][j], dp[i+1][j+1]);
	}

	printf("%lld", dp[n][m]);
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <complex>

#define sz(v) ((int)((v).size()))
#define all(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

const int INF = 0x3c3c3c3c;
const ll LINF = 1ll*INF*INF*2;

const int N = 1000001;
int n,m,x,y;
vector<pii> v;

ll dp[100][100100];
int main(void){
	scanf("%d%d", &n, &m);
	for(int i=0; i<n; i++) {
		scanf("%d%d", &x, &y);
		v.push_back({x, y});
	}
	sort(all(v));
	dp[0][0]=0;
	for(int j=1; j<=m; j++) dp[0][j]=LINF;
	for(int i=0; i<n; i++) {
		x=v[i].first, y=v[i].second;
		for(int j=1; j<=m; j++) {
			dp[i+1][j]=dp[i][j];
		}
		if(x+y>m) {
			dp[i+1][m]=min(dp[i][max(0,x-y-1)], dp[i+1][m]);
		} else {
			for(int d=0; x+y+d<=m; d++) {
				dp[i+1][x+y+d]=min(dp[i][x+y+d], d+dp[i][max(0, x-y-d-1)]);
			}
		}

		for(int j=m-1; j>=0; j--) dp[i+1][j]=min(dp[i+1][j], dp[i+1][j+1]);
	}

	printf("%lld", dp[n][m]);
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <complex>

#define sz(v) ((int)((v).size()))
#define all(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

const int INF = 0x3c3c3c3c;
const ll LINF = 1ll*INF*INF*2;

const int N = 1000001;
int n,m,x,y;
vector<pii> v;

ll dp[100][100100];
int main(void){
	scanf("%d%d", &n, &m);
	for(int i=0; i<n; i++) {
		scanf("%d%d", &x, &y);
		v.push_back({x, y});
	}
	sort(all(v));
	dp[0][0]=0;
	for(int j=1; j<=m; j++) dp[0][j]=INF;
	for(int i=0; i<n; i++) {
		x=v[i].first, y=v[i].second;
		for(int j=1; j<=m; j++) {
			dp[i+1][j]=dp[i][j];
		}
		if(x+y>m) {
			dp[i+1][m]=min(dp[i][max(0,x-y-1)], dp[i+1][m]);
		} else {
			for(int d=0; x+y+d<=m; d++) {
				dp[i+1][x+y+d]=min(dp[i][x+y+d], d+dp[i][max(0, x-y-d-1)]);
			}
		}

		for(int j=m-1; j>=0; j--) dp[i+1][j]=min(dp[i+1][j], dp[i+1][j+1]);
	}

	printf("%lld", dp[n][m]);
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <complex>

#define sz(v) ((int)((v).size()))
#define all(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

const int INF = 0x3c3c3c3c;
const ll LINF = 1ll*INF*INF*2;

const int N = 1000001;
int n,m,x,y;
vector<pii> v;

ll dp[100][100100];
int main(void){
	scanf("%d%d", &n, &m);
	for(int i=0; i<n; i++) {
		scanf("%d%d", &x, &y);
		v.push_back({x, y});
	}
	sort(all(v));
	dp[0][0]=0;
	for(int j=1; j<=m; j++) dp[0][j]=LINF;
	for(int i=0; i<n; i++) {
		x=v[i].first, y=v[i].second;
		for(int j=1; j<=m; j++) {
			dp[i+1][j]=dp[i][j];
		}
		for(int d=0; d<=m; d++) {
			dp[i+1][min(m,x+y+d)]=min(dp[i+1][min(m,x+y+d)], d+dp[i][max(0, x-y-d-1)]);
		}

		for(int j=m-1; j>=0; j--) dp[i+1][j]=min(dp[i+1][j], dp[i+1][j+1]);
	}

	printf("%lld", dp[n][m]);
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define si signed
#define pb push_back
#define fi first
#define se second
#define endl '\n'
#define sc(x) scanf("%I64d", &x);
int n, m;
int A[100005];
int B[100005];
bool vis[100005];
int C[100005];
int tmp[100005];
bool cmp(int x, int y)
{
    if(A[x]-B[x]==A[y]-B[y])
       return A[x]>A[y];
    return A[x] - B[x] < A[y] - B[y];
}
bool cmp1(int x,int y)
{
        if(A[x]-B[x]==A[y]-B[y])
       return A[x]<A[y];
    return A[x]+B[x]<A[y]+B[y];
}
signed main()
{
    sc(n) sc(m)
    for (int i = 1; i <= n; i++)
    {
        sc(A[i]) sc(B[i])
        tmp[i]=B[i];
            C[i] = i;
    }
    sort(C + 1, C + 1 + n, cmp);
    int j = 1;
    int ans = 0;
    for (int i = 1; i <= m; i++)
    {
        if (i < max(A[C[j]] - B[C[j]], 1ll))
        {
            ans += A[C[j]] - B[C[j]] - i;
            B[C[j]] += A[C[j]] - B[C[j]] - i;
        }
        else if (i >= max(A[C[j]] - B[C[j]], 1ll) && i <= min(A[C[j]] + B[C[j]], m))
        {

        }
        else
        {
            j++;
            if(j>n&&A[C[j-1]]+B[C[j-1]]<=m){
                ans+=m-A[C[j-1]]-B[C[j-1]];
                break;
            }else if(j>n)break;
            i--;
        }
    }
     j=n;
    int ans1=0;
    for(int i=1;i<=n;i++)B[i]=tmp[i];
    sort(C+1,C+1+n,cmp1);

    for(int i=m;i>0;i--){
        if(i>min(A[C[j]]+B[C[j]],m)){
            ans1+=i-A[C[j]]-B[C[j]];
            B[C[j]]+=i-A[C[j]]-B[C[j]];
        }else if(i<max(1ll,A[C[j]]-B[C[j]])){
            j--;
            if(j==0&&A[C[1]]-B[C[1]]>1){
                ans1+=A[C[1]]-B[C[1]]-1;
                break;
            }else if(j==0)break;
            i++;
        }
    }
    cout << min(ans,ans1) << '\n';
}


#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <random>
#include <assert.h>
#include <memory.h>
#include <time.h>
#include <bitset>

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = (l); i < (r); i++)
#define repb(i, r, l) for (int i = (r); i > (l); i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)
#define rank qwertyuio
#define next dfghjk
#define plus fsghsf
#define minus ytryr

using namespace std;

inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; }
inline bool setmin(ll &x, ll y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(ll &x, ll y) { return (y > x) ? x = y, 1 : 0; }

const int N = 100000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const ld eps = 1e-12;
const double pi = atan2(0, -1);
const int ABC = 26;

int dp[N];

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    //ll TL = 10.95 * CLOCKS_PER_SEC;
    //clock_t time = clock();
    int n, m;
    cin >> n >> m; m--;
    int x[n], s[n];
    rep(i, 0, n) {
        cin >> x[i] >> s[i];
        x[i]--;
    }
    rep(i, 0, m + 1) {
        dp[i] = inf;
    }
    rep(i, 0, m + 1) {
        rep(j, 0, n) {
            int need = max(0, abs(i - x[j]) - s[j]);
            //int need1 = max(0, abs(0 - x[j]) - s[j]);
            //setmin(dp[i], max(need, need1));
            setmin(dp[i], need);
            int l = x[j] - s[j] - need;
            if (l - 1 >= 0) {
                setmin(dp[i], need + dp[l - 1]);
            } else {
                setmin(dp[i], need);
            }
        }
    }
    cout << dp[m] << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define si signed
#define pb push_back
#define fi first
#define se second
#define endl '\n'
#define sc(x) scanf("%I64d", &x);
int n, m;
int A[100005];
int B[100005];
int dp[100004];
si main()
{
    sc(n)sc(m)
    for(int i=1;i<=n;i++){
        sc(A[i])sc(B[i])
    }
    for(int i=1;i<=m;i++){
        dp[i]=1e18;
        for(int j=1;j<=n;j++){
            dp[i]=min(dp[i],min(max(max(abs(i-A[j])-B[j],0ll),(abs(1-A[j])-B[j])),dp[A[j]-abs(i-A[j])-1<0?0:A[j]-abs(i-A[j])-1]+max(abs(i-A[j])-B[j],0ll)));
        }
    }
    cout<<dp[m]<<'\n';
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define si signed
#define pb push_back
#define fi first
#define se second
#define endl '\n'
#define sc(x) scanf("%I64d", &x);
int n, m;
int A[100005];
int B[100005];
bool vis[100005];
int C[100005];
int tmp[100005];
bool cmp(int x, int y)
{
    return A[x] - B[x] < A[y] - B[y];
}
bool cmp1(int x,int y)
{
    return A[x]+B[x]<A[y]+B[y];
}
signed main()
{
    sc(n) sc(m)
    for (int i = 1; i <= n; i++)
    {
        sc(A[i]) sc(B[i])
        tmp[i]=B[i];
            C[i] = i;
    }
    sort(C + 1, C + 1 + n, cmp);
    int j = 1;
    int ans = 0;
    for (int i = 1; i <= m; i++)
    {
        if (i < max(A[C[j]] - B[C[j]], 1ll))
        {
            ans += A[C[j]] - B[C[j]] - i;
            B[C[j]] += A[C[j]] - B[C[j]] - i;
        }
        else if (i >= max(A[C[j]] - B[C[j]], 1ll) && i <= min(A[C[j]] + B[C[j]], m))
        {

        }
        else
        {
            j++;
            if(j>n&&A[C[j-1]]+B[C[j-1]]<=m){
                ans+=m-A[C[j-1]]-B[C[j-1]];
                break;
            }else if(j>n)break;
            i--;
        }
    }
     j=n;
    int ans1=0;
    for(int i=1;i<=n;i++)B[i]=tmp[i];
    sort(C+1,C+1+n,cmp1);

    for(int i=m;i>0;i--){
        if(i>min(A[C[j]]+B[C[j]],m)){
            ans1+=i-A[C[j]]-B[C[j]];
            B[C[j]]+=i-A[C[j]]-B[C[j]];
        }else if(i<max(1ll,A[C[j]]-B[C[j]])){
            j--;
            if(j==0&&A[C[1]]-B[C[1]]>1){
                ans1+=A[C[1]]-B[C[1]]-1;
                break;
            }else if(j==0)break;
            i++;
        }
    }
    cout << min(ans,ans1) << '\n';
}


#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
template<typename T> bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    vector<pint> lrs(N);
    REP(i, N)
    {
        int x, s;
        cin >> x >> s;
        lrs[i] = make_pair(x - s, x + s);
    }

    vector<int> dp(M * 2 + 1, M * 2 + 10);
    dp[0] = 0;
    priority_queue<pint, vector<pint>, greater<pint>> pq;
    pq.emplace(M * 2 + 10, M * 2 + 10);
    FOR(x, 1, M + 1)
    {
        for (auto lr : lrs)
        {
            int l = lr.first, r = lr.second;
            if (l <= x and x <= r)
            {
                if (mmin(dp[x], dp[max(l - 1, 0)])) pq.emplace(dp[x], r);
            }
            else
            {
                int c = min(abs(l - x), abs(r - x));
                int newl = max(0, l - c - 1);
                if (mmin(dp[x], dp[newl] + c)) pq.emplace(dp[x], r + c);
            }
        }
        while (pq.top().second < x) pq.pop();
        mmin(dp[x], pq.top().first);
    }
    // dbg(dp);
    cout << *min_element(dp.begin() + M, dp.end()) << endl;
}


#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
template<typename T> bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    vector<pint> lrs(N);
    REP(i, N)
    {
        int x, s;
        cin >> x >> s;
        lrs[i] = make_pair(x - s, x + s);
    }

    vector<int> dp(M + 1, M + 10);
    dp[0] = 0;
    priority_queue<pint, vector<pint>, greater<pint>> pq;
    pq.emplace(M + 10, M + 10);
    FOR(x, 1, M + 1) for (auto lr : lrs)
    {
        while (pq.top().second < x) pq.pop();
        mmin(dp[x], pq.top().first);
        int l = lr.first, r = lr.second;
        if (l <= x and x <= r)
        {
            if (mmin(dp[x], dp[max(l - 1, 0)])) pq.emplace(dp[x], r);
        }
        else
        {
            int c = min(abs(l - x), abs(r - x));
            int newl = max(0, l - c - 1);
            if (mmin(dp[x], dp[newl] + c)) pq.emplace(dp[x], r + c);
        }
    }
    // dbg(dp);
    cout << dp[M] << endl;
}


#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
template<typename T> bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    vector<pint> lrs(N);
    REP(i, N)
    {
        int x, s;
        cin >> x >> s;
        lrs[i] = make_pair(x - s, x + s);
    }

    vector<int> dp(M + 1, M);
    dp[0] = 0;
    priority_queue<pint, vector<pint>, greater<pint>> pq;
    pq.emplace(M, M + 1);
    FOR(x, 1, M + 1) for (auto lr : lrs)
    {
        while (pq.top().second < x) pq.pop();
        mmin(dp[x], pq.top().first);
        int l = lr.first, r = lr.second;
        if (l <= x and x <= r)
        {
            if (mmin(dp[x], dp[max(l - 1, 0)])) pq.emplace(dp[x], r);
        }
        else
        {
            int c = min(abs(l - x), abs(r - x));
            int newl = max(0, l - c - 1);
            if (mmin(dp[x], dp[newl] + c)) pq.emplace(dp[x], r + c);
        }
    }
    cout << dp[M] << endl;
}


#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
template<typename T> bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    vector<pint> lrs(N);
    REP(i, N)
    {
        int x, s;
        cin >> x >> s;
        lrs[i] = make_pair(x - s, x + s);
    }

    vector<int> dp(M * 2 + 1, M * 2 + 10);
    dp[0] = 0;
    priority_queue<pint, vector<pint>, greater<pint>> pq;
    pq.emplace(M * 2 + 10, M * 2 + 10);
    FOR(x, 1, M * 2 + 1)
    {
        for (auto lr : lrs)
        {
            int l = lr.first, r = lr.second;
            if (l <= x and x <= r)
            {
                if (mmin(dp[x], dp[max(l - 1, 0)])) pq.emplace(dp[x], r);
            }
            else
            {
                int c = min(abs(l - x), abs(r - x));
                int newl = max(0, l - c - 1);
                if (mmin(dp[x], dp[newl] + c)) pq.emplace(dp[x], r + c);
            }
        }
        while (pq.top().second < x) pq.pop();
        mmin(dp[x], pq.top().first);
    }
    // dbg(dp);
    cout << *min_element(dp.begin() + M, dp.end()) << endl;
}


#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
template<typename T> bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N, M;
    cin >> N >> M;
    vector<pint> lrs(N);
    REP(i, N)
    {
        int x, s;
        cin >> x >> s;
        lrs[i] = make_pair(x - s, x + s);
    }
    lrs.emplace_back(0, 0);

    vector<int> dp(M * 2 + 1, M * 2 + 10);
    dp[0] = 0;
    priority_queue<pint, vector<pint>, greater<pint>> pq;
    pq.emplace(M * 2 + 10, M * 2 + 10);
    FOR(x, 1, M * 2 + 1)
    {
        for (auto lr : lrs)
        {
            int l = lr.first, r = lr.second;
            if (l <= x and x <= r)
            {
                if (mmin(dp[x], dp[max(l - 1, 0)])) pq.emplace(dp[x], r);
            }
            else
            {
                int c = min(abs(l - x), abs(r - x));
                if (mmin(dp[x], dp[max(0, l - c - 1)] + c)) pq.emplace(dp[x], r + c);
            }
        }
        while (pq.top().second < x) pq.pop();
        mmin(dp[x], pq.top().first);
    }
    // dbg(dp);
    cout << *min_element(dp.begin() + M, dp.end()) << endl;
}


#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O2")

#define vi vector<int>
#define pii pair<int, int >
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define all(x) (x).begin(), (x).end()
#define all2(x,n) (x+1), (x+1+n)
#define sz(x) ((int)(x).size())
#define mod(x) ((x)%MOD)
#define debug(x) cerr<<#x<<" : "<<x<<endl
#define mt make_tuple
#define eb emplace_back
#define o(X) (1<<(X))
#define oL(X) (1LL<<(X))
#define contain(S,X) (((S)&o(X))!=0)
#define containL(S,X) (((S)&oL(X))!=0)
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cerr<<endl;}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << "=" << a << " # ";
	err(++it, args...);
}

using LL=long long;
const int N = 1e6 + 5, INF = 0x3f3f3f3f, MOD = 998244353;

vector<pair<int,int> > h[N];

struct node {
	int x,d;
	node() {}
	node(int a,int b):x(a),d(b) {}
	friend int operator<(const node &,const node &);
};
int operator<(const node &a,const node &b) {
	if(a.d==b.d) return a.x<b.x;
	return a.d>b.d;
}

int dis[N],vis[N];
priority_queue<node> que;
void dijk(int u,int n) {
	for(int i=1;i<=n;i++) vis[i]=0,dis[i]=INF; dis[u]=0;
	que.push(node(u,0));
	node x; pair<int,int> t;
	while(!que.empty()) {
		x=que.top(), que.pop();
		if(vis[x.x]) continue; vis[x.x]=1;
		for(int i=0;i<h[x.x].size();i++) {
			t=h[x.x][i];
			if(x.d+t.second<dis[t.first]) {
				dis[t.first]=x.d+t.second;
				que.push(node(t.first,dis[t.first]));
			}
		}
	}
}
void add_edge(int u,int v,int w) {
	h[u].eb(v,w);
//	h[v].eb(u,w);
}

int x[N],s[N];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
//    freopen("o1.txt","w",stdout);
#endif
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);

	int n,m,des;
	cin>>n>>m;des=m+1;
	rep(i,1,n){
		cin>>x[i]>>s[i];
	}
	rep(i,2,des){
		add_edge(i-1,i,1);
		add_edge(i,i-1,0);
	}
	rep(i,1,n){
		int l=max(1,x[i]-s[i]),r=min(des,x[i]+s[i]+1);
		for(int d=0;l-d>=1&&r+d<=des;d++){
			add_edge(l-d,r+d,d);
		}
	}
	dijk(1,des);
	cout<<dis[des]<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O2")

#define vi vector<int>
#define pii pair<int, int >
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define all(x) (x).begin(), (x).end()
#define all2(x,n) (x+1), (x+1+n)
#define sz(x) ((int)(x).size())
#define mod(x) ((x)%MOD)
#define debug(x) cerr<<#x<<" : "<<x<<endl
#define mt make_tuple
#define eb emplace_back
#define o(X) (1<<(X))
#define oL(X) (1LL<<(X))
#define contain(S,X) (((S)&o(X))!=0)
#define containL(S,X) (((S)&oL(X))!=0)
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cerr<<endl;}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << "=" << a << " # ";
	err(++it, args...);
}

using LL=long long;
const int N = 1e6 + 5, INF = 0x3f3f3f3f, MOD = 998244353;

int x[N],s[N],f[N];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
//    freopen("o1.txt","w",stdout);
#endif
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);

	int n,m,des;
	cin>>n>>m;des=m+1;
	rep(i,1,n){
		cin>>x[i]>>s[i];
	}
	memset(f,0x3f,sizeof f);
	f[0]=0;
	rep(i,1,m){
		int cover=0;
		rep(j,1,n)if(i>=x[i]-s[i]&&i<=x[i]+s[i])cover=1;
		if(cover)f[i]=f[i-1];
		rep(j,1,n){
			f[i]=min(f[i],f[max(0,2*x[j]-i-1)]+i-x[j]-s[j]);
		}
	}
	cout<<f[m]<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O2")

#define vi vector<int>
#define pii pair<int, int >
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define all(x) (x).begin(), (x).end()
#define all2(x,n) (x+1), (x+1+n)
#define sz(x) ((int)(x).size())
#define mod(x) ((x)%MOD)
#define debug(x) cerr<<#x<<" : "<<x<<endl
#define mt make_tuple
#define eb emplace_back
#define o(X) (1<<(X))
#define oL(X) (1LL<<(X))
#define contain(S,X) (((S)&o(X))!=0)
#define containL(S,X) (((S)&oL(X))!=0)
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cerr<<endl;}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << "=" << a << " # ";
	err(++it, args...);
}

using LL=long long;
const int N = 1e6 + 5, INF = 0x3f3f3f3f, MOD = 998244353;

int x[N],s[N],f[N];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
//    freopen("o1.txt","w",stdout);
#endif
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);

	int n,m,des;
	cin>>n>>m;des=m+1;
	rep(i,1,n){
		cin>>x[i]>>s[i];
	}
	memset(f,0x3f,sizeof f);
	f[0]=0;
	rep(i,1,m){
		int cover=0;
		rep(j,1,n)if(i>=x[i]-s[i]&&i<=x[i]+s[i])cover=1;
		if(cover)f[i]=f[i-1];
		rep(j,1,n){
			if(i>=x[j]+s[j])f[i]=min(f[i],f[max(0,2*x[j]-i-1)]+i-x[j]-s[j]);
		}
	}
	cout<<f[m]<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O2")

#define vi vector<int>
#define pii pair<int, int >
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define all(x) (x).begin(), (x).end()
#define all2(x,n) (x+1), (x+1+n)
#define sz(x) ((int)(x).size())
#define mod(x) ((x)%MOD)
#define debug(x) cerr<<#x<<" : "<<x<<endl
#define mt make_tuple
#define eb emplace_back
#define o(X) (1<<(X))
#define oL(X) (1LL<<(X))
#define contain(S,X) (((S)&o(X))!=0)
#define containL(S,X) (((S)&oL(X))!=0)
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cerr<<endl;}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << "=" << a << " # ";
	err(++it, args...);
}

using LL=long long;
const int N = 1e6 + 5, INF = 0x3f3f3f3f, MOD = 998244353;

int x[N],s[N],f[N];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
//    freopen("o1.txt","w",stdout);
#endif
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);

	int n,m;
	cin>>n>>m;
	rep(i,1,n){
		cin>>x[i]>>s[i];
	}
	f[0]=0;
	rep(i,1,m){
		f[i]=i;
		int cover=0;
		rep(j,1,n)if(i>=x[j]-s[j]&&i<=x[j]+s[j])cover=1;
		if(cover)f[i]=f[i-1];
		rep(j,1,n){
			if(i>=x[j]+s[j])f[i]=min(f[i],f[max(0,2*x[j]-i-1)]+i-x[j]-s[j]);
		}
	}
	cout<<f[m]<<endl;
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 85;
const int maxm = 2e5 + 10;
struct node{
    int x,s,l,r;
    friend bool operator <(node a,node b){
        if(a.x != b.x)
            return a.x < b.x;
        return a.s > b.s;
    }
}p[maxn];
int dp[maxm];
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m) != EOF){
        for(int i = 1;i <= n;i++){
            scanf("%d%d",&p[i].x,&p[i].s);
            p[i].l = p[i].x - p[i].s;
            p[i].r = p[i].x + p[i].s;
        }
        sort(p + 1,p + n + 1);

        dp[0] = 0;
        int res = max(p[1].l - 1,0),pos;
        for(int i = 1;i <= p[1].r + res;i++){
            dp[i] = res;
        }
        for(int i = p[1].r + res + 1;i <= m;i++){
            dp[i] = dp[i - 1] + 1;
        }

        for(int i = 2;i <= n;i++){
            res = dp[p[i].l - 1];
            for(int j = p[i].l;j <= p[i].r;j++){
                dp[j] = min(dp[j],res);
            }
            for(int j = p[i].r + 1;j <= m;j++){
                pos = p[i].x - (j - p[i].x);
                dp[j] = min(dp[j],dp[max(0,pos - 1)] + j - p[i].r);
            }
            res = max(p[i].l - 1,0);
            for(int j = 1;j <= p[i].l - 1;j++){
                dp[j] = min(dp[j],res);
            }
        }

        printf("%d\n",dp[m]);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 85;
const int maxm = 1e5 + 10;
struct node{
    int x,s,l,r;
    friend bool operator <(node a,node b){
        if(a.x != b.x)
            return a.x < b.x;
        return a.s > b.s;
    }
}p[maxn];
int dp[maxm];
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m) != EOF){
        for(int i = 1;i <= n;i++){
            scanf("%d%d",&p[i].x,&p[i].s);
            p[i].l = p[i].x - p[i].s;
            p[i].r = p[i].x + p[i].s;
        }
        sort(p + 1,p + n + 1);

        int res = max(p[1].l - 1,0),pos;
        for(int i = 1;i <= p[1].r + res;i++){
            dp[i] = res;
        }
        for(int i = p[1].r + res + 1;i <= m;i++){
            dp[i] = dp[i - 1] + 1;
        }

        for(int i = 2;i <= n;i++){
            res = dp[p[i].l - 1];
            for(int j = p[i].l;j <= p[i].r;j++){
                dp[j] = min(dp[j],res);
            }
            for(int j = p[i].r + 1;j <= m;j++){
                pos = p[i].x - (j - p[i].x);
                dp[j] = min(dp[j],dp[pos - 1] + j - p[i].r);
            }
            res = max(p[i].l - 1,0);
            for(int j = 1;j <= p[i].l - 1;j++){
                dp[j] = min(dp[j],res);
            }
        }

        printf("%d\n",dp[m]);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 85;
const int maxm = 1e5 + 10;
struct node{
    int x,s,l,r;
    friend bool operator <(node a,node b){
        if(a.x != b.x)
            return a.x < b.x;
        return a.s > b.s;
    }
}p[maxn];
int dp[maxm];
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m) != EOF){
        for(int i = 1;i <= n;i++){
            scanf("%d%d",&p[i].x,&p[i].s);
            p[i].l = p[i].x - p[i].s;
            p[i].r = p[i].x + p[i].s;
        }
        sort(p + 1,p + n + 1);

        dp[0] = 0;
        int res = max(p[1].l - 1,0),pos;
        for(int i = 1;i <= p[1].r + res;i++){
            dp[i] = res;
        }
        for(int i = p[1].r + res + 1;i <= m;i++){
            dp[i] = dp[i - 1] + 1;
        }

        for(int i = 2;i <= n;i++){
            res = dp[p[i].l - 1];
            for(int j = p[i].l;j <= p[i].r;j++){
                dp[j] = min(dp[j],res);
            }
            for(int j = p[i].r + 1;j <= m;j++){
                pos = p[i].x - (j - p[i].x);
                dp[j] = min(dp[j],dp[max(0,pos - 1)] + j - p[i].r);
            }
            res = max(p[i].l - 1,0);
            for(int j = 1;j <= p[i].l - 1;j++){
                dp[j] = min(dp[j],res);
            }
        }

        printf("%d\n",dp[m]);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 85;
const int maxm = 2e5 + 10;
struct node{
    int x,s,l,r;
    friend bool operator <(node a,node b){
        if(a.x != b.x)
            return a.x < b.x;
        return a.s > b.s;
    }
}p[maxn];
int dp[maxm];
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m) != EOF){
        for(int i = 1;i <= n;i++){
            scanf("%d%d",&p[i].x,&p[i].s);
            p[i].l = max(1,p[i].x - p[i].s);
            p[i].r = min(m,p[i].x + p[i].s);
        }
        sort(p + 1,p + n + 1);

        dp[0] = 0;
        int res = max(p[1].l - 1,0),pos;
        for(int i = 1;i <= p[1].r + res;i++){
            dp[i] = res;
        }
        for(int i = p[1].r + res + 1;i <= m;i++){
            dp[i] = dp[i - 1] + 1;
        }

        for(int i = 2;i <= n;i++){
            if(p[i].x == p[i - 1].x)
                continue;

            for(int j = m;j >= p[i].r + 1;j--){
                pos = p[i].x - (j - p[i].x);
                dp[j] = min(dp[j],dp[max(0,pos - 1)] + j - p[i].r);
            }

            res = dp[max(0,p[i].l - 1)];
            for(int j = p[i].r;j >= p[i].l;j--){
                dp[j] = min(dp[j],res);
            }

            res = max(p[i].l - 1,0);
            for(int j = p[i].l - 1;j >= 1;j--){
                dp[j] = min(dp[j],res);
            }

            res = p[i].l - 1;
            int temp = res;
            for(int j = 1;j <= min(m,p[i].r + res);j++){
                dp[j] = min(dp[j],res);
            }
            for(int j = min(m,p[i].r + res) + 1;j <= m;j++){
                temp++;
                dp[j] = min(dp[j],temp);
            }
        }

        printf("%d\n",dp[m]);
    }
    return 0;
}


/*
Nahid Hossain
Jahangirnagar University
Roll:54
*/
#include<bits/stdc++.h>
#define ll          long long int
#define db          double
#define pf          printf
#define sf          scanf
#define ff          first
#define ss          second
#define clr         clear()
#define sz          size()
#define pb          push_back
#define mk          make_pair
#define pi          acos(-1)
#define inf         100000000000000000
#define mod         1000000007
#define ull         unsigned long long int
#define f(i,k,n)    for(ll i=k;i<n;i++)
#define fr(i,n,k)   for(i=n;i>=k;i--)
#define ent(a)      scanf("%lld",&a)
#define ent2(a,b)   scanf("%lld%lld",&a,&b)
#define ent3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define mem(a)      memset(a,0,sizeof(a))
#define vec(v,s)    vector<ll>v[s]
#define arr(a,s)    ll a[s];
#define bitone(x)   __builtin_popcount(x)
//#define check(n,pos) (n&(1<<pos))
//#define set(n,pos)  (n|(1<<pos))
//knight and king//

//for(i=x;i>0;i=(i-1)&x)
int dr[]= {2, 2, -2, -2, 1, -1, 1, -1};
int dc[]= {1,-1,  1, -1, 2,  2,-2, -2};
int dr1[]= {0, 0, 0, 1, 1, 1, -1, -1, -1};
int dc1[]= {-1,0, 1,-1, 0, 1, -1, 0,   1};
int dr2[]= {-1, 0, 0, 1};
int dc2[]= {-1,-1, -2, -1 };
////////////////////////////
using namespace std;
#define ma 100005
pair<ll,ll>p[ma];

ll tree[ma*3],lazy[ma*3];
ll b[ma];

void make(ll n,ll b,ll e)
{
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    ll x=lazy[n];
    tree[l]=min(x,tree[l]);
    tree[r]=min(x,tree[r]);
    lazy[l]=min(x,lazy[l]);
    lazy[r]=min(x,lazy[r]);
    lazy[n]=inf;
}

void update(ll n,ll b,ll e,ll x,ll y,ll val)
{
    if(b>y||e<x)return;
    if(b>=x&&e<=y)
    {
        tree[n]=min(tree[n],val);
        lazy[n]=min(lazy[n],val);
        return;
    }
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    make(n,b,e);
    update(l,b,mid,x,y,val);
    update(r,mid+1,e,x,y,val);
    tree[n]=min(tree[l],tree[r]);
}

ll query(ll n,ll b,ll e,ll x,ll y)
{
    if(b>y||e<x)return inf;
    if(b>=x&&e<=y)return tree[n];
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    make(n,b,e);
    ll p=inf,p1=inf;
    p=query(l,b,mid,x,y);
    p1=query(r,mid+1,e,x,y);
    return min(p,p1);

}

int main()
{
    ll n,m,i,j;

    ent2(n,m);
        for(i=1;i<=n;i++)
        {
            ent2(p[i].ff,p[i].ss);
        }
        sort(p+1,p+n+1);
        for(i=0;i<ma*3;i++)
        {
            tree[i]=inf;
            lazy[i]=inf;
        }
       // for(j=1;j<=m;j++)dp[j]=inf;
       ll max1=0;
        update(1,0,m,0,0,0);
        for(i=1;i<=n;i++)
        {
            ll x=p[i].ff;
            ll s=p[i].ss;
            ll lef=x+s;
            if(max1>=lef)j=max1-lef+1;
            else j=0;

            for(;j<=m;j++)
            {

                ll lef=max(x-s-j-1,0LL);
                ll rig=min(x+s+j,m);
                ll p=query(1,0,m,lef,x-1)+j;
                update(1,0,m,lef+1,rig,p);
                if(lef==0&&rig==m)break;
            }
            max1=max(max1,x+s);

        }
        ll ans=query(1,0,m,m,m);
        pf("%lld\n",ans);

    return 0;
}


/*
Nahid Hossain
Jahangirnagar University
Roll:54
*/
#include<bits/stdc++.h>
#define ll          long long int
#define db          double
#define pf          printf
#define sf          scanf
#define ff          first
#define ss          second
#define clr         clear()
#define sz          size()
#define pb          push_back
#define mk          make_pair
#define pi          acos(-1)
#define inf         100000000000000000
#define mod         1000000007
#define ull         unsigned long long int
#define f(i,k,n)    for(ll i=k;i<n;i++)
#define fr(i,n,k)   for(i=n;i>=k;i--)
#define ent(a)      scanf("%lld",&a)
#define ent2(a,b)   scanf("%lld%lld",&a,&b)
#define ent3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define mem(a)      memset(a,0,sizeof(a))
#define vec(v,s)    vector<ll>v[s]
#define arr(a,s)    ll a[s];
#define bitone(x)   __builtin_popcount(x)
//#define check(n,pos) (n&(1<<pos))
//#define set(n,pos)  (n|(1<<pos))
//knight and king//

//for(i=x;i>0;i=(i-1)&x)
int dr[]= {2, 2, -2, -2, 1, -1, 1, -1};
int dc[]= {1,-1,  1, -1, 2,  2,-2, -2};
int dr1[]= {0, 0, 0, 1, 1, 1, -1, -1, -1};
int dc1[]= {-1,0, 1,-1, 0, 1, -1, 0,   1};
int dr2[]= {-1, 0, 0, 1};
int dc2[]= {-1,-1, -2, -1 };
////////////////////////////
using namespace std;
#define ma 100005
pair<ll,ll>p[ma];

ll tree[ma*3],lazy[ma*3];
ll b[ma];

void make(ll n,ll b,ll e)
{
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    ll x=lazy[n];
    tree[l]=min(x,tree[l]);
    tree[r]=min(x,tree[r]);
    lazy[l]=min(x,lazy[l]);
    lazy[r]=min(x,lazy[r]);
    lazy[n]=inf;
}

void update(ll n,ll b,ll e,ll x,ll y,ll val)
{
    if(b>y||e<x)return;
    if(b>=x&&e<=y)
    {
        tree[n]=min(tree[n],val);
        lazy[n]=min(lazy[n],val);
        return;
    }
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    make(n,b,e);
    update(l,b,mid,x,y,val);
    update(r,mid+1,e,x,y,val);
    tree[n]=min(tree[l],tree[r]);
}

ll query(ll n,ll b,ll e,ll x,ll y)
{
    if(b>y||e<x)return inf;
    if(b>=x&&e<=y)return tree[n];
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    make(n,b,e);
    ll p=inf,p1=inf;
    p=query(l,b,mid,x,y);
    p1=query(r,mid+1,e,x,y);
    return min(p,p1);

}

int main()
{
    ll n,m,i,j;

    ent2(n,m);
        for(i=1;i<=n;i++)
        {
            ent2(p[i].ff,p[i].ss);
        }
        //sort(p+1,p+n+1);
        for(i=0;i<ma*3;i++)
        {
            tree[i]=inf;
            lazy[i]=inf;
        }
       // for(j=1;j<=m;j++)dp[j]=inf;
       ll max1=0;
        update(1,0,m,0,0,0);
        for(i=1;i<=n;i++)
        {
            ll x=p[i].ff;
            ll s=p[i].ss;
            ll lef=x+s;
            //if(max1>=lef)j=max1-lef+1;
            //else j=0;

            for(j=0;j<=m;j++)
            {

                ll lef=max(x-s-j-1,0LL);
                ll rig=min(x+s+j,m);
                ll p=query(1,0,m,lef,x-1)+j;
                update(1,0,m,lef+1,rig,p);
                if(lef==0&&rig==m)break;
            }
           // max1=max(max1,x+s);

        }
        ll ans=query(1,0,m,m,m);
        pf("%lld\n",ans);

    return 0;
}


/*
Nahid Hossain
Jahangirnagar University
Roll:54
*/
#include<bits/stdc++.h>
#define ll          long long int
#define db          double
#define pf          printf
#define sf          scanf
#define ff          first
#define ss          second
#define clr         clear()
#define sz          size()
#define pb          push_back
#define mk          make_pair
#define pi          acos(-1)
#define inf         100000000000000000
#define mod         1000000007
#define ull         unsigned long long int
#define f(i,k,n)    for(ll i=k;i<n;i++)
#define fr(i,n,k)   for(i=n;i>=k;i--)
#define ent(a)      scanf("%lld",&a)
#define ent2(a,b)   scanf("%lld%lld",&a,&b)
#define ent3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define mem(a)      memset(a,0,sizeof(a))
#define vec(v,s)    vector<ll>v[s]
#define arr(a,s)    ll a[s];
#define bitone(x)   __builtin_popcount(x)
//#define check(n,pos) (n&(1<<pos))
//#define set(n,pos)  (n|(1<<pos))
//knight and king//

//for(i=x;i>0;i=(i-1)&x)
int dr[]= {2, 2, -2, -2, 1, -1, 1, -1};
int dc[]= {1,-1,  1, -1, 2,  2,-2, -2};
int dr1[]= {0, 0, 0, 1, 1, 1, -1, -1, -1};
int dc1[]= {-1,0, 1,-1, 0, 1, -1, 0,   1};
int dr2[]= {-1, 0, 0, 1};
int dc2[]= {-1,-1, -2, -1 };
////////////////////////////
using namespace std;
#define ma 100005
pair<ll,ll>p[ma];

ll tree[ma*3],lazy[ma*3];
ll dp[100005];
ll b[ma];

void make(ll n,ll b,ll e)
{
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    ll x=lazy[n];
    tree[l]=min(x,tree[l]);
    tree[r]=min(x,tree[r]);
    lazy[l]=min(x,lazy[l]);
    lazy[r]=min(x,lazy[r]);
    lazy[n]=inf;
}

void update(ll n,ll b,ll e,ll x,ll y,ll val)
{
    if(b>y||e<x)return;
    if(b>=x&&e<=y)
    {
        tree[n]=min(tree[n],val);
        lazy[n]=min(lazy[n],val);
        return;
    }
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    make(n,b,e);
    update(l,b,mid,x,y,val);
    update(r,mid+1,e,x,y,val);
    tree[n]=min(tree[l],tree[r]);
}

ll query(ll n,ll b,ll e,ll x,ll y)
{
    if(b>y||e<x)return inf;
    if(b>=x&&e<=y)return tree[n];
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    make(n,b,e);
    ll p=query(l,b,mid,x,y);
    ll p1=query(r,mid+1,e,x,y);
    return min(p,p1);

}

int main()
{
    ll n,m,i,j;

    ent2(n,m);
        for(i=1;i<=n;i++)
        {
            ent2(p[i].ff,p[i].ss);
        }
        sort(p+1,p+n+1);
        for(i=0;i<ma*3;i++)
        {
            tree[i]=inf;
            lazy[i]=inf;
        }
       // for(j=1;j<=m;j++)dp[j]=inf;
        update(1,0,m,0,0,0);
        for(i=1;i<=n;i++)
        {
            ll x=p[i].ff;
            ll s=p[i].ss;

            for(j=0;j<=m;j++)
            {
                ll lef=max(x-s-j-1,0LL);
                ll rig=min(x+s+j,m);
                ll p=query(1,0,m,lef,x-1)+j;
                update(1,0,m,lef+1,rig,p);

            }
        }
        ll ans=query(1,0,m,m,m);
        pf("%lld\n",ans);

    return 0;
}


/*
Nahid Hossain
Jahangirnagar University
Roll:54
*/
#include<bits/stdc++.h>
#define ll          long long int
#define db          double
#define pf          printf
#define sf          scanf
#define ff          first
#define ss          second
#define clr         clear()
#define sz          size()
#define pb          push_back
#define mk          make_pair
#define pi          acos(-1)
#define inf         100000000000000000
#define mod         1000000007
#define ull         unsigned long long int
#define f(i,k,n)    for(ll i=k;i<n;i++)
#define fr(i,n,k)   for(i=n;i>=k;i--)
#define ent(a)      scanf("%lld",&a)
#define ent2(a,b)   scanf("%lld%lld",&a,&b)
#define ent3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define mem(a)      memset(a,0,sizeof(a))
#define vec(v,s)    vector<ll>v[s]
#define arr(a,s)    ll a[s];
#define bitone(x)   __builtin_popcount(x)
//#define check(n,pos) (n&(1<<pos))
//#define set(n,pos)  (n|(1<<pos))
//knight and king//

//for(i=x;i>0;i=(i-1)&x)
int dr[]= {2, 2, -2, -2, 1, -1, 1, -1};
int dc[]= {1,-1,  1, -1, 2,  2,-2, -2};
int dr1[]= {0, 0, 0, 1, 1, 1, -1, -1, -1};
int dc1[]= {-1,0, 1,-1, 0, 1, -1, 0,   1};
int dr2[]= {-1, 0, 0, 1};
int dc2[]= {-1,-1, -2, -1 };
////////////////////////////
using namespace std;
#define ma 100005
pair<ll,ll>p[ma];

ll tree[ma*3],lazy[ma*3];
ll dp[100005];
ll b[ma];

void make(ll n,ll b,ll e)
{
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    ll x=lazy[n];
    tree[l]=min(x,tree[l]);
    tree[r]=min(x,tree[r]);
    lazy[l]=min(x,lazy[l]);
    lazy[r]=min(x,lazy[r]);
    lazy[n]=inf;
}

void update(ll n,ll b,ll e,ll x,ll y,ll val)
{
    if(b>y||e<x)return;
    if(b>=x&&e<=y)
    {
        tree[n]=min(tree[n],val);
        lazy[n]=min(lazy[n],val);
        return;
    }
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    make(n,b,e);
    update(l,b,mid,x,y,val);
    update(r,mid+1,e,x,y,val);
    tree[n]=min(tree[l],tree[r]);
}

ll query(ll n,ll b,ll e,ll x,ll y)
{
    if(b>y||e<x)return inf;
    if(b>=x&&e<=y)return tree[n];
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    make(n,b,e);
    ll p=query(l,b,mid,x,y);
    ll p1=query(r,mid+1,e,x,y);
    return min(p,p1);

}

int main()
{
    ll n,m,i,j;
    while(cin>>n>>m)
    {
        for(i=1;i<=n;i++)
        {
            cin>>p[i].ff>>p[i].ss;
        }
        sort(p+1,p+n+1);
        for(i=0;i<ma*3;i++)
        {
            tree[i]=inf;
            lazy[i]=inf;
        }
       // for(j=1;j<=m;j++)dp[j]=inf;
        update(1,0,m,0,0,0);
        for(i=1;i<=n;i++)
        {
            ll x=p[i].ff;
            ll s=p[i].ss;

            for(j=0;j<=m;j++)
            {
                ll lef=max(x-s-j-1,0LL);
                ll rig=min(x+s+j,m);
                ll p=query(1,0,m,lef,x-1)+j;
                update(1,0,m,lef+1,rig,p);

            }
        }
        ll ans=query(1,0,m,m,m);
        cout<<ans<<endl;

    }
    return 0;
}


/*
Nahid Hossain
Jahangirnagar University
Roll:54
*/
#include<bits/stdc++.h>
#define ll          long long int
#define db          double
#define pf          printf
#define sf          scanf
#define ff          first
#define ss          second
#define clr         clear()
#define sz          size()
#define pb          push_back
#define mk          make_pair
#define pi          acos(-1)
#define inf         100000000000000000
#define mod         1000000007
#define ull         unsigned long long int
#define f(i,k,n)    for(ll i=k;i<n;i++)
#define fr(i,n,k)   for(i=n;i>=k;i--)
#define ent(a)      scanf("%lld",&a)
#define ent2(a,b)   scanf("%lld%lld",&a,&b)
#define ent3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define mem(a)      memset(a,0,sizeof(a))
#define vec(v,s)    vector<ll>v[s]
#define arr(a,s)    ll a[s];
#define bitone(x)   __builtin_popcount(x)
//#define check(n,pos) (n&(1<<pos))
//#define set(n,pos)  (n|(1<<pos))
//knight and king//

//for(i=x;i>0;i=(i-1)&x)
int dr[]= {2, 2, -2, -2, 1, -1, 1, -1};
int dc[]= {1,-1,  1, -1, 2,  2,-2, -2};
int dr1[]= {0, 0, 0, 1, 1, 1, -1, -1, -1};
int dc1[]= {-1,0, 1,-1, 0, 1, -1, 0,   1};
int dr2[]= {-1, 0, 0, 1};
int dc2[]= {-1,-1, -2, -1 };
////////////////////////////
using namespace std;
#define ma 100005
pair<ll,ll>p[ma];

ll tree[ma*3],lazy[ma*3];
ll dp[100005];
ll b[ma];

void make(ll n,ll b,ll e)
{
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    ll x=lazy[n];
    tree[l]=min(x,tree[l]);
    tree[r]=min(x,tree[r]);
    lazy[l]=min(x,lazy[l]);
    lazy[r]=min(x,lazy[r]);
    lazy[n]=inf;
}

void update(ll n,ll b,ll e,ll x,ll y,ll val)
{
    if(b>y||e<x)return;
    if(b>=x&&e<=y)
    {
        tree[n]=min(tree[n],val);
        lazy[n]=min(lazy[n],val);
        return;
    }
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    make(n,b,e);
    update(l,b,mid,x,y,val);
    update(r,mid+1,e,x,y,val);
    tree[n]=min(tree[l],tree[r]);
}

ll query(ll n,ll b,ll e,ll x,ll y)
{
    if(b>y||e<x)return inf;
    if(b>=x&&e<=y)return tree[n];
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    make(n,b,e);
    ll p=query(l,b,mid,x,y);
    ll p1=query(r,mid+1,e,x,y);
    return min(p,p1);

}

int main()
{
    ll n,m,i,j;
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    while(cin>>n>>m)
    {
        for(i=1;i<=n;i++)
        {
            cin>>p[i].ff>>p[i].ss;
        }
        sort(p+1,p+n+1);
        for(i=0;i<ma*3;i++)
        {
            tree[i]=inf;
            lazy[i]=inf;
        }
       // for(j=1;j<=m;j++)dp[j]=inf;
        update(1,0,m,0,0,0);
        for(i=1;i<=n;i++)
        {
            ll x=p[i].ff;
            ll s=p[i].ss;

            for(j=0;j<=m;j++)
            {
                ll lef=max(x-s-j-1,0LL);
                ll rig=min(x+s+j,m);
                ll p=query(1,0,m,lef,x-1)+j;
                update(1,0,m,lef+1,rig,p);

            }
        }
        ll ans=query(1,0,m,m,m);
        cout<<ans<<endl;

    }
    return 0;
}


/*
Nahid Hossain
Jahangirnagar University
Roll:54
*/
#include<bits/stdc++.h>
#define ll          long long int
#define db          double
#define pf          printf
#define sf          scanf
#define ff          first
#define ss          second
#define clr         clear()
#define sz          size()
#define pb          push_back
#define mk          make_pair
#define pi          acos(-1)
#define inf         100000000000000000
#define mod         1000000007
#define ull         unsigned long long int
#define f(i,k,n)    for(ll i=k;i<n;i++)
#define fr(i,n,k)   for(i=n;i>=k;i--)
#define ent(a)      scanf("%lld",&a)
#define ent2(a,b)   scanf("%lld%lld",&a,&b)
#define ent3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define mem(a)      memset(a,0,sizeof(a))
#define vec(v,s)    vector<ll>v[s]
#define arr(a,s)    ll a[s];
#define bitone(x)   __builtin_popcount(x)
//#define check(n,pos) (n&(1<<pos))
//#define set(n,pos)  (n|(1<<pos))
//knight and king//

//for(i=x;i>0;i=(i-1)&x)
int dr[]= {2, 2, -2, -2, 1, -1, 1, -1};
int dc[]= {1,-1,  1, -1, 2,  2,-2, -2};
int dr1[]= {0, 0, 0, 1, 1, 1, -1, -1, -1};
int dc1[]= {-1,0, 1,-1, 0, 1, -1, 0,   1};
int dr2[]= {-1, 0, 0, 1};
int dc2[]= {-1,-1, -2, -1 };
////////////////////////////
using namespace std;
#define ma 100005
pair<ll,ll>p[ma];

ll tree[ma*3],lazy[ma*3];
ll b[ma];

void make(ll n,ll b,ll e)
{
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    ll x=lazy[n];
    tree[l]=min(x,tree[l]);
    tree[r]=min(x,tree[r]);
    lazy[l]=min(x,lazy[l]);
    lazy[r]=min(x,lazy[r]);
    lazy[n]=inf;
}

void update(ll n,ll b,ll e,ll x,ll y,ll val)
{
    if(b>y||e<x)return;
    if(b>=x&&e<=y)
    {
        tree[n]=min(tree[n],val);
        lazy[n]=min(lazy[n],val);
        return;
    }
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    make(n,b,e);
    update(l,b,mid,x,y,val);
    update(r,mid+1,e,x,y,val);
    tree[n]=min(tree[l],tree[r]);
}

ll query(ll n,ll b,ll e,ll x,ll y)
{
    if(b>y||e<x)return inf;
    if(b>=x&&e<=y)return tree[n];
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    make(n,b,e);
    ll p=inf,p1=inf;
    p=query(l,b,mid,x,y);
    p1=query(r,mid+1,e,x,y);
    return min(p,p1);

}

int main()
{
    ll n,m,i,j;

    ent2(n,m);
        for(i=1;i<=n;i++)
        {
            ent2(p[i].ff,p[i].ss);
        }
        //sort(p+1,p+n+1);
        for(i=0;i<ma*3;i++)
        {
            tree[i]=inf;
            lazy[i]=inf;
        }
       // for(j=1;j<=m;j++)dp[j]=inf;
       ll max1=0;
        update(1,0,m,0,0,0);
        for(i=1;i<=n;i++)
        {
            ll x=p[i].ff;
            ll s=p[i].ss;
            ll lef=x+s;
            if(max1>=lef)j=max1-lef+1;
            else j=0;

            for(;j<=m;j++)
            {

                ll lef=max(x-s-j-1,0LL);
                ll rig=min(x+s+j,m);
                ll p=query(1,0,m,lef,x-1)+j;
                update(1,0,m,lef+1,rig,p);
                if(lef==0&&rig==m)break;
            }
            max1=max(max1,x+s);

        }
        ll ans=query(1,0,m,m,m);
        pf("%lld\n",ans);

    return 0;
}


/*
Nahid Hossain
Jahangirnagar University
Roll:54
*/
#include<bits/stdc++.h>
#define ll          long long int
#define db          double
#define pf          printf
#define sf          scanf
#define ff          first
#define ss          second
#define clr         clear()
#define sz          size()
#define pb          push_back
#define mk          make_pair
#define pi          acos(-1)
#define inf         1000000000
#define mod         1000000007
#define ull         unsigned long long int
#define f(i,k,n)    for(ll i=k;i<n;i++)
#define fr(i,n,k)   for(i=n;i>=k;i--)
#define ent(a)      scanf("%lld",&a)
#define ent2(a,b)   scanf("%lld%lld",&a,&b)
#define ent3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define mem(a)      memset(a,0,sizeof(a))
#define vec(v,s)    vector<ll>v[s]
#define arr(a,s)    ll a[s];
#define bitone(x)   __builtin_popcount(x)
//#define check(n,pos) (n&(1<<pos))
//#define set(n,pos)  (n|(1<<pos))
//knight and king//

//for(i=x;i>0;i=(i-1)&x)
int dr[]= {2, 2, -2, -2, 1, -1, 1, -1};
int dc[]= {1,-1,  1, -1, 2,  2,-2, -2};
int dr1[]= {0, 0, 0, 1, 1, 1, -1, -1, -1};
int dc1[]= {-1,0, 1,-1, 0, 1, -1, 0,   1};
int dr2[]= {-1, 0, 0, 1};
int dc2[]= {-1,-1, -2, -1 };
////////////////////////////
using namespace std;
#define ma 100005
pair<ll,ll>p[ma];

int dp[82][100005],n,m;

int cal(ll x,ll y)
{
    if(y>m)return 0;
    if(x>n)return inf;
    int &r=dp[x][y];
    if(r!=-1)return r;
    r=inf;
    r=cal(x+1,y);
    if(p[x].ff<=y&&p[x].ss>=y)r=cal(x,y+1);
    else
    {
        if(y<p[x].ff)
        {
            ll b=p[x].ff-y;
            int r1=cal(x,p[x].ss+b+1)+b;
            r=min(r,r1);
        }
        else
        {
            r=min(r,cal(x,y+1)+1);
        }
    }
    return r;
}

int main()
{
    int i,j,x,y;

    cin>>n>>m;
    for(i=1; i<=n; i++)
    {
        cin>>x>>y;
        p[i].ff=max(0,x-y);
        p[i].ss=min(m,x+y);
    }
    sort(p+1,p+n+1);

    memset(dp,-1,sizeof(dp));
    int ans=cal(1,1);
    cout<<ans<<endl;

    return 0;
}


/*
Nahid Hossain
Jahangirnagar University
Roll:54
*/
#include<bits/stdc++.h>
#define ll          long long int
#define db          double
#define pf          printf
#define sf          scanf
#define ff          first
#define ss          second
#define clr         clear()
#define sz          size()
#define pb          push_back
#define mk          make_pair
#define pi          acos(-1)
#define inf         1000000000
#define mod         1000000007
#define ull         unsigned long long int
#define f(i,k,n)    for(ll i=k;i<n;i++)
#define fr(i,n,k)   for(i=n;i>=k;i--)
#define ent(a)      scanf("%lld",&a)
#define ent2(a,b)   scanf("%lld%lld",&a,&b)
#define ent3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define mem(a)      memset(a,0,sizeof(a))
#define vec(v,s)    vector<ll>v[s]
#define arr(a,s)    ll a[s];
#define bitone(x)   __builtin_popcount(x)
//#define check(n,pos) (n&(1<<pos))
//#define set(n,pos)  (n|(1<<pos))
//knight and king//

//for(i=x;i>0;i=(i-1)&x)
int dr[]= {2, 2, -2, -2, 1, -1, 1, -1};
int dc[]= {1,-1,  1, -1, 2,  2,-2, -2};
int dr1[]= {0, 0, 0, 1, 1, 1, -1, -1, -1};
int dc1[]= {-1,0, 1,-1, 0, 1, -1, 0,   1};
int dr2[]= {-1, 0, 0, 1};
int dc2[]= {-1,-1, -2, -1 };
////////////////////////////
using namespace std;
#define ma 100005
pair<ll,ll>p[ma];

int dp[82][100005][2],n,m;

int cal(ll x,ll y,ll f)
{
    if(y>m)return 0;
    if(x>n)return inf;
    int &r=dp[x][y][f];
    if(r!=-1)return r;
    r=inf;
    r=cal(x+1,y,0);
    if(p[x].ff<=y&&p[x].ss>=y)r=cal(x,y+1,0);
    else
    {
        if(y<p[x].ff)
        {
            ll b=p[x].ff-y;
            int r1=cal(x,p[x].ss+b+1,1)+b;
            r=min(r,r1);
        }
        else
        {
            int b=y-p[x].ss;
            if(f==1)r=min(r,cal(x,y+1,1)+1);
            else r=min(r,cal(x,y+1,1)+b);
        }
    }
    return r;
}

int main()
{
    int i,j,x,y;

    cin>>n>>m;
    for(i=1; i<=n; i++)
    {
        cin>>x>>y;
        p[i].ff=max(0,x-y);
        p[i].ss=min(m,x+y);
    }
    sort(p+1,p+n+1);

    memset(dp,-1,sizeof(dp));
    int ans=cal(1,1,0);
    cout<<ans<<endl;

    return 0;
}


/*
Nahid Hossain
Jahangirnagar University
Roll:54
*/
#include<bits/stdc++.h>
#define ll          long long int
#define db          double
#define pf          printf
#define sf          scanf
#define ff          first
#define ss          second
#define clr         clear()
#define sz          size()
#define pb          push_back
#define mk          make_pair
#define pi          acos(-1)
#define inf         100000000000000000
#define mod         1000000007
#define ull         unsigned long long int
#define f(i,k,n)    for(ll i=k;i<n;i++)
#define fr(i,n,k)   for(i=n;i>=k;i--)
#define ent(a)      scanf("%lld",&a)
#define ent2(a,b)   scanf("%lld%lld",&a,&b)
#define ent3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define mem(a)      memset(a,0,sizeof(a))
#define vec(v,s)    vector<ll>v[s]
#define arr(a,s)    ll a[s];
#define bitone(x)   __builtin_popcount(x)
//#define check(n,pos) (n&(1<<pos))
//#define set(n,pos)  (n|(1<<pos))
//knight and king//

//for(i=x;i>0;i=(i-1)&x)
int dr[]= {2, 2, -2, -2, 1, -1, 1, -1};
int dc[]= {1,-1,  1, -1, 2,  2,-2, -2};
int dr1[]= {0, 0, 0, 1, 1, 1, -1, -1, -1};
int dc1[]= {-1,0, 1,-1, 0, 1, -1, 0,   1};
int dr2[]= {-1, 0, 0, 1};
int dc2[]= {-1,-1, -2, -1 };
////////////////////////////
using namespace std;
#define ma 100005
pair<ll,ll>p[ma];

ll tree[ma*3],lazy[ma*3];
ll b[ma];

void make(ll n,ll b,ll e)
{
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    ll x=lazy[n];
    tree[l]=min(x,tree[l]);
    tree[r]=min(x,tree[r]);
    lazy[l]=min(x,lazy[l]);
    lazy[r]=min(x,lazy[r]);
    lazy[n]=inf;
}

void update(ll n,ll b,ll e,ll x,ll y,ll val)
{
    if(b>y||e<x)return;
    if(b>=x&&e<=y)
    {
        tree[n]=min(tree[n],val);
        lazy[n]=min(lazy[n],val);
        return;
    }
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    make(n,b,e);
    update(l,b,mid,x,y,val);
    update(r,mid+1,e,x,y,val);
    tree[n]=min(tree[l],tree[r]);
}

ll query(ll n,ll b,ll e,ll x,ll y)
{
    if(b>y||e<x)return inf;
    if(b>=x&&e<=y)return tree[n];
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    make(n,b,e);
    ll p=inf,p1=inf;
    p=query(l,b,mid,x,y);
    p1=query(r,mid+1,e,x,y);
    return min(p,p1);

}

int main()
{
    ll n,m,i,j;

    ent2(n,m);
        for(i=1;i<=n;i++)
        {
            ent2(p[i].ff,p[i].ss);
        }
        sort(p+1,p+n+1);
        for(i=0;i<ma*3;i++)
        {
            tree[i]=inf;
            lazy[i]=inf;
        }
       // for(j=1;j<=m;j++)dp[j]=inf;
        update(1,0,m,0,0,0);
        for(i=1;i<=n;i++)
        {
            ll x=p[i].ff;
            ll s=p[i].ss;

            for(j=0;j<=m;j++)
            {
                ll lef=max(x-s-j-1,0LL);
                ll rig=min(x+s+j,m);
                ll p=query(1,0,m,lef,x-1)+j;
                update(1,0,m,lef+1,rig,p);
                if(lef==0&&rig==m)break;
            }
        }
        ll ans=query(1,0,m,m,m);
        pf("%lld\n",ans);

    return 0;
}


/*
Nahid Hossain
Jahangirnagar University
Roll:54
*/
#include<bits/stdc++.h>
#define ll          long long int
#define db          double
#define pf          printf
#define sf          scanf
#define ff          first
#define ss          second
#define clr         clear()
#define sz          size()
#define pb          push_back
#define mk          make_pair
#define pi          acos(-1)
#define inf         100000000000000000
#define mod         1000000007
#define ull         unsigned long long int
#define f(i,k,n)    for(ll i=k;i<n;i++)
#define fr(i,n,k)   for(i=n;i>=k;i--)
#define ent(a)      scanf("%lld",&a)
#define ent2(a,b)   scanf("%lld%lld",&a,&b)
#define ent3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define mem(a)      memset(a,0,sizeof(a))
#define vec(v,s)    vector<ll>v[s]
#define arr(a,s)    ll a[s];
#define bitone(x)   __builtin_popcount(x)
//#define check(n,pos) (n&(1<<pos))
//#define set(n,pos)  (n|(1<<pos))
//knight and king//

//for(i=x;i>0;i=(i-1)&x)
int dr[]= {2, 2, -2, -2, 1, -1, 1, -1};
int dc[]= {1,-1,  1, -1, 2,  2,-2, -2};
int dr1[]= {0, 0, 0, 1, 1, 1, -1, -1, -1};
int dc1[]= {-1,0, 1,-1, 0, 1, -1, 0,   1};
int dr2[]= {-1, 0, 0, 1};
int dc2[]= {-1,-1, -2, -1 };
////////////////////////////
using namespace std;
#define ma 100005
pair<ll,ll>p[ma];

ll tree[ma*3],lazy[ma*3];
ll b[ma];

void make(ll n,ll b,ll e)
{
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    ll x=lazy[n];
    tree[l]=min(x,tree[l]);
    tree[r]=min(x,tree[r]);
    lazy[l]=min(x,lazy[l]);
    lazy[r]=min(x,lazy[r]);
    lazy[n]=inf;
}

void update(ll n,ll b,ll e,ll x,ll y,ll val)
{
    if(b>y||e<x)return;
    if(b>=x&&e<=y)
    {
        tree[n]=min(tree[n],val);
        lazy[n]=min(lazy[n],val);
        return;
    }
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    make(n,b,e);
    update(l,b,mid,x,y,val);
    update(r,mid+1,e,x,y,val);
    tree[n]=min(tree[l],tree[r]);
}

ll query(ll n,ll b,ll e,ll x,ll y)
{
    if(b>y||e<x)return inf;
    if(b>=x&&e<=y)return tree[n];
    ll mid=(b+e)>>1,l=n*2,r=l+1;
    make(n,b,e);
    ll p=inf,p1=inf;
    p=query(l,b,mid,x,y);
    p1=query(r,mid+1,e,x,y);
    return min(p,p1);

}

int main()
{
    ll n,m,i,j;

    ent2(n,m);
        for(i=1;i<=n;i++)
        {
            ent2(p[i].ff,p[i].ss);
        }
        sort(p+1,p+n+1);
        for(i=0;i<ma*3;i++)
        {
            tree[i]=inf;
            lazy[i]=inf;
        }
       // for(j=1;j<=m;j++)dp[j]=inf;
       ll max1=0;
        update(1,0,m,0,0,0);
        for(i=1;i<=n;i++)
        {
            ll x=p[i].ff;
            ll s=p[i].ss;

            for(j=0;j<=m;j++)
            {
                ll lef=max(x-s-j-1,0LL);
                ll rig=min(x+s+j,m);
                if(rig<=max1)continue;
                ll p=query(1,0,m,lef,x-1)+j;
                update(1,0,m,lef+1,rig,p);
                if(lef==0&&rig==m)break;
            }
            max1=max(max1,x+s);
        }
        ll ans=query(1,0,m,m,m);
        pf("%lld\n",ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,f[100005];
struct record{
	int x,s;
}a[85];
bool cmp(record a,record b){
	return a.x<b.x;
}
int main(){
	int i,j,k,Min;
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].s);
	sort(a+1,a+n+1,cmp);
	memset(f,63,sizeof(f));
	f[0]=0;
	for (i=1;i<=n;i++){
		Min=min(a[i].x+a[i].s,m);
		k=max(a[i].x-a[i].s,1)-1;
		for (j=max(a[i].x-a[i].s,1);j<=Min;j++)
		if (f[j]<f[k]) k=j;
		for (j=max(a[i].x-a[i].s,1);j<=Min;j++) f[j]=f[k];
		k=a[i].x-a[i].s-1; Min=f[a[i].x];
		for (j=a[i].x+a[i].s+1;j<=m;j++){
			if (k) Min=min(Min,f[--k]);
			if (Min+j-(a[i].x+a[i].s)<f[j]) f[j]=Min+j-(a[i].x+a[i].s);
		}
	}
	printf("%d",f[m]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,f[100005];
struct record{
	int x,s;
}a[85];
bool cmp(record a,record b){
	return a.x-a.s<b.x-b.s;
}
int main(){
	int i,j,k,Min;
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].s);
	sort(a+1,a+n+1,cmp);
	for (i=0;i<=m;i++) f[i]=i;
	for (i=1;i<=n;i++){
		Min=min(a[i].x+a[i].s,m);
		k=max(a[i].x-a[i].s,1);
		for (j=k;j<=Min;j++) f[j]=f[k-1];
		Min=f[--k];
		for (j=a[i].x+a[i].s+1;j<=m;j++){
			if (k) Min=min(Min,f[--k]);
			if (Min+j-(a[i].x+a[i].s)<f[j]) f[j]=Min+j-(a[i].x+a[i].s);
		}
	}
	printf("%d",f[m]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,f[100005];
struct record{
	int x,s;
}a[85];
bool cmp(record a,record b){
	return a.x-a.s<b.x-b.s;
}
int main(){
	int i,j,k,Min;
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].s);
	sort(a+1,a+n+1,cmp);
	memset(f,63,sizeof(f));
	f[0]=0;
	for (i=1;i<=n;i++){
		Min=min(a[i].x+a[i].s,m);
		k=max(a[i].x-a[i].s,1)-1;
		for (j=max(a[i].x-a[i].s,1);j<=Min;j++)
		if (f[j]<f[k]) k=j;
		for (j=max(a[i].x-a[i].s,1);j<=Min;j++) f[j]=f[k];
		k=max(a[i].x-a[i].s,1)-1; Min=f[a[i].x];
		for (j=a[i].x+a[i].s+1;j<=m;j++){
			if (k) Min=min(Min,f[--k]);
			if (Min+j-(a[i].x+a[i].s)<f[j]) f[j]=Min+j-(a[i].x+a[i].s);
		}
	}
	printf("%d",f[m]);
	return 0;
}


#include <iostream>
    #include <vector>
    #include <set>
    #include <algorithm>
    #include <utility>
    using namespace std;

    #define fori(n) for (int i = 0; i < n; ++i)
    #define forj(n) for (int j = 0; j < n; ++j)

    typedef long long int64;
    typedef vector<int> vint;
    typedef vector<int64> vint64;
    typedef vector<vector<int>> vvint;
    typedef pair<int, int> ii;
    typedef vector<ii> vii;

    int n, m;
    vector<char> vis;
    vector<ii> ant;
    vint64 dp;

    int main(){

        cin >> n >> m;
        ant.resize(n);
        vis.resize(m + 1, 0);
        dp.resize(m + 1, (int64)(1e17));
        fori(n) {
            cin >> ant[i].first >> ant[i].second;
            int s= ant[i].second;
            int c = ant[i].first;
            ant[i].first = c - s;
            ant[i].second = c + s;
        }

        fori(n){
            for (int j = max(ant[i].first, 1); j <= min(ant[i].second, m); ++j)
                vis[j] = 1;
        }

        dp[m] = 0;
        for (int j = m - 1; j >= 0; --j){
            dp[j] = m - j;
            if (vis[j])
                dp[j] = min(dp[j + 1], dp[j]);
            else{
                fori(n){
                    if (ant[i].first > j){
                        int64 u = ant[i].first - j - 1;
                        dp[j] = min(dp[j], u +  dp[min((int64)m, ant[i].second + u)]);
                    }
                }
            }
        }

        cout << dp[0] << endl;
        return 0;
    }


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <utility>
using namespace std;

#define fori(n) for (int i = 0; i < n; ++i)
#define forj(n) for (int j = 0; j < n; ++j)

typedef long long int64;
typedef vector<int> vint;
typedef vector<int64> vint64;
typedef vector<vector<int>> vvint;
typedef pair<int, int> ii;
typedef vector<ii> vii;

int n, m;
vector<char> vis;
vector<ii> ant;
vint64 dp;

int main(){

	cin >> n >> m;
	ant.resize(n);
	vis.resize(m + 1, 0);
	dp.resize(m + 1, (int64)(1e17));
	fori(n) {
		cin >> ant[i].first >> ant[i].second;
		int s = ant[i].second;
		int c = ant[i].first;
		ant[i].first = c - s;
		ant[i].second = c + s;
	}

	fori(n){
		for (int j = max(ant[i].first, 0); j <= min(ant[i].second, m); ++j)
			vis[j] = 1;
	}

	dp[m] = 0;
	for (int j = m - 1; j >= 0; --j){
		dp[j] = m - j;
		if (vis[j + 1])
			dp[j] = min(dp[j + 1], dp[j]);
		else{
			fori(n){
				if (ant[i].first > j){
					int64 u = ant[i].first - j - 1;
					dp[j] = min(dp[j], u + dp[min((int64)m, ant[i].second + u)]);
				}
			}
		}
	}

	cout << dp[0] << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <utility>
using namespace std;

#define fori(n) for (int i = 0; i < n; ++i)
#define forj(n) for (int j = 0; j < n; ++j)

typedef long long int64;
typedef vector<int> vint;
typedef vector<int64> vint64;
typedef vector<vector<int>> vvint;
typedef pair<int, int> ii;
typedef vector<ii> vii;

int n, m;
vector<char> vis;
vector<ii> ant;
vint64 dp;

int main(){

	cin >> n >> m;
	ant.resize(n);
	vis.resize(m + 1, 0);
	dp.resize(m + 1, (int64)(1e17));
	fori(n) {
		cin >> ant[i].first >> ant[i].second;
		int s = ant[i].second;
		int c = ant[i].first;
		ant[i].first = c - s;
		ant[i].second = c + s;
	}

	fori(n){
		for (int j = max(ant[i].first, 1); j <= min(ant[i].second, m); ++j)
			vis[j] = 1;
	}

	dp[0] = 0;
	for (int j = 1; j <= m; ++j){
		dp[j] = j;
		if (vis[j])
			dp[j] = min(dp[j], dp[j - 1]);
		else{
			fori(n)
				if (ant[i].second < j){
					int64 u = j - ant[i].second;
					dp[j] = min(dp[j], u + dp[max(0ll, ant[i].first - u - 1)]);
				}
		}
	}

	cout << dp[m] << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <utility>
using namespace std;

#define fori(n) for (int i = 0; i < n; ++i)
#define forj(n) for (int j = 0; j < n; ++j)

typedef long long int64;
typedef vector<int> vint;
typedef vector<int64> vint64;
typedef vector<vector<int>> vvint;
typedef pair<int, int> ii;
typedef vector<ii> vii;

int n, m;
vector<char> vis;
vector<ii> ant;
vint64 dp;

int main(){

    cin >> n >> m;
    ant.resize(n);
    vis.resize(m + 1, 0);
    dp.resize(m + 1, (int64)(1e17));
    fori(n) {
        cin >> ant[i].first >> ant[i].second;
        int s= ant[i].second;
        int c = ant[i].first;
        ant[i].first = c - s;
        ant[i].second = c + s;
    }

    fori(n){
        for (int j = ant[i].first; j <= ant[i].second; ++j)
            vis[j] = 1;
    }

    dp[m] = 0;
    for (int j = m - 1; j >= 0; --j){
        dp[j] = m - j;
        if (vis[j])
            dp[j] = min(dp[j + 1], dp[j]);
        else{
            fori(n){
                if (ant[i].first > j){
                    int64 u = ant[i].first - j - 1;
                    dp[j] = min(dp[j], u +  dp[min((int64)m, ant[i].second + u)]);
                }
            }
        }
    }

    cout << dp[0] << endl;
    return 0;
}


    #include <iostream>
    #include <vector>
    #include <set>
    #include <algorithm>
    #include <utility>
    using namespace std;

    #define fori(n) for (int i = 0; i < n; ++i)
    #define forj(n) for (int j = 0; j < n; ++j)

    typedef long long int64;
    typedef vector<int> vint;
    typedef vector<int64> vint64;
    typedef vector<vector<int>> vvint;
    typedef pair<int, int> ii;
    typedef vector<ii> vii;

    int n, m;
    vector<char> vis;
    vector<ii> ant;
    vint64 dp;

    int main(){

        cin >> n >> m;
        ant.resize(n);
        vis.resize(m + 1, 0);
        dp.resize(m + 1, (int64)(1e17));
        fori(n) {
            cin >> ant[i].first >> ant[i].second;
            int s= ant[i].second;
            int c = ant[i].first;
            ant[i].first = c - s;
            ant[i].second = c + s;
        }

        fori(n){
            for (int j = ant[i].first; j <= ant[i].second; ++j)
                vis[j] = 1;
        }

        dp[m] = 0;
        for (int j = m - 1; j >= 0; --j){
            dp[j] = m - j;
            if (vis[j + 1])
                dp[j] = min(dp[j + 1], dp[j]);
            else{
                fori(n){
                    if (ant[i].first > j){
                        int64 u = ant[i].first - j - 1;
                        dp[j] = min(dp[j], u +  dp[min((int64)m, ant[i].second + u)]);
                    }
                }
            }
        }

        cout << dp[0] << endl;
        return 0;
    }


    #include <iostream>
    #include <vector>
    #include <set>
    #include <algorithm>
    #include <utility>
    using namespace std;

    #define fori(n) for (int i = 0; i < n; ++i)
    #define forj(n) for (int j = 0; j < n; ++j)

    typedef long long int64;
    typedef vector<int> vint;
    typedef vector<int64> vint64;
    typedef vector<vector<int>> vvint;
    typedef pair<int, int> ii;
    typedef vector<ii> vii;

    int n, m;
    vector<char> vis;
    vector<ii> ant;
    vint64 dp;

    int main(){

        cin >> n >> m;
        ant.resize(n);
        vis.resize(m + 1, 0);
        dp.resize(m + 1, (int64)(1e17));
        fori(n) {
            cin >> ant[i].first >> ant[i].second;
            int s= ant[i].second;
            int c = ant[i].first;
            ant[i].first = c - s;
            ant[i].second = c + s;
        }

        fori(n){
            for (int j = max(ant[i].first, 0); j <= min(ant[i].second, m); ++j)
                vis[j] = 1;
        }

        dp[m] = 0;
        for (int j = m - 1; j >= 0; --j){
            dp[j] = m - j;
            if (vis[j])
                dp[j] = min(dp[j + 1], dp[j]);
            else{
                fori(n){
                    if (ant[i].first > j){
                        int64 u = ant[i].first - j - 1;
                        dp[j] = min(dp[j], u +  dp[min((int64)m, ant[i].second + u)]);
                    }
                }
            }
        }

        cout << dp[0] << endl;
        return 0;
    }


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,M;
pair<int,int> P[101];

ll dp[91][101010];

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>N>>M;
	FOR(i,N) cin>>P[i].first>>P[i].second;
	sort(P,P+N);
	vector<pair<int,int>> V;
	FOR(i,N) {
		if(V.size()&&V.back().first+V.back().second>=P[i].first+P[i].second) continue;
		while(V.size() && P[i].first-P[i].second<=V.back().first-V.back().second) V.pop_back();
		V.push_back(P[i]);
	}
	N=V.size();
	FOR(i,N+1) FOR(j,M+1) dp[i][j]=1LL<<60;
	dp[0][0]=0;
	FOR(i,N) {
		dp[i+1][0]=0;
		for(j=0;j<V[i].first-V[i].second;j++) {
			dp[i+1][min(M,V[i].first+(V[i].first-j-1))]=dp[i][j]+(V[i].first-j-1-V[i].second);
		}
		for(j=max(0,V[i].first-V[i].second-1);j<=min(M,V[i].first+V[i].second);j++) {
			dp[i+1][min(M,V[i].first+V[i].second)]=min(dp[i+1][min(M,V[i].first+V[i].second)],dp[i][j]);
		}
		for(j=1;j<M;j++) dp[i+1][j+1]=min(dp[i+1][j+1],dp[i+1][j]+1);
		for(j=M-1;j>=1;j--) dp[i+1][j]=min(dp[i+1][j+1],dp[i+1][j]);

	}
	cout<<dp[N][M]<<endl;

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,M;
pair<int,int> P[101];

ll dp[101010];

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>N>>M;
	FOR(i,N) cin>>P[i].first>>P[i].second;
	sort(P,P+N);
	/*
	vector<pair<int,int>> V;
	FOR(i,N) {
		if(V.size()&&V.back().first+V.back().second>=P[i].first+P[i].second) continue;
		while(V.size() && P[i].first-P[i].second<=V.back().first-V.back().second) V.pop_back();
		V.push_back(P[i]);
	}
	N=V.size();
	*/
	FOR(j,M+1) dp[j+1]=1LL<<60;
	FOR(i,N) {
		for(j=0;j<P[i].first-P[i].second;j++) {
			dp[min(M,P[i].first+(P[i].first-j-1))]=min(dp[min(M,P[i].first+(P[i].first-j-1))],dp[j]+(P[i].first-j-1-P[i].second));
		}
		for(j=max(0,P[i].first-P[i].second-1);j<=min(M,P[i].first+P[i].second);j++) {
			dp[min(M,P[i].first+P[i].second)]=min(dp[min(M,P[i].first+P[i].second)],dp[j]);
		}
		for(j=1;j<M;j++) dp[j+1]=min(dp[j+1],dp[j]+1);
		for(j=M-1;j>=1;j--) dp[j]=min(dp[j+1],dp[j]);

	}
	cout<<dp[M]<<endl;

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,M;
pair<int,int> P[101];

ll dp[91][101010];

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>N>>M;
	FOR(i,N) cin>>P[i].first>>P[i].second;
	sort(P,P+N);
	vector<pair<int,int>> V;
	FOR(i,N) {
		if(V.size()&&V.back().first+V.back().second>=P[i].first+P[i].second) continue;
		while(V.size() && P[i].first-P[i].second<=V.back().first-V.back().second) V.pop_back();
		V.push_back(P[i]);
	}
	N=V.size();
	FOR(i,N+1) FOR(j,M+1) dp[i][j+1]=1LL<<60;
	FOR(i,N) {
		for(j=0;j<V[i].first-V[i].second;j++) {
			dp[i+1][min(M,V[i].first+(V[i].first-j-1))]=min(dp[i+1][min(M,V[i].first+(V[i].first-j-1))],dp[i][j]+(V[i].first-j-1-V[i].second));
		}
		for(j=max(0,V[i].first-V[i].second-1);j<=min(M,V[i].first+V[i].second);j++) {
			dp[i+1][min(M,V[i].first+V[i].second)]=min(dp[i+1][min(M,V[i].first+V[i].second)],dp[i][j]);
		}
		for(j=min(M,V[i].first+V[i].second+1);j<=M;j++) dp[i+1][j]=min(dp[i][j],dp[i+1][j]);
		for(j=1;j<M;j++) dp[i+1][j+1]=min(dp[i+1][j+1],dp[i+1][j]+1);
		for(j=M-1;j>=1;j--) dp[i+1][j]=min(dp[i+1][j+1],dp[i+1][j]);

	}
	cout<<dp[N][M]<<endl;

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,M;
pair<int,int> P[101];

ll dp[91][101010];

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>N>>M;
	FOR(i,N) cin>>P[i].first>>P[i].second;
	sort(P,P+N);
	vector<pair<int,int>> V;
	FOR(i,N) {
		if(V.size()&&V.back().first+V.back().second>=P[i].first+P[i].second) continue;
		while(V.size() && P[i].first-P[i].second<=V.back().first-V.back().second) V.pop_back();
		V.push_back(P[i]);
	}
	N=V.size();
	FOR(i,N+1) FOR(j,M+1) dp[i][j]=1LL<<60;
	dp[0][0]=0;
	FOR(i,N) {
		dp[i+1][0]=0;
		for(j=0;j<V[i].first-V[i].second;j++) {
			dp[i+1][min(M,V[i].first+(V[i].first-j-1))]=dp[i][j]+(V[i].first-j-1-V[i].second);
		}
		for(j=max(0,V[i].first-V[i].second-1);j<=min(M,V[i].first+V[i].second);j++) {
			dp[i+1][min(M,V[i].first+V[i].second)]=min(dp[i+1][min(M,V[i].first+V[i].second)],dp[i][j]);
		}
		FOR(j,M) dp[i+1][j+1]=min(dp[i+1][j+1],dp[i+1][j]+1);
		for(j=M-1;j>=1;j--) dp[i+1][j]=min(dp[i+1][j+1],dp[i+1][j]);

	}
	cout<<dp[N][M]<<endl;

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,M;
pair<int,int> P[101];

ll dp[91][101010];

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>N>>M;
	FOR(i,N) cin>>P[i].first>>P[i].second;
	sort(P,P+N);
	vector<pair<int,int>> V;
	FOR(i,N) {
		if(V.size()&&V.back().first+V.back().second>=P[i].first+P[i].second) continue;
		while(V.size() && P[i].first-P[i].second<=V.back().first-V.back().second) V.pop_back();
		V.push_back(P[i]);
	}
	N=V.size();
	FOR(i,N+1) FOR(j,M+1) dp[i][j+1]=1LL<<60;
	FOR(i,N) {
		for(j=0;j<V[i].first-V[i].second;j++) {
			dp[i+1][min(M,V[i].first+(V[i].first-j-1))]=dp[i][j]+(V[i].first-j-1-V[i].second);
		}
		for(j=max(0,V[i].first-V[i].second-1);j<=min(M,V[i].first+V[i].second);j++) {
			dp[i+1][min(M,V[i].first+V[i].second)]=min(dp[i+1][min(M,V[i].first+V[i].second)],dp[i][j]);
		}
		for(j=min(M,V[i].first+V[i].second+1);j<=M;j++) dp[i+1][j]=min(dp[i][j],dp[i+1][j]);
		for(j=1;j<M;j++) dp[i+1][j+1]=min(dp[i+1][j+1],dp[i+1][j]+1);
		for(j=M-1;j>=1;j--) dp[i+1][j]=min(dp[i+1][j+1],dp[i+1][j]);

	}
	cout<<dp[N][M]<<endl;

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pfi(a) printf("%d",a);
#define pfl(a) printf("%lld",a);
#define pfin(a) printf("%d\n",a);
#define pfln(a) printf("%lld\n",a);
#define sfi(a) scanf("%d",&a);
#define sfl(a) scanf("%lld",&a);
#define pb push_back
#define f(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
const int M=1e9+7;
const int MAXN=2e5+1;

int main(){
 ios_base::sync_with_stdio(false);

 	int n,m;
 	cin>>n>>m;

 	vector<pair<int,int> > v(n);

 	for(int i=0;i<n;i++){
 		cin>>v[i].first>>v[i].second;
 	}

 	sort(v.begin(),v.end());

 	int dp[n+1][2*m+1];

 	vector<int> lo(m+1);

 	vector<int> mp(m+1);

 	for(int i=1;i<=2*m;i++){
 		if(i<v[0].first){
 			int cost=v[0].first-v[0].second-i;
 			cost=max(0,cost);
 			dp[0][i]=cost;
 		}
 		else{
 			int cost=i-(v[0].first+v[0].second);
 			cost=max(0,cost);
 			dp[0][i]=cost;
 		}
 	}

 	int tm=max(0,v[0].first-v[0].second-1);

 	for(int i=1;i<=2*m;i++){
 		dp[0][i]=max(dp[0][i],tm);
 	}

 	for(int i=1;i<n;i++){

 		for(int j=1;j<=2*m;j++){
 			dp[i][j]=dp[i-1][j];

 			if(j<v[i].first){
 				int cost=v[i].first-v[i].second-j;
 				cost=max(0,cost);
 				int fac=0;

 				if(j!=1){
 					fac=dp[i][j-1];
 				}

 				dp[i][j]=min(dp[i][j],fac+cost);

 			}
 			else{
 				int cost=j-(v[i].first+v[i].second);
 				cost=max(0,cost);

 				int sind=min(v[i].first-v[i].second,v[i].first-v[i].second-cost);
 				sind--;
 				sind=max(sind,0);

 				int fac=0;

 				if(sind!=0)
 				fac=dp[i-1][sind];

 				dp[i][j]=min(dp[i][j],fac+cost);
 			}

 		}

 		for(int j=2*m-1;j>=1;j--){
 			dp[i][j]=min(dp[i][j],dp[i][j+1]);
 		}

 	}

 	int ans=dp[n-1][m];

 	for(int j=m+1;j<=2*m;j++){
 		ans=min(ans,dp[n-1][j]);
 	}

 	cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define pfi(a) printf("%d",a);
#define pfl(a) printf("%lld",a);
#define pfin(a) printf("%d\n",a);
#define pfln(a) printf("%lld\n",a);
#define sfi(a) scanf("%d",&a);
#define sfl(a) scanf("%lld",&a);
#define pb push_back
#define f(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
const int M=1e9+7;
const int MAXN=2e5+1;

int main(){
 ios_base::sync_with_stdio(false);

 	int n,m;
 	cin>>n>>m;

 	vector<pair<int,int> > v(n);

 	for(int i=0;i<n;i++){
 		cin>>v[i].first>>v[i].second;
 	}

 	sort(v.begin(),v.end());

 	int dp[n+1][m+1];

 	vector<int> lo(m+1);

 	vector<int> mp(m+1);

 	for(int i=1;i<=m;i++){
 		if(i<v[0].first){
 			int cost=v[0].first-v[0].second-i;
 			cost=max(0,cost);
 			dp[0][i]=cost;
 		}
 		else{
 			int cost=i-(v[0].first+v[0].second);
 			cost=max(0,cost);
 			dp[0][i]=cost;
 		}
 	}

 	for(int i=2;i<=m;i++){
 		dp[0][i]=max(dp[0][i],dp[0][i-1]);
 	}

 	for(int i=1;i<n;i++){
 		for(int j=1;j<=m;j++){
 			dp[i][j]=dp[i-1][j];
 			if(j<v[i].first){
 				int cost=v[i].first-v[i].second-j;
 				cost=max(0,cost);
 				int fac=0;

 				if(j!=1){
 					fac=dp[i][j-1];
 				}

 				dp[i][j]=min(dp[i-1][j],fac+cost);

 			}
 			else{
 				int cost=j-(v[i].first+v[i].second);
 				cost=max(0,cost);

 				int sind=min(v[i].first-v[i].second,v[i].first-v[i].second-cost);
 				sind--;
 				sind=max(sind,0);

 				int fac=0;

 				if(sind!=0)
 				fac=dp[i][sind];

 				dp[i][j]=min(dp[i][j],fac+cost);
 			}

 		}

 	}

 	cout<<dp[n-1][m]<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define pfi(a) printf("%d",a);
#define pfl(a) printf("%lld",a);
#define pfin(a) printf("%d\n",a);
#define pfln(a) printf("%lld\n",a);
#define sfi(a) scanf("%d",&a);
#define sfl(a) scanf("%lld",&a);
#define pb push_back
#define f(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
const int M=1e9+7;
const int MAXN=2e5+1;

int main(){
 ios_base::sync_with_stdio(false);

 	int n,m;
 	cin>>n>>m;

 	vector<pair<int,int> > v(n);

 	for(int i=0;i<n;i++){
 		cin>>v[i].first>>v[i].second;
 	}

 	sort(v.begin(),v.end());

 	int dp[n+1][m+1];

 	vector<int> lo(m+1);

 	vector<int> mp(m+1);

 	for(int i=1;i<=m;i++){
 		if(i<v[0].first){
 			int cost=v[0].first-v[0].second-i;
 			cost=max(0,cost);
 			dp[0][i]=cost;
 		}
 		else{
 			int cost=i-(v[0].first+v[0].second);
 			cost=max(0,cost);
 			dp[0][i]=cost;
 		}
 	}

 	for(int i=2;i<=m;i++){
 		dp[0][i]=max(dp[0][i],dp[0][i-1]);
 	}

 	for(int i=1;i<n;i++){
 		for(int j=1;j<=m;j++){
 			dp[i][j]=dp[i-1][j];

 			if(j<v[i].first){
 				int cost=v[i].first-v[i].second-j;
 				cost=max(0,cost);
 				int fac=0;

 				if(j!=1){
 					fac=dp[i][j-1];
 				}

 				dp[i][j]=min(dp[i][j],fac+cost);

 			}
 			else{
 				int cost=j-(v[i].first+v[i].second);
 				cost=max(0,cost);

 				int sind=min(v[i].first-v[i].second,v[i].first-v[i].second-cost);
 				sind--;
 				sind=max(sind,0);

 				int fac=0;

 				if(sind!=0)
 				fac=dp[i-1][sind];

 				dp[i][j]=min(dp[i][j],fac+cost);
 			}

 		}

 	}

 	cout<<dp[n-1][m]<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define pfi(a) printf("%d",a);
#define pfl(a) printf("%lld",a);
#define pfin(a) printf("%d\n",a);
#define pfln(a) printf("%lld\n",a);
#define sfi(a) scanf("%d",&a);
#define sfl(a) scanf("%lld",&a);
#define pb push_back
#define f(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
const int M=1e9+7;
const int MAXN=2e5+1;

int main(){
 ios_base::sync_with_stdio(false);

 	int n,m;
 	cin>>n>>m;

 	vector<pair<int,int> > v(n);

 	for(int i=0;i<n;i++){
 		cin>>v[i].first>>v[i].second;
 	}

 	sort(v.begin(),v.end());

 	int dp[n+1][2*m+1];

 	vector<int> lo(m+1);

 	vector<int> mp(m+1);

 	for(int i=1;i<=2*m;i++){
 		if(i<v[0].first){
 			int cost=v[0].first-v[0].second-i;
 			cost=max(0,cost);
 			dp[0][i]=cost;
 		}
 		else{
 			int cost=i-(v[0].first+v[0].second);
 			cost=max(0,cost);
 			dp[0][i]=cost;
 		}
 	}

 	int tm=max(0,v[0].first-v[0].second-1);

 	for(int i=1;i<=2*m;i++){
 		dp[0][i]=max(dp[0][i],tm);
 	}

 	for(int i=1;i<n;i++){
 		for(int j=1;j<=2*m;j++){
 			dp[i][j]=dp[i-1][j];

 			if(j<v[i].first){
 				int cost=v[i].first-v[i].second-j;
 				cost=max(0,cost);
 				int fac=0;

 				if(j!=1){
 					fac=dp[i][j-1];
 				}

 				dp[i][j]=min(dp[i][j],fac+cost);

 			}
 			else{
 				int cost=j-(v[i].first+v[i].second);
 				cost=max(0,cost);

 				int sind=min(v[i].first-v[i].second,v[i].first-v[i].second-cost);
 				sind--;
 				sind=max(sind,0);

 				int fac=0;

 				if(sind!=0)
 				fac=dp[i-1][sind];

 				dp[i][j]=min(dp[i][j],fac+cost);
 			}

 		}

 	}

 	int ans=dp[n-1][m];

 	for(int j=m+1;j<=2*m;j++){
 		ans=min(ans,dp[n-1][j]);
 	}

 	cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define pfi(a) printf("%d",a);
#define pfl(a) printf("%lld",a);
#define pfin(a) printf("%d\n",a);
#define pfln(a) printf("%lld\n",a);
#define sfi(a) scanf("%d",&a);
#define sfl(a) scanf("%lld",&a);
#define pb push_back
#define f(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
const int M=1e9+7;
const int MAXN=2e5+1;

int main(){
 ios_base::sync_with_stdio(false);

 	int n,m;
 	cin>>n>>m;

 	vector<pair<int,int> > v(n);

 	for(int i=0;i<n;i++){
 		cin>>v[i].first>>v[i].second;
 	}

 	sort(v.begin(),v.end());

 	int dp[n+1][m+1];

 	vector<int> lo(m+1);

 	vector<int> mp(m+1);

 	for(int i=1;i<=m;i++){
 		if(i<v[0].first){
 			int cost=v[0].first-v[0].second-i;
 			cost=max(0,cost);
 			dp[0][i]=cost;
 		}
 		else{
 			int cost=i-(v[0].first+v[0].second);
 			cost=max(0,cost);
 			dp[0][i]=cost;
 		}
 	}

 	for(int i=2;i<=m;i++){
 		dp[0][i]=max(dp[0][i],dp[0][i-1]);
 	}

 	for(int i=1;i<n;i++){
 		for(int j=1;j<=m;j++){
 			dp[i][j]=dp[i-1][j];

 			if(j<v[i].first){
 				int cost=v[i].first-v[i].second-j;
 				cost=max(0,cost);
 				int fac=0;

 				if(j!=1){
 					fac=dp[i][j-1];
 				}

 				dp[i][j]=min(dp[i][j],fac+cost);

 			}
 			else{
 				int cost=j-(v[i].first+v[i].second);
 				cost=max(0,cost);

 				int sind=min(v[i].first-v[i].second,v[i].first-v[i].second-cost);
 				sind--;
 				sind=max(sind,0);

 				int fac=0;

 				if(sind!=0)
 				fac=dp[i][sind];

 				dp[i][j]=min(dp[i][j],fac+cost);
 			}

 		}

 	}

 	cout<<dp[n-1][m]<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define pfi(a) printf("%d",a);
#define pfl(a) printf("%lld",a);
#define pfin(a) printf("%d\n",a);
#define pfln(a) printf("%lld\n",a);
#define sfi(a) scanf("%d",&a);
#define sfl(a) scanf("%lld",&a);
#define pb push_back
#define f(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
const int M=1e9+7;
const int MAXN=2e5+1;

int main(){
 ios_base::sync_with_stdio(false);

 	int n,m;
 	cin>>n>>m;

 	vector<pair<int,int> > v(n);

 	for(int i=0;i<n;i++){
 		cin>>v[i].first>>v[i].second;
 	}

 	sort(v.begin(),v.end());

 	int dp[n+1][m+1];

 	vector<int> lo(m+1);

 	vector<int> mp(m+1);

 	for(int i=1;i<=m;i++){
 		if(i<v[0].first){
 			int cost=v[0].first-v[0].second-i;
 			cost=max(0,cost);
 			dp[0][i]=cost;
 		}
 		else{
 			int cost=i-(v[0].first+v[0].second);
 			cost=max(0,cost);
 			dp[0][i]=cost;
 		}
 	}

 	int tm=max(0,v[0].first-v[0].second-1);

 	for(int i=1;i<=m;i++){
 		dp[0][i]=max(dp[0][i],tm);
 	}

 	for(int i=1;i<n;i++){
 		for(int j=1;j<=m;j++){
 			dp[i][j]=dp[i-1][j];

 			if(j<v[i].first){
 				int cost=v[i].first-v[i].second-j;
 				cost=max(0,cost);
 				int fac=0;

 				if(j!=1){
 					fac=dp[i][j-1];
 				}

 				dp[i][j]=min(dp[i][j],fac+cost);

 			}
 			else{
 				int cost=j-(v[i].first+v[i].second);
 				cost=max(0,cost);

 				int sind=min(v[i].first-v[i].second,v[i].first-v[i].second-cost);
 				sind--;
 				sind=max(sind,0);

 				int fac=0;

 				if(sind!=0)
 				fac=dp[i-1][sind];

 				dp[i][j]=min(dp[i][j],fac+cost);
 			}

 		}

 	}

 	cout<<dp[n-1][m]<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define pfi(a) printf("%d",a);
#define pfl(a) printf("%lld",a);
#define pfin(a) printf("%d\n",a);
#define pfln(a) printf("%lld\n",a);
#define sfi(a) scanf("%d",&a);
#define sfl(a) scanf("%lld",&a);
#define pb push_back
#define f(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
const int M=1e9+7;
const int MAXN=2e5+1;

int main(){
 ios_base::sync_with_stdio(false);

 	int n,m;
 	cin>>n>>m;

 	vector<pair<int,int> > v(n);

 	for(int i=0;i<n;i++){
 		cin>>v[i].first>>v[i].second;
 	}

 	sort(v.begin(),v.end());

 	int dp[n+1][2*m+1];

 	vector<int> lo(m+1);

 	vector<int> mp(m+1);

 	for(int i=1;i<=2*m;i++){
 		if(i<v[0].first){
 			int cost=v[0].first-v[0].second-i;
 			cost=max(0,cost);
 			dp[0][i]=cost;
 		}
 		else{
 			int cost=i-(v[0].first+v[0].second);
 			cost=max(0,cost);
 			dp[0][i]=cost;
 		}
 	}

 	int tm=max(0,v[0].first-v[0].second-1);

 	for(int i=1;i<=2*m;i++){
 		dp[0][i]=max(dp[0][i],tm);
 	}

 	for(int i=1;i<n;i++){
 		for(int j=1;j<=2*m;j++){
 			dp[i][j]=dp[i-1][j];

 			if(j<v[i].first){
 				int cost=v[i].first-v[i].second-j;
 				cost=max(0,cost);
 				int fac=0;

 				if(j!=1){
 					fac=dp[i][j-1];
 				}

 				dp[i][j]=min(dp[i][j],fac+cost);

 			}
 			else{
 				int cost=j-(v[i].first+v[i].second);
 				cost=max(0,cost);

 				int sind=min(v[i].first-v[i].second,v[i].first-v[i].second-cost);
 				sind--;
 				sind=max(sind,0);

 				int fac=0;

 				if(sind!=0)
 				fac=dp[i-1][sind];

 				dp[i][j]=min(dp[i][j],fac+cost);
 			}

 		}

 		for(int j=2*m-1;j>=1;j++){
 			dp[i][j]=min(dp[i][j],dp[i][j+1]);
 		}

 	}

 	int ans=dp[n-1][m];

 	for(int j=m+1;j<=2*m;j++){
 		ans=min(ans,dp[n-1][j]);
 	}

 	cout<<ans<<endl;

}


#include <bits/stdc++.h>
#ifdef TOPOLOGY
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
using namespace std;

pair<int, int> p[85];
int n, m, x[85], s[85], dp[85][250003];

int rmq(int k, int i, int j) {
	i += 1 << 17;
	j += 1 << 17;
	int ans = (int)1e9;
	while (i < j) {
		if (i & 1) ans = min(ans, dp[k][i++]);
		if (!(j & 1)) ans = min(ans, dp[k][j--]);
		i >>= 1;
		j >>= 1;
	}
	if (i == j) ans = min(ans, dp[k][i]);
	return ans;
}

void upd(int k, int i, int val) {
	dp[k][i + (1 << 17)] = val - i;
	i += 1 << 17;
	i >>= 1;
	while (i) {
		dp[k][i] = min(dp[k][i * 2], dp[k][i * 2 + 1]);
		i >>= 1;
	}
}

int getdp(int k, int i) {
	return dp[k][i + (1 << 17)] + i;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> p[i].first >> p[i].second;
	}
	sort(p, p + n, [](const pair<int, int> &p1, const pair<int, int> &p2) {
		return p1.first + p1.second < p2.first + p2.second;
	});
	for (int i = 0; i < n; i++) {
		tie(x[i], s[i]) = p[i];
	}
	for (int j = 1; j <= m; j++) {
		upd(0, j, max({0, x[0] - s[0] - 1, j - x[0] - s[0]}));
	}
	for (int i = 1; i < n; i++) {
		for (int j = 1; j <= m; j++) {
			int xi = x[i];
			int si = s[i];
			int cost = 0;
			if (j > xi + si) {
				cost += j - (xi + si);
				si += cost;
			}
			int rms = min(j, max(0, min(x[i - 1] + s[i - 1], xi - si - 1)));
			int rmx = min(j, max(0, xi - si - 1));
			// int rms = max(0, min(j, min(x[i - 1] + s[i - 1], xi - si - 1)));
			// int rmx = min(j, max(x[i - 1] + s[i - 1], xi - si - 1));
			upd(i, j, cost + rmq(i - 1, rms, rmx) + rmx);
			// debug("dp[%d][%d] = %d, cf. %d %d\n", i, j, getdp(i, j), rms, rmx);
		}
	}
	cout << getdp(n - 1, m) << endl;
	return 0;
}