#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
set<string> Set;
LL getMin(string s){
    sort(s.begin(), s.end());
    LL ans = 0;
    for(int i = 0;i<s.size();++i)
    {
        ans = ans *10+s[i]-'0';

    }
    return ans;
}
LL getMax(string s){
    sort(s.begin(), s.end(), greater<LL>());
    LL ans = 0;
    for(int i = 0;i<s.size();++i)
    {
        ans = ans *10+s[i]-'0';

    }
    return ans;
}
string getString(LL d){
    string s="";
    if(d==0)
    {
        return "0";
    }
    while(d){
        s += d%10+'0';
        d/=10;
    }
    reverse(s.begin(),s.end());
    return s;
}
int main() {
    string s;
    LL d;
    cin >> d;
    s = getString(d);
    int n;
    cin >> n;

    LL b=d, a=0;
    while(n--){
         a = getMin(s);
         b = getMax(s);
        s = getString(b-a);
        //cout << a << " " << b << " " << s  << "\n";

    }
    cout <<s << "\n";
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif

int main()
{
	int64_t n, k;
	cin >> n >> k;

	auto f = [](int64_t x) -> int64_t {
		string s(to_string(x)), t(s);

		sort(s.begin(), s.end(), greater<>());
		sort(t.begin(), t.end());

		return stoll(s) - stoll(t);
	};

	int64_t a = n;

	for (int64_t i = 0; i < k; i++)
	{
		a = f(a);
	}

	cout << a << endl;

	return 0;
}