#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <iomanip>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
 
#define debug(x) cout << #x << " = " << x << endl
#define fori(i, ini, lim) for(ll i = ll(ini); i < ll(lim); i++)
#define ford(i, ini, lim) for(ll i = ll(ini); i >= ll(lim); i--)
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
 
ll g1(ll x) {
    if (x == 0) return 0;
    string s = to_string(x);
    sort(begin(s), end(s), greater<char>());
    return stoi(s);
}

ll g2(ll x) {
    if (x == 0) return 0;
    string s = to_string(x);
    sort(begin(s), end(s));
    return stoi(s);
}

ll f(ll x) {
    return g1(x) - g2(x);
}

void solve() {
    ll n, k;
    cin >> n >> k;
    ll x = n;
    fori (i, 0, k) {
        x = f(x);
    }
    cout << x << endl;
}
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    solve();
    return 0;
}

#include<bits/stdc++.h>
#include<string>
using namespace std;
//functions like ceil() and floor() are used to truncate the decimal values
//countsort is great

#define ll long long 
#define int long long 
#define vi vector<int>
#define vlli vector<ll>
#define pii pair<int,int>
#define pb push_back
#define mpr make_pair
#define ft first
#define sec second
#define f0r(n) for(int i=0;i<n;i++)
#define f1r(n) for(int j=0;j<n;j++)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define fastio(); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define txtinp(); freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define inp(n,arr); for(int i=0;i<n;i++){cin>>arr[i];}
#define srt(arr); sort(arr.begin(),arr.end());
#define rsrt(arr); sort(arr.rbegin(),arr.rend());

int dp[2];

int solve(int n){
	vi arr;
	while(n){
		arr.pb(n%10);
		n /= 10;
	}
	int s = arr.size();
	srt(arr);
	dp[0] = 0;dp[1] = 0;
	f0r(s){
		dp[0] = dp[0]*10 + arr[i]; 
		dp[1] = dp[1]*10 + arr[s-1-i]; 
	}

	return abs(dp[0] - dp[1]);
}

signed main(){
    fastio();
    // txtinp();
    int testc=1;//cin>>testc;
    while(testc--){
    	int n,k;cin>>n>>k;
    	int ans = n;
    	f0r(k){
    		ans = solve(ans);
    	}cout<<ans<<endl;
    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
    return 0;
}
