#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>

using namespace std;

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

#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define X first
#define Y second
#define pb push_back
#define sz(s) int64_t(s.size())
#define make_unique(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())

const ll mod = 1e9 + 7;


template<class T>
istream& operator >> (istream& in, vector<T>& v){ for (auto &x : v) { in >> x; } return in; }

template<class T, class U>
istream& operator >> (istream& in, pair<T, U> & v){ in >> v.X >> v.Y;return in; }

template<class T, class U>
ostream& operator << (ostream& out, pair<T, U> & v){ out << v.X << " " << v.Y;return out; }


template<class T, class U>
void chkmax(T &a, U b) {
    a = max(a, (T)b);
    return;
}

template<class T, class U>
void chkmin(T &a, U b) {
    a = min(a, (T)b);
    return;
}

ll ppow (ll x, ll s) {
    if (!s) return 1;
    if (!(s - 1)) return x % mod;
    if (s % 2) return (x * ppow (x, s - 1)) % mod;
    ll b = ppow (x, s / 2);
    return (b * b) % mod;
}

vector<int> zf (string s) {

	int n = sz(s);

	vector<int> z (n);

	for (int i = 1, l = 0, r = 0; i < n; i++) {

		if (i <= r)
			z[i] = min (r - i + 1, z[i - l]);

		while (i + z[i] < n && s[z[i]] == s[i + z[i]])
			z[i]++;

		if (i + z[i] - 1 > r)
			l = i,  r = i + z[i] - 1;

	}

	return z;

}

vector<int> pf(string p) {
    int n = sz(p);
    vector<int> pref_fun(n, 0);

    for (int i = 1;i < n;i++) {
        int j = pref_fun[i - 1];
        while (j > 0 && p[i] != p[j])
            j = pref_fun[j - 1];
        if (p[i] == p[j])
            j++;
        pref_fun[i] = j;
    }
    return pref_fun;
}

vector<vector<int> > mul(vector<vector<int> > &a, vector<vector<int> > &b) {
    int n = sz(a);
    vector<vector<int> > c(n, vector<int> (n, 0));

    for (int i = 0;i < n;i++) {
        for (int j = 0;j < n;j++) {
            for (int k = 0;k < n;k++) {
                ll ga = a[i][k];
                ga *= b[k][j];
                ga %= mod;
                c[i][j] += ga;
                if (c[i][j] >= mod) {
                    c[i][j] -= mod;
                }
            }
        }
    }

    return c;
}

vector<vector<int> > matrix_pow(vector<vector<int> > a, ll y) {
    int n = sz(a);
    vector<vector<int> > res(n, vector<int> (n, 0));
    for (int i = 0;i < n;i++) res[i][i] = 1;
    while (y) {
        if (y%2 == 1) {
            res = mul(res, a);
            y--;
        }else {
            a = mul(a, a);
            y /= 2;
        }
    }
    return res;
}

ll f(ll x) {
    vector<int> gg;
    while (x) {
        gg.pb(x%10);
        x /= 10;
    }
    sort(all(gg));
    ll res1 = 0, res2 = 0;
    for (auto x : gg) res1 = res1 * 10 + x;
    reverse(all(gg));
    for (auto x : gg) res2 = res2 * 10 + x;
    return max(res1, res2) - min(res1, res2);
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    ll n, k;
    cin >> n >> k;

    for (int i = 0;i < k;i++) n = f(n);

    cout << n;

    return 0;
}



/*
    @uthor: Amit Kumar
    user -->GitHub: drviruses ; CodeChef: dr_virus_ ; Codeforces,AtCoder,Hackerearth,Hakerrank: dr_virus;
*/
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
using namespace std::chrono;
//#include <boost/multiprecision/cpp_int.hpp>
//namespace mp = boost::multiprecision;
//#define ln mp::cpp_int;
#define int      long long
#define double   long double
#define uint     unsigned long long
#define all(vec) vec.begin(),vec.end()
#define endl "\n"
int google_itr = 1;
#define google(x) cout<<"Case #"<<x<<": "
#define pi 3.14159265358979323846264338327950L

vector<string> vec_splitter(string s) {
        s += ',';
        vector<string> res;
        while(!s.empty()) {
                res.push_back(s.substr(0, s.find(',')));
                s = s.substr(s.find(',') + 1);
        }
        return res;
}
void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx, 
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; } 
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
        if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
        stringstream ss; ss << H;
        cerr << args[idx] << " = " << ss.str();
        debug_out(args, idx + 1, LINE_NUM, T...);
}

#define XOX
#ifdef XOX
    #define deb(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
    #define deb(...) 42
#endif

const int mod = 1e9+7;
const int inf = 1e18;


void virus(){
        int n, k;
        cin >> n >> k;

        int last = n;
        auto inc = [&](int num) {
            string s = to_string(num);
            sort(all(s));
            return stoll(s);
        };
        auto dec = [&](int num) {
            string s = to_string(num);
            sort(all(s), greater<char>());
            return stoll(s);
        };
        for(auto i=0; i<k; i++) {
            int curr = dec(last) - inc(last);
            last = curr;
        }

        cout << last << endl;
}


int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
  /*#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif*/

    int t = 1;
    //cin >> t;
    while(t--){
           auto start = system_clock::now();
           virus();
           auto stop = system_clock::now();
           duration<double> duration = (stop - start);
           //cerr << "\nTime: "<< fixed << setprecision(10) << (double)duration.count() <<"sec"<< endl;
           //your code goes here
    }
    return 0;
}