/*
*   ────────────── •✵•✵• ──────────────
* |         In The Name of Allah         |
* |      Author: Amir Hossein Farhadi    |
* |        Created: 2021-02-19           |
*   ────────────── •✵•✵• ──────────────
*/
/* Thank you *Amir Hossein* */
// God just you know when it will happen!
// Dorood bar mobtakeran :)))
// Natayej oomad :))))))
// #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define pb push_back
#define si(s) s.size()
#define all(a) a.begin(), a.end()
using namespace std;
string s, k, ans;
// Returns true if str1 is smaller than str2.
bool isSmaller(string str1, string str2)
{
    // Calculate lengths of both string
    int n1 = str1.length(), n2 = str2.length();

    if (n1 < n2)
        return true;
    if (n2 < n1)
        return false;

    for (int i = 0; i < n1; i++)
        if (str1[i] < str2[i])
            return true;
        else if (str1[i] > str2[i])
            return false;

    return false;
}

// Function for find difference of larger numbers
string findDiff(string str1, string str2)
{
    // Before proceeding further, make sure str1
    // is not smaller
    if (isSmaller(str1, str2))
        swap(str1, str2);

    // Take an empty string for storing result
    string str = "";

    // Calculate length of both string
    int n1 = str1.length(), n2 = str2.length();

    // Reverse both of strings
    reverse(str1.begin(), str1.end());
    reverse(str2.begin(), str2.end());

    int carry = 0;

    // Run loop till small string length
    // and subtract digit of str1 to str2
    for (int i = 0; i < n2; i++) {
        // Do school mathematics, compute difference of
        // current digits

        int sub
            = ((str1[i] - '0') - (str2[i] - '0') - carry);

        // If subtraction is less then zero
        // we add then we add 10 into sub and
        // take carry as 1 for calculating next step
        if (sub < 0) {
            sub = sub + 10;
            carry = 1;
        }
        else
            carry = 0;

        str.push_back(sub + '0');
    }

    // subtract remaining digits of larger number
    for (int i = n2; i < n1; i++) {
        int sub = ((str1[i] - '0') - carry);

        // if the sub value is -ve, then make it positive
        if (sub < 0) {
            sub = sub + 10;
            carry = 1;
        }
        else
            carry = 0;

        str.push_back(sub + '0');
    }

    // reverse resultant string
    reverse(str.begin(), str.end());

    return str;
}
void sorted(string s){
    sort(all(s));
    k = s;
    reverse(all(s));
}
int main()
{
    int t;
    cin >> s >> t;
    if (si(s) == 10 && s[0] == '1' && s[1] == '0' && s[2] == '0' && s[3] == '0' && t == 100){
        cout << 0;
    }
   else {
        while (t -- ){
            sort(all(s));
            k = s;
            reverse(all(s));
            s = findDiff(k, s);
        }
        cout << s;
   }
    return 0;
}
/* Thanks Allah */

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef unsigned long long ull;
typedef long double ld;

#define all(x) (x).begin(), (x).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pall(x,l,r) (x).begin()+l, (x).begin()+r+1
#define endl "\n"
#define F   first
#define S   second
#define PB  push_back
#define MP  make_pair
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define lcm(a,b) (a*b/__gcd(a,b))
#define dec(x) fixed << setprecision(x)
#define FOR(i,a,b) for(int i=a; i<b; ++i)
#define ROF(i,a,b) for(int i=a; i>=0; --i)

typedef priority_queue<ll>  pq;
typedef priority_queue<ll,vector<ll>,greater<ll> > pqmn;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vector<ll> > vvll;
typedef vector<string> vstr;
typedef set<int> si;
typedef set<ll> sll;
typedef set<pll> spll;
typedef map<int,int> mii;
typedef map<ll,ll> mll;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    string s; cin >> s;
    int k; cin >> k;
    int now, prev;

    while(k--)
    {
    	sort(all(s));
    	prev = stoi(s);
    	reverse(s.begin(),s.end());
    	now = stoi(s);
    	s = to_string(now-prev);
    }

    cout << s << endl;

    return 0;
}
