#include<iostream>
#include <string>       // std::string
#include <iostream>     // std::cout
#include <sstream>
#include <algorithm>    // std::sort
#include <vector>  
#define ll long long
using namespace std;

ll conv(ll n)
{
	std::stringstream ss;
	ss << n;
	string s,sc;
	ss>>s;
	sort(s.begin(), s.end());
	sc=s;
	reverse(s.begin(), s.end());

	ll a,b;
		std::stringstream s1,s2;
		s1<<s;
		s2<<sc;
		s1>>a;
		s2>>b;

	return a-b;

}


void solve()
{
	ll n, k;
	cin >> n >> k;
	while(k--)
	{
		n=conv(n);
	}
	cout<<n<<endl;


}

int main()
{	//fio;


	solve();


	//pf("\n\n\n\n"); Time();
	return 0;
}

//#include <bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<unordered_map>
#include<queue>

#define INF 0x3f3f3f3f
#define ll long long
#define LL long long
#define ull unsigned long long
#define endl '\n'
#define ios ios::sync_with_stdio(false);
#define open freopen("in.in","w",stdout);
using namespace std;
const int maxn = 1e5 + 10;
const ll mod = 1e9 + 7;
int a[15],b[15];
int main() {
    int n,k,la=-1;
    cin>>n>>k;
    while(k--){
        if(n==la)break;
        la=n;
        int cnt=0,tmp = n;
        while(tmp){
            a[cnt]=tmp%10;
            b[cnt]=a[cnt];
            cnt++;
            tmp/=10;
        }
        sort(a,a+cnt,less<int>());
        sort(b,b+cnt,greater<int>());
        int p=1;
        tmp=0;
        for(int i=0;i<cnt;i++){
            if(a[i]<b[i]){
                a[i+1]--;
                a[i]=a[i]+10;
            }
            a[i]-=b[i];
            tmp=tmp+p*a[i];
            p*=10;
        }
        n=tmp;
//        cout<<n<<endl;
    }
    cout<<n<<endl;
    return 0;
}


