#include <iostream>
#include <iomanip>
#include <algorithm>
#include <array>
#include <cassert>
#include <optional>
#include <utility>
#include <vector>
// #include <atcoder/all>

template <class InputIterator>
std::ostream& range_output(std::ostream& os_arg, InputIterator first_arg, InputIterator last_arg){ if(first_arg != last_arg){ do{ os_arg << *(first_arg++); if(first_arg == last_arg) break; os_arg << ' '; } while(true); }  return os_arg; }
template <class Tp> std::ostream& operator << (std::ostream& os_arg, const std::vector<Tp>& arr_arg){ return range_output(os_arg, arr_arg.cbegin(), arr_arg.cend()); }
template <class Tp, std::size_t Size> std::ostream& operator << (std::ostream& os_arg, const std::array<Tp, Size>& arr_arg){ return range_output(os_arg, arr_arg.cbegin(), arr_arg.cend()); }
template <class S, class T> std::ostream& operator << (std::ostream& os_arg, const std::pair<S, T>& pair_arg){ return os_arg << '(' << pair_arg.first << ", " << pair_arg.second << ')'; }

#ifndef ONLINE_JUDGE
    template <typename Head> void dump_out(Head head_arg){ std::cerr << head_arg << '\n'; }
    template <typename Head, typename... Tail>
    void dump_out(Head head_arg, Tail... tail_args){ std::cerr << head_arg << ", "; dump_out(tail_args...); }
    #define dump(...) do { std::cerr << "[in line " << __LINE__ << "] " << #__VA_ARGS__ << " : "; dump_out(__VA_ARGS__); } while(false)
#else
    #define dump(...) (void(0))
#endif

template <class S, class T> bool chmax(S& x, const T& y){ if(x < y){ x = y; return true; } return false; }
template <class S, class T> bool chmin(S& x, const T& y){ if(x > y){ x = y; return true; } return false; }


int A[100];
inline int next(int n){
    int t = 0; while(n > 0){ A[t++] = n % 10; n /= 10; }
    std::sort(A, A + t);
    int x = 0; for(int i = 0; i < t; ++i) x = 10 * x + A[i];
    int y = 0; for(int i = t - 1; i >= 0; --i) y = 10 * y + A[i];
    return y - x;
}

int main(void){
    std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); 
    std::cout << std::fixed << std::setprecision(16);
    
    int n, k; std::cin >> n >> k;
    while(k--) n = next(n);
    std::cout << n << '\n';
    return 0;
}
#include <iostream>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <iomanip>
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<=n;i++)
#define all(V) V.begin(),V.end()
#define PI 3.14159265359
#define fi fixed
#define se setprecision(10)
using namespace std;

int f(int x){
  string s = to_string(x);
  sort(all(s));
  int g2 = stoi(s);
  reverse(all(s));
  int g1 = stoi(s);
  return g1 - g2;
}
int main() {
  int N,K;
  cin >> N >> K;
  int A = N;
  for(int i=0;i<K;i++) {
    A = f(A);
  }
  cout << A << endl;
}
  