#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
int now[15];
void getstring(int n){
	now[0]=0;
	while(n!=0){
		now[now[0]+1]=n%10;
		now[0]++;
		n=n/10;
	}
	sort(now+1,now+now[0]+1);
//	for(int i=1;i<=now[0];i++){
//		cout<<now[i]<<endl;
//	}
}
int main(){
	int n,k,l;
	cin>>n>>k;
	while(k>0){
		getstring(n);
		l=n;
		n=0;
		int temp=1;
		for(int i=1;i<=now[0];i++){
			n+=temp*(now[i]-now[now[0]+1-i]);
			temp=temp*10;
		}
		//cout<<n<<endl;
		k--;
		if(n==l){
			break;
		}
	}
	cout<<n<<endl;
	return 0;
}
#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
//学习到了 string的函数 to_string() ,reverse(s.begin(),s.end()), stol
int main()
{

    int N;
    int k;
    cin >> N >> k;
    for (int i = 0; i < k; i++)
    {
        string S = to_string(N);
        sort(S.begin(), S.end());
        string down =S ;
        reverse(S.begin(), S.end());
        string up = S;
        N = stol(up) - stol(down);
        
    }
    cout << N << endl;
    return 0;
}