//                                      CPP
//                                      Mind_flayer404

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define len length()
#define vt vector

#define f(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=b-1; i>=a; i--)

ll const mod = 1e9+7;
ll const mod1 = 998244353;
ll const N = 1e+6;

ll power(ll a,ll b,ll m)
{
    ll res = 1;
    while(b>0)
    {
        if(b&1)
        {
            res = ((res%m)*(a%m))%m;
        }
        a = ((a%m)*(a%m))%m;
        b /= 2;
    }
    return res;
}

ll modi(ll a,ll p)
{
    return power(a,p-2,p);
}

ll max(ll a,ll b)
{
    return a>b?a:b;
}

ll min(ll a,ll b)
{
    return a>b?b:a;
}

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll bins(ll ar[],ll l,ll r,ll x)
{
    if(l<=r)
    {
        ll mid = l + (r-1)/2;
        if(ar[mid]==x)
            return mid;
        else if(ar[mid]>x)
        {
            bins(ar,l,mid-1,x);
        }
        else if(ar[mid]<x)
        {
            bins(ar,mid+1,r,x);
        }
    }
    return -1;
}

ll get(ll n)
{
    vt<ll> v;
    while(n>0)
    {
        v.pb(n%10);
        n /= 10;
    }
    ll a = 0,b = 0;
    sort(all(v));
    ll sz = v.size();
    rf(i,0,sz)
    {
        a += v[i]*pow(10,i);
    }
    f(i,0,sz)
    {
        b += v[i]*pow(10,sz - i - 1);
    }
    return a - b;
}

int main()
{
    fast;
    ll t = 1;
    //cin>>t;
    while(t--)
    {
        ll n,k;
        cin>>n>>k;
        ll ar[k+1];
        ar[0] = n;
        f(i,1,k+1)
        {
            ar[i] = get(ar[i-1]);
        }
        cout<<ar[k]<<endl;
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void run(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif 
}
int karp(int n){
	int f[11]={};
	int z=1,y=1;
	while(n>0){
		f[n%10]++;
		n/=10;
	}
		for(int i=9;i>=0;i--){
			for(int j=0;j<f[i];j++){
				y = y*10+i;
			}
		}
		for(int i=0;i<=9;i++){
			for(int j=0;j<f[i];j++){
			 z = z*10+i;
			}
		}
		return (y-z);
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	run();
	int n,k; cin>>n>>k; 
	for(int i=0;i<k;i++){
	   n = karp(n);
	}
	cout<<n<<endl;
}
															