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

using ll = long long;
using ii = pair<ll, ll>;

#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mp make_pair

const ll N = 2e5 + 5;
const ll MOD = 1e9 + 7;
const ll INF = 2e9;
const ll dr4[] = {1, -1, 0, 0};
const ll dc4[] = {0, 0, 1, -1};
const ll dr8[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const ll dc8[] = {0, 1, 1, 1, 0, -1, -1, -1};

ll n, k;

void solve() {
  cin >> n >> k;

  const auto G1 = [&](ll x) {
    ll freq[10];
    memset(freq, 0, sizeof(freq));
    while(x) {
      freq[x % 10]++;
      x /= 10;
    }

    ll ret = 0;
    for(ll i = 9; i >= 0; i--) {
      for(ll j = 1; j <= freq[i]; j++) {
        ret = ret * 10 + i;
      }
    }

    return ret;
  };

  const auto G2 = [&](ll x) {
    ll freq[10];
    memset(freq, 0, sizeof(freq));
    while(x) {
      freq[x % 10]++;
      x /= 10;
    }

    ll ret = 0;
    for(ll i = 1; i <= 9; i++) {
      for(ll j = 1; j <= freq[i]; j++) {
        ret = ret * 10 + i;
      }
    }

    return ret;
  };

  ll ans = n;
  for(ll i = 0; i < k; i++) {
    ll g1 = G1(ans);
    ll g2 = G2(ans);
    ans = g1 - g2;
  }

  cout << ans << "\n";
}

int main() {

  ios_base::sync_with_stdio(0);
  // cin.tie(0); cout.tie(0);

  ll T = 1;
  // cin >> T;
  for(ll tc = 1; tc <= T; tc++) {
    solve();
  }

}
#ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using PIL = pair<int,ll>;
using PLI = pair<ll,int>;
using PLL = pair<ll,ll>;
using Graph = vector<vector<int>>;
using Cost_Graph = vector<vector<PIL>>;

template<class T> bool chmin(T &a, T b) {if(a>b){a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b){a=b;return 1;}return 0;}
#define rep(i,n) for(int i=0;i<int(n);i++)
#define roundup(a,b) (a+b-1)/b
#define YESNO(T) if(T){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define yesno(T) if(T){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}
#define YesNo(T) if(T){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}

const ll INF = 1LL << 60;
const ll MOD = 1000000007LL;
const double pi = 3.14159265358979;

ll func(int x){
    string s = to_string(x);
    string t = to_string(x);

    sort(s.begin(), s.end());
    sort(t.begin(), t.end());
    reverse(t.begin(), t.end());

    ll big = stoll(t);
    ll small = stoll(s);

    return (big - small);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);

    ll n,k;
    cin >> n >> k;

    for (int i = 0; i < k; i++){
        n = func(n);
    }

    cout << n << endl;
}