
// Problem : C - Friends and Travel costs
// Contest : AtCoder - AtCoder Beginner Contest 203（Sponsored by Panasonic）
// URL : https://atcoder.jp/contests/abc203/tasks/abc203_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
 
#define endl  "\n"
#define int   long long
#define FAST  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef pair<int,int>   pii;
typedef vector<pii>     vii;
#define rep(i,n)        for(int i=0;i<(n);i++)
#define pb              push_back
#define ff              first
#define ss              second
#define sz(x)           ((int)(x).size())
#define all(x)          (x).begin(), (x).end()
#define INF             (long long) 1e13
#define MOD             1000000007
#define MAX             (int) 2e5+5


void solve() {
	
}

int32_t main(){
    FAST
    int n,k;
    cin>>n>>k;
    int best = 0;
    vector<pii> a(n);
    for(int i=0;i<n;i++){
        cin >> a[i].ff >> a[i].ss;
    }
    sort(all(a));
    for(int i=0;i<n;i++){
        if(abs(a[i].ff-best)<=k){
            k -= a[i].ff-best;
            best = a[i].ff;
            k += a[i].ss;
        }
    }
    best += k;
    cout << best << endl;
    return 0;
}








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

int main() {
	int n,k;
	cin>>n>>k;
	string p=to_string(n);
	for(int i=1;i<=k;i++){
		string tmp1=p,tmp2=p;
		sort(tmp1.begin(),tmp1.end());
		sort(tmp2.begin(),tmp2.end(),greater<int>());
		p=to_string(stoi(tmp2)-stoi(tmp1));
		
	}
	cout<<p<<endl;
	return 0;
}