#include <bits/stdc++.h>
#define int long long
using namespace std;
int solve(string s1,string s2)
{
    int x=0,y=0,n1=s1.size(),n2=s2.size();
    for(int i=0,f=0;i<n1;++i)
    {
        if(!f&&s1[i]=='0') continue;
        else f=1,x=(x<<3)+(x<<1)+(s1[i]^48);
    }
    for(int i=0,f=0;i<n2;++i)
    {
        if(!f&&s2[i]=='0') continue;
        else f=1,y=(y<<3)+(y<<1)+(s2[i]^48);
    }
    return x-y;
}
signed main()
{
    int n,k,res;cin>>n>>k;res=n;
    for(int i=1;i<=k;++i)
    {
        string s1=to_string(res);
        sort(s1.begin(),s1.end());
        string s2=s1;
        reverse(s1.begin(),s1.end());
        res=solve(s1,s2);
    }
    cout<<res;return 0;
}


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

using ll = long long;

ll powk(ll a, ll n = 2){
  ll r = 1;
  for(ll i = 0;i < n;i++){
    r *= a;
  }

  return r;
}


int main()
{
  ll n, k;
  cin >> n >> k;

  ll t = n;
  for(ll i = 0;i < k;i++){
    string st;
    st = to_string(t);
    sort(st.begin(),st.end());
    ll m = 0, u = 0;
    for(ll j = 0;j < st.size();j++){
      m += pow(10, j) * (st[st.size() - j - 1] - '0');
      u += pow(10, j) * (st[j] - '0');
    }
    t = u - m;
  }

  cout << t;

  return 0;
}