//AUTHOR: RAVAN_2070
//PUNE INSTITUTE OF COMPUTER TECHNOLOGY
/*                                                    
      I ♥ CLARICE STARLING...                                                               
        EXPLAINATION BELOW->
*/
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<pair<ll,ll>> vii;
typedef pair<ll,ll> pii;
typedef map<ll,ll> mii;
#define MOD7 1000000007
#define MOD9 1000000009
#define pi 3.1415926535
#define Test_cases ll TC;cin>>TC;while(TC--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(), x.rend()
#define sp(x) fixed<<setprecision(x)
#define sz(x) (ll)(x.size())
#define fo(i,a,b) for(i=a;i<b;i++)
#define foe(i,a,b) for(i=a;i<=b;i++)
const ll INF = (ll)2e18 + 77;
ll num(ll a,int f)
{
    vector<ll>d;
    while(a)
    {
        d.pb(a%10);
        a/=10;
    }
    if(f==0)sort(all(d));
    else sort(all(d),greater<ll>());
    ll ans=0ll;
    for(auto dg:d)
    {
        ans*=10;
        ans+=dg;
    }
    return ans;
}
void solve()
{
    ll N,K,i;cin>>N>>K;
    ll a=N;
    foe(i,1,K)
    {
       a=(num(a,1)-num(a,0)); 
    }
    cout<<a<<"\n";
}
int main()
{
   fastio
   //Test_cases
   solve();
  return 0;
}

#include <bits/stdc++.h> 
using namespace std; 
  
// function to count the no of removal of digits 
// to make a very large number divisible by 3 
int divisible(string num) 
{ 
    int n = num.length(); 
  
    // add up all the digits of num 
    int sum = accumulate(begin(num), end(num), 0) - '0' * 1; 
  
    // if num is already is divisible by 3 
    // then no digits are to be removed 
    if (sum % 3 == 0) 
        return 0; 
  
    // if there is single digit, then it is 
    // not possible to remove one digit. 
    if (n == 1) 
        return -1; 
  
    // traverse through the number and find out 
    // if any number on removal makes the sum 
    // divisible by 3 
    for (int i = 0; i < n; i++) 
        if (sum % 3 == (num[i] - '0') % 3) 
            return 1; 
  
    // if there are two numbers then it is 
    // not possible to remove two digits. 
    if (n == 2) 
        return -1; 
  
    // Otherwise we can always make a number 
    // multiple of 2 by removing 2 digits. 
    return 2; 
} 
  
// driver program to test the above function 
int main() 
{ 
    string num ;cin>>num; 
    cout << divisible(num); 
    return 0; 
} 