/*
                    author         : TAPAN SAVANI 
                    codeforces     : savanitapan2001
                    codechef       : savanitapan17
*/

/*
------------------------------------------------------------------------

░██╗░░░░░░░██╗░█████╗░██████╗░██╗░░██╗  ██╗░░██╗░█████╗░██████╗░██████╗░
░██║░░██╗░░██║██╔══██╗██╔══██╗██║░██╔╝  ██║░░██║██╔══██╗██╔══██╗██╔══██╗
░╚██╗████╗██╔╝██║░░██║██████╔╝█████═╝░  ███████║███████║██████╔╝██║░░██║
░░████╔═████║░██║░░██║██╔══██╗██╔═██╗░  ██╔══██║██╔══██║██╔══██╗██║░░██║
░░╚██╔╝░╚██╔╝░╚█████╔╝██║░░██║██║░╚██╗  ██║░░██║██║░░██║██║░░██║██████╔╝
░░░╚═╝░░░╚═╝░░░╚════╝░╚═╝░░╚═╝╚═╝░░╚═╝  ╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚═╝╚═════╝░
 
------------------------------------------------------------------------
*/

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

#define HAPPY_CODING              \
    ios_base::sync_with_stdio(0); \
    cin.tie(NULL);                \
    cout.tie(NULL);
#define pb push_back
#define mp make_pair
#define Debug(x) cout << #x " = " << (x) << endl
#define SORT(a) sort(a.begin(), a.end())
#define SORTR(a) sort(a.rbegin(), a.rend())
#define mod 1000000007
#define pi 3.141592653589793238
#define ll long long int
#define ull unsigned long long
#define be begin()
#define en end()
#define FOR(i, a, b) for (long long int i = a; i < b; i++)
#define FORI(i, a, b) for (int i = a; i >= b; i--)

typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;
typedef vector<PI> VPI;

int main()
{
    HAPPY_CODING;

    int n, k;
    cin >> n >> k;

    if (n == 0 || k == 0)
    {
        cout << n << "\n";
        return 0;
    }

    VI x;

    int temp = n, ans = 0;
    while (temp)
    {
        x.pb(temp % 10);
        temp /= 10;
    }

    while (k--)
    {
        int first = 0;

        SORT(x);
        FOR(i, 0, x.size())
        {
            first *= 10;
            first += x[i];
        }

        int second = 0;
        SORTR(x);
        FOR(i, 0, x.size())
        {
            second *= 10;
            second += x[i];
        }

        ans = second - first;

        int temp = ans;
        x.clear();
        while (temp)
        {
            x.pb(temp % 10);
            temp /= 10;
        }

        if (ans == 0)
            break;
    }

    cout << ans << "\n";

    return 0;
}

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

int g1(int x) {
  priority_queue<int> q;
  int ans = 0;
  while (x > 0) {
    q.push(x % 10);
    x /= 10;
  }
  while (!q.empty()) {
    ans *= 10;
    ans += q.top();
    q.pop();
  }
  return ans;
}

int g2(int x) {
  priority_queue<int, vector<int>, greater<int>> q;
  int ans = 0;
  while (x > 0) {
    q.push(x % 10);
    x /= 10;
  }
  while (!q.empty()) {
    ans *= 10;
    ans += q.top();
    q.pop();
  }
  return ans;
}

int f(int x) { return g1(x) - g2(x); }

int main(void) {
  int N, K;
  cin >> N >> K;
  int a = N;
  for (int i = 0; i < K; ++i) {
    a = f(a);
  }
  printf("%d\n", a);

  return 0;
}
