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

#define ll long long

ll converter(ll number)
{
    ll num1,  num2, res;
    string num = to_string(number);
    
    sort(num.begin(), num.end(), greater<char>());
  	num1 = stoll(num);
  	
  	sort(num.begin(), num.end());
    num2 = stoll(num);
    
  	res = num1-num2;
  	
  	return res;
}

int main(int argc, char const *argv[])
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
  	
  	ll num,res;
  	int k;
  	cin >> num >> k;
  	res = num;
  	for(int i=0; i<k; i++){
  	    res = converter(res);
  	}
  	
  	cout << res << "\n";
  
  	return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define fo(a,b) for(int64_t a=0;a<b;a++)
#define sor(a) sort(a.begin(),a.end())
#define rev(a) reverse(a.begin(),a.end())
#define ll int64_t
#define mod 1000000007
#define vl vector<int64_t>
#define vc vector<char>
#define vs vector<string>
#define np(vec) next_permutation(vec.begin(),vec.end())==true
#define en endl
#define vvl vector<vector<int64_t>>
#define sti stack<int64_t>
#define qui que<int64_t>
#define ms(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
ll fun(ll x){
  ll r,s,t;
  s=0;
  t=0;
  if(x==0){return 0;}
  fo(i,11){
    ll k=i;
    if(pow(10,k-1)<=x&&x<pow(10,k)){r=k;
                                   break;}
  }
  vl vec(r);
  vl v2(r);
  fo(i,r){
    vec.at(i)=x%10;
    x=x/10;
  }
  sor(vec);
  fo(i,r){
    v2.at(i)=vec.at(i);
  }
  rev(v2);
  fo(i,r){
    s+=vec.at(i);
    s=s*10;
  }
  s=s/10;
  fo(i,r){
    t+=v2.at(i);
    t=t*10;
  }
  t=t/10;
  return abs(t-s);
}

int main(){
 ll n,k;
  cin>>n>>k;
  fo(i,k){
    n=fun(n);
  }
  cout<<n<<endl;
}