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

vector<int> getIntArray(int num) {
  vector<int> result;
  int tmp = num;
  while(tmp > 0) {
    int digit = tmp % 10;
    result.push_back(digit);
    tmp /= 10;
  }
  return result;
}

int calc(vector<int> v) {
  sort(v.begin(), v.end());
  int greater = 0;
  int less = 0;
  for(int i = 0; i < (int)v.size(); i++) {
    int num = v.at(i);
    greater+=(num * pow(10, i));
    if(num == 0) {
      less*=10;
    } else {
      less+=(num * pow(10, (v.size() - i - 1)));
    }
  }
  return greater - less;
}

int main() {
  int N, K;
  cin >> N >> K;

  int result = N;
  for(int i = 0; i < K; i++) {
    vector<int> intArr = getIntArray(result);
    result = calc(intArr);
  }
  cout << result << endl;
  return 0;
}

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

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl





using ll = long long;


const int maxn = 1e6 + 5;




int g2(int x) {
    string s = to_string(x);
    sort(s.begin(),s.end());
    return stoi(s);
}

int g1(int x) {
    string s = to_string(x);
    sort(s.rbegin(),s.rend());
    return stoi(s);
}

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

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

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

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

    cout<<n<<endl;    
    return 0;
}
