#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

int main(void){

    string  n;
    int     k;
    cin >> n >> k;
    
    for (int i = 0; i < k; i++) {
        vector<int>  g1, g2;
        int          keta = n.size();
        for (int j = 0; j < keta; j++) {
            g1.push_back(n[j] - '0');
            g2.push_back(n[j] - '0');
        }
        sort(g1.begin(), g1.end(), greater<int>());
        sort(g2.begin(), g2.end());
        
        //g1, g2を数字に変換
        long long  num_g1 = 0, num_g2 = 0;
        long long  f;
        for (int j = 0; j < keta; j++) {
            num_g1 += g1[j] * pow(10, keta - j - 1);
            num_g2 += g2[j] * pow(10, keta - j - 1);
        }
        f = num_g1 - num_g2;
        /*
        cout << "n[" << i << "] = " << n << endl; 
        cout << "g1[" << i << "] = " << num_g1;
        cout << " g2[" << i << "] = " << num_g2 << endl;
        cout << "f[" << i << "] = " << f << endl; 
        */
        n = to_string(f);
    }
    
    cout << n << endl;
    
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
 
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         cout<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define pw(b,p)         pow(b,p) + 0.1
#define endl            "\n"
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());


void __print(int32_t x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
 
char myHeap[200 * 1024 * 1024];
int sz = 0;
void assert_tle(bool q) { while (!q); }
void* operator new  ( std::size_t count ) {
    sz += count;
    assert_tle(sz <= 200 * 1024 * 1024);
    return myHeap + sz - count;
}
 
void operator delete (void *ptr) { }
 
 
void fastIO()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

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

    while(k--)
    {
        string g1=to_string(n);
        string g2=to_string(n);

        sort(g1.begin(),g1.end(),greater<int>());
        sort(g2.begin(),g2.end());

        n=stoi(g1)-stoi(g2);
    }

    cout<<n;
}
 
int32_t main()
{
    fastIO();
    //w(t)
    {
        solve();
        cout<<endl;
    }
    return 0;
}