#include <bits/stdc++.h>
using namespace std;

void setIO(string s) {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s + ".in").c_str(), "r", stdin);
//    freopen((s + ".out").c_str(), "w", stdout);
}

int descending(string a)
{
    int arr[a.length()];
    for (int i = 0; i < a.length(); i++)
    {
        arr[i] = a[i] - '0';
    }
    sort(arr, arr + (sizeof(arr) / sizeof(arr[0])), greater<int>());

    for (int i = 0; i < a.length(); i++)
    {
        a[i] = arr[i] + '0';
    }
    int ab = stoi(a); 
    return ab;
}

int ascending(string a)
{
    int arr[a.length()];
    for (int i = 0; i < a.length(); i++)
    {
        arr[i] = a[i] - '0';
    }
    sort(arr, arr + (sizeof(arr) / sizeof(arr[0])));

    bool zero = true;
    string s = "";
    for (int i = 0; i < a.length(); i++)
    {
        if (arr[i] != 0) zero = false;
        else continue;

        if (!zero) s.push_back(arr[i] + '0');
    }
    int ab = stoi(s); 
    return ab;
}

int main() 
{
    setIO("");

    string n; cin >> n;
    int k; cin >> k;
    for (int i = 0; i < k; i++)
    {
        int nDup = descending(n) - ascending(n);        
        n = to_string(nDup);
        if (n == "0") break;
    }
    cout << n;
}
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;

int ak(int num){
    string n = to_string(num);

    sort(n.begin(), n.end());
    int g2 = stoi(n);
    
    reverse(n.begin(), n.end());
    int g1 = stoi(n);
    
    return g1- g2;
}

int main(){
    int n; cin >> n;
    int k; cin >> k;
    for(int i=0;i<k;i++){
        n = ak(n);
    }
    cout << n;
    return 0;
}