#ifdef _DEBUG
    #define dout(x) clog << "Line " << __LINE__ << ": " << #x << "=" << (x) << el
#else
    #pragma GCC optimize("Ofast,unroll-loops")
    #pragma GCC target("avx,avx2,fma")
    #define dout(x)
#endif

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

#define MP(a, b) make_pair(a, b)
#define PB(x) push_back(x)
#define EB(args...) emplace_back(args)
#define F first
#define S second
#define die exit(0)

template<typename T>
using vc = vector<T>;
template<typename T>
using uset = unordered_set<T>;
template<typename A, typename B>
using umap = unordered_map<A, B>;
template<typename T, typename Comp = less<T>>
using pq = priority_queue<T, vc<T>, Comp>;
template<typename T>
using minpq = pq<T, greater<T>>;

using db = double;
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using tri = tuple<int, int, int>;
using vi = vc<int>;
using vii = vc<pii>;
using str = string;

constexpr char el = '\n';
constexpr char sp = ' ';
constexpr int INF = 0x3f3f3f3f;
constexpr ll LLINF = 0x3f3f3f3f3f3f3f3fLL;
// ---------------------------------------------------------------------


int a[10];
int n, k;

int g1(int x){
    for(int& x: a)
        x = 0;

    while(x>0){
        ++a[x%10];
        x/=10;
    }
    int ans = 0;
    for(int i = 9; i >= 0; --i)
        while(a[i]--){
            ans *= 10;
            ans += i;
        }

    return ans;
}

int g2(int x){
    for(int& x: a)
        x = 0;
    while(x>0){
        ++a[x%10];
        x/=10;
    }
    int ans = 0;
    for(int i = 1; i < 10; ++i)
        while(a[i]--){
            ans *= 10;
            ans += i;
        }

    return ans;
}

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cout << fixed; clog << fixed;
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("debug.txt", "w", stderr);
#endif

    cin >> n >> k;
    while(k--)
        n = g1(n) - g2(n);

    cout << n;
}

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>


std::pair<std::vector<int>,bool> fct(std::vector<int>z) {
	std::vector<int> a(z.begin(), z.end());
	std::sort(a.rbegin(), a.rend());
	std::vector<int> b(a.rbegin(), a.rend());
	int taille = a.size();
	std::vector<int>ans(taille);
	bool ok = true;
	for (int i = (taille - 1); i >= 0; i--) {
		if (a[i] < b[i]) {
			ans[i] = 10 + a[i] - b[i];
			if (ans[i] != z[i])ok = false;
			if (i >= 1)b[i - 1]++;
		}
		else {
			ans[i] = a[i] - b[i]; if (z[i] != ans[i])ok = false;
		}
	}
	int index = ans.size()-1;
	for (int i = 0; i < ans.size(); i++) {
		if (ans[i] != 0) {
			index = i;
			break;
		}
	}
	std::vector<int> aa;
	for (int i = index; i < ans.size(); i++)aa.emplace_back(ans[i]);
	
	return make_pair(aa,ok);
}


int main() {
	//int t;
	//std::cin >> t;
	//while (t--) {
		int n, k;
		std::cin >> n >> k;
			if (k == 0) {
				std::cout << n;
				return 0;
			}
		std::vector<int> a;
		int tmp = n;
		while (tmp) {
			a.emplace_back(tmp % 10);
			tmp /= 10;
		}
		//aa.resize(a.size() / 2);
		while (k--) {
			std::pair<std::vector<int>, bool> response = fct(a);
			a.resize(response.first.size());
			a = response.first;
			if (response.second == true) {
				break;
			}
			//for (auto x : a)std::cout << x;
			//std::cout << "\n";
		}
		 for (auto x : a)std::cout << x;
		//std::cout << "\n";
	//}
}