#include<bits/stdc++.h>
using namespace std;
 
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(c) (c).begin(),(c).end()
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
typedef long double ld;
typedef pair<ll,ll> pii;
 
const ll inf = 1e9;
const ll inf64 = 1e18;
const ll MOD = inf + 7;
 
void solve() 
{
	ll n,k;
	cin>>n>>k;
	string s=std::to_string(n);
	ll last=n;
	for(ll i=1;i<=k;i++)
	{
		//cout<<last<<"\n";
		string s1=std::to_string(last);
		sort(s1.begin(),s1.end());
		string s2=s1;
		sort(s2.rbegin(),s2.rend());
		ll g=stoi(s2);
		ll r=stoi(s1);
		last=g-r;
	}
	cout<<last;
}
 
int main() 
{
   fast;
   ll t=1;
   //cin>>t;
   while (t--)  solve();
}
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    const auto g1 = [](long a) {
        std::vector<long> v;
        while (a > 0) {
            v.push_back(a % 10);
            a /= 10;
        }

        std::sort(v.begin(), v.end(), std::greater<long>());

        long ret = 0;
        long n = 1;
        for (auto it = v.rbegin(); it != v.rend(); ++it, n *= 10) {
            ret += *it * n;
        }

        return ret;
    };

    const auto g2 = [](long a) {
        std::vector<long> v;
        while (a > 0) {
            v.push_back(a % 10);
            a /= 10;
        }

        std::sort(v.begin(), v.end());

        long ret = 0;
        long n = 1;
        for (auto it = v.rbegin(); it != v.rend(); ++it, n *= 10) {
            ret += *it * n;
        }

        return ret;
    };

    const auto f = [&g1, &g2](long a) { return g1(a) - g2(a); };

    long n, k;
    std::cin >> n >> k;

    long ret = n;
    for (long i = 0; i < k; ++i) {
        ret = f(ret);
    }

    std::cout << ret << std::endl;
    return 0;
}
