#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<strings.h>
#include<sstream>
using namespace std;
int fan(int x)
{
    int maxn=0,mixn=0;
    int a[10]= {0},b[10]= {0};
    string str;
    stringstream ss;
    ss << x;
    ss >> str;
    if(x==0)
        return 0;
    else
    {
        int len = str.length();
        for(int i = 0; i < len; i ++)
        {
            a[str[i]-'0']++;
            b[str[i]-'0']++;
        }
        //最大值
        for(int i = 9; i >= 0; i--)
        {
            while(a[i]!=0)
            {
                maxn = maxn *10 + i;
                a[i]--;
            }
        }
        //最小值
        for(int i = 1; i <= 9; i ++)
        {
            while(b[i]!=0)
            {
                mixn = mixn *10 + i;
                b[i]--;
            }
        }
        return maxn - mixn;
    }
}
int main()
{
    int x,n,pre=0,now=0;
    scanf("%d%d",&x,&n);
    if(n==0)
        printf("%d\n",x);
    else
    {
        for(int i = 1; i <= n ; i ++)
        {
            pre = now;
            if(i==1)
                now = fan(x);
            else
                now = fan(now);
            if(now==pre)
                break;
        }
        printf("%d\n",now);
    }
    return 0;
}

#include<cstdio>
#include<iostream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<string>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib> 
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl "\n"
#define int long long
//#define double long double
using namespace std;
	typedef long long ll;
	const int maxn=100005;
	const int inf=0x3f3f3f3f;
	int n,m,k;
    int gfun1(int x){
        vector<int>v;
        while(x){
            v.push_back(x%10);
            x/=10;
        }
        sort(v.begin(),v.end());
        int re=0;
        for(auto i:v){
            re*=10;
            re+=i;
        }
        return re;
    }
    int gfun2(int x){
        vector<int>v;
        while(x){
            v.push_back(x%10);
            x/=10;
        }
        sort(v.begin(),v.end());
        reverse(v.begin(),v.end());
        int re=0;
        for(auto i:v){
            re*=10;
            re+=i;
        }
        return re;
    }
    int fun(int x){
        return gfun2(x)-gfun1(x);
    }
    
	signed main(){
		IOS
		#ifndef ONLINE_JUDGE
		    freopen("D:\\_ACM_code\\IO\\in.txt","r",stdin);
		    freopen("D:\\_ACM_code\\IO\\out.txt","w",stdout);
        #endif
        cin>>n>>m;
        int ans=n;
        while(m--){
            int t=fun(ans);
            if(t==ans)
                break;
            ans=t;
        }
        cout<<ans<<endl;
	} 
				