#include "bits/stdc++.h"
#include<sstream>
using namespace std;
typedef long long ll;
#define _USE_MATH_DEFINES
#include <math.h>
#define NIL = -1;
#define all(x) x.begin(),x.end()
const ll INF = 1e9;
const long long inf = 1e18;
const ll INFL = 1e18;
const ll MOD = 1e9 + 7;

int digit(ll x) {
    int digits = 0;
 
    while(x > 0){
        x /= 10;
        digits++;
    }
 
    return digits;
}
 
ll gcd(long long a,long long b) {
    if (a < b) swap(a,b);
    if (b == 0) return a;
    
    return gcd(b,a%b);
}
 
bool is_prime(long long N){
    if (N == 1) return false;
    for (long long i = 2;i * i <= N;i++){
        if (N % i == 0) return false;
    }
    return true;
}
 
ll lcm(ll a,ll b){
    return ((a * b == 0)) ? 0 : (a / gcd(a,b) * b);
}
 
double DegreeToRadian(double degree){
    return degree * M_PI / 180.0;
}
 
long long modpow(long long a, long long b, long long m){
    long long ans = 1;
    while(b > 0){
        if (b % 2 == 1){
            ans *= a;
            ans %= m;
        }
        a *= a;
        a %= m;
        b /= 2;
    }
    return ans;
}

long long comb(long long x, long long y){
    int z;
    if (y == 0) return 1;
    else {
        z = modpow(x, y/2, MOD)*modpow(x, y/2, MOD)%MOD;
        if (y % 2 == 1) z = z*x%MOD;
        return z;
    }
}


vector<pair<long long, long long>> prime_fact(long long x){
    vector<pair<long long, long long>> res;
    for(int i = 2;i*i <= x;i++){
        if (x % i == 0){
            long long cnt = 0;
            while(x % i == 0){
                cnt++;
                x /= i;
            }
            res.push_back({i, cnt});
        }
    }
    if (x != 1){
        res.push_back({x, 1});
    }
    return res;
}

int64_t mod_inv(int64_t a, int64_t m){
    int64_t b = m, u = 1, v = 0;

    while(b){
        int64_t t = a/b;
        a -= t*b;
        swap(a, b);
        u -= t*v;
        swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;

    return u;
}


// {g, x, y}: ax + by = g
tuple<long long, long long, long long> extgcd(long long a, long long b){
    if (b == 0) return {a, 1, 0};
    long long g, x, y;
    tie(g, x, y) = extgcd(b, a % b);
    return {g, y, x - a/b*y};
}

int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};

 
//////////////////////////////////////////////////////////////


int main(){
    int n,k;
    cin >> n >> k;
    for(int i = 0;i < k;i++){
        string tmp = to_string(n);
        sort(tmp.begin(), tmp.end());
        int mini = stoi(tmp);
        sort(tmp.begin(), tmp.end(), greater<>());
        int mx = stoi(tmp);
        n = mx - mini;
    }
    cout << n << endl;
}
#include<bits/stdc++.h>
using namespace std;
#define rep(i, N) for(int i = 0; i < (int)N; i++)
#define all(v) v.begin(), v.end()
#define MOD 1000000007

long int powi(long int a, long int n){
    int re = 1;
    rep(i, n) re *= a;
    return re;
}
long int func(long int a){
    long int re = 0;
    vector<long int> v;
    while(a > 0){
        v.push_back(a % 10);
        a /= 10;
    }
    sort(all(v));
    rep(i, v.size()) re += powi(10,i) * (v.at(i)-v.at(v.size()-1-i));
    return re;
}
int main(){
    long int N, K, ans;
    cin >> N >> K;
    ans = N;
    rep(i, K) ans = func(ans);
    cout << ans << endl;
}