#include <bits/stdc++.h>
// #include <atcoder/all>
// #include "icld.cpp"
using namespace std;
using ll = long long int;
using vi = vector<int>;
using si = set<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using ss = string;
using db = double;
template<typename T> using minpq = priority_queue <T,vector<T>,greater<T>>;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
#define V vector
#define P pair<int,int>
#define PLL pair<ll,ll>
#define rep(i,s,n) for(int i=(s);i<(int)(n);i++)
#define rev(i,s,n) for(int i=(s);i>=(int)(n);i--)
#define reciv(v,n) vi (v)((n)); rep(i,0,(n))cin>>v[i]
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ci(x) cin >> x
#define cii(x) ll x;cin >> x
#define cci(x,y) ll x,y;cin >> x >> y
#define co(x) cout << x << endl
#define pb push_back
#define eb emplace_back
#define rz resize
#define pu push
#define sz(x) int(x.size())
#define vij v[i][j]
// ll p = 1e9+7;
// ll p = 998244353;
#define yn cout<<"Yes"<<endl;else cout<<"No"<<endl
#define YN cout<<"YES"<<endl;else cout<<"NO"<<endl
template<class T>void chmax(T &x,T y){x=max(x,y);}
template<class T>void chmin(T &x,T y){x=min(x,y);}

ll g(ll n, int now){
  ss s=to_string(n);

  if(now==0)sort(all(s));
  else sort(rall(s));

  ll x=stoll(s);
  return x;
}

ll f(ll n){
  return g(n,1)-g(n,0);
}

int main(){
  cci(n,k);
  while(k--)n=f(n);

  co(n);
}
#include<stdio.h>
#include<bits/stdc++.h> 
#include<math.h>
#include<stdlib.h>
#include<stdbool.h>
#include<vector>
#define ll long long
#define pb push_back
#define inp(x) scanf("%d",&x);
#define input(x) scanf("%lld",&x);
#define tc int t;scanf("%d",&t);while(t--)
using namespace std;


//Functions

int MaxNum(int num) 
{ 
  
    int count[10] = {0}; 
  
     
    string str = to_string(num); 
  
   
    for (int i=0; i<str.length(); i++) 
        count[str[i]-'0']++; 
    int result = 0, multiplier = 1; 
    for (int i = 0; i <= 9; i++) 
    { 
        while (count[i] > 0) 
        { 
            result = result + (i * multiplier); 
            count[i]--; 
            multiplier = multiplier * 10; 
        } 
    } 
    return result; 
} 
int MinNum(int num) 
{ 
  
    int count[10] = {0}; 
  
     
    string str = to_string(num); 
  
   
    for (int i=0; i<str.length(); i++) 
        count[str[i]-'0']++; 
    int result = 0, multiplier = 1; 
    for (int i = 9; i >= 0; i--) 
    { 
        while (count[i] > 0) 
        { 
            result = result + (i * multiplier); 
            count[i]--; 
            multiplier = multiplier * 10; 
        } 
    } 
    return result; 
} 


int main()
{
    int n,k;
    cin >> n >>k;
    int temp = n;
    while(k--)
    {
        temp = MaxNum(temp) - MinNum(temp);
        if(temp == 0) break;
    }
    cout << temp;
    
} 