#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>

using namespace std;

using ll = long long;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define sz(x) (int)(x).size()
#define ms(arr, v) memset(arr, v, sizeof(arr))
#define f first
#define s second
#define pb push_back

// clang-format off
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T_vector>void output_vector(const T_vector &v, bool add_one = false, int start = -1, int end = -1) {if (start < 0) start = 0; if (end < 0) end = int(v.size()); for (int i = start; i < end; i++) cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\n');}
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << "(" << v.first << ":" << v.second << ")" << ","; os << "}"; return os; }


auto random_address = [] {char *p = new char; delete p;return uint64_t(p);};
const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1);
mt19937_64 rng(SEED);
// clang-format on

void solve() { }
vector<int> to_int(int x) {
     vector<int> a;
     while (x) {
          int s = x % 10;
          a.push_back(s);
          x /= 10;
     }
     return a;
}

int g1(vector<int> x) {
     int ans = 0;
     sort(x.rbegin(), x.rend());
     // 314
     int z = 1;
     for (int i = sz(x) - 1; i >= 0; --i) {
          ans += z * x[i];
          z *= 10;
     }
     return ans;
}
int g2(vector<int> x) {
     int ans = 0;
     sort(x.begin(), x.end());
     // 314
     int z = 1;
     for (int i = sz(x) - 1; i >= 0; --i) {
          ans += z * x[i];
          z *= 10;
     }
     return ans;
}
int main() {
     ios::sync_with_stdio(0);
     cin.tie(0);
     int n, k;
     cin >> n >> k;
     int x = n;
     vector<int> ans;
     for (int i = 0; i < k; i++) {
          auto d = to_int(x);
          int t = g1(d) - g2(d);
          x = t;
          ans.push_back(t);
     }
     if (k == 0)
          cout << n;
     else {
          cout << ans[sz(ans) - 1];
     }
     // int tests;
     // cin >> tests;
     // while (tests-- > 0)
     //      solve();
}

#include <bits/stdc++.h>
#define speedup                                                                \
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr),          \
        cout.precision(12)
using namespace std;

#ifndef ONLINE_JUDGE
#define eprintf(...) fprintf(stderr,__VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) void(0)
#endif
using ll = long long;
using str = string;
using vi = vector<int>;
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define sz(x) int((x).size())

ll nxt(ll N) {
	vi d;
	while(N) {
		d.pb(N%10);
		N/=10;
	}
	sort(all(d));
	ll a=0,b=0,p=1;
	for(const auto& it:d) {
		a=a*10+it;
		b=it*p+b;
		p*=10;
	}
	return b-a;
}

int main() {
	speedup;
	ll N,k;
	cin>>N>>k;
	while(k--&&N>0) {
		N=nxt(N);
	}
	cout<<N<<'\n';	
}
