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

#define ll long long
 
#define pll pair<ll,ll>
#define vll vector<ll>
#define vii vector<int>
#define mll map<ll, ll>

#define all(v) v.begin(), v.end()

#define ce(ele) cout<<ele<<' '
#define cs(ele) cout<<ele<<'\n'

#define CASE(t) ll t; cin>>t; while(t--)

#define forr(i, b) for(ll i=0;i<b;i++)

/***********************************FUNCTIONS***********************************/

void FAST() {	//FAST Input Output, DONT use both scnaf,printf / cin,cout , Use any ONE system
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}

/***********************************CODE***********************************/

int f(int n){
	vector<int> d;
	while(n!=0){
		d.push_back(n%10);
		n=n/10;
	}
	sort(d.begin(),d.end());

	int x=0,y=0;
	int k=d.size();
	for(int i=0;i<k;++i){
		x=x*10;
		x+=d[i];
		y=y*10;
		y+=d[k-1-i];
	}

	return y-x;
}

int main() {
	FAST();
    // CASE(t) 
	{
		int n,k;
		cin>>n>>k;

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

		cout<<n;

	}
	return 0;
}

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

int n,k,tot;
priority_queue<int, vector<int>, greater<int> > g2;
priority_queue<int ,vector<int>,less<int> >g1;

int G1(int x)
{
	int num=0,NUM=1;
	int X;
	while(x/10!=0)
	{
		X=x%10;
		g1.push(X);
		x=x/10;
		if(x<10&&x>0) g1.push(x);
	}
	while(!g1.empty())
	{
		num+=g1.top()*NUM;
		NUM=NUM*10;
		g1.pop();
	}
	return num;
}
int G2(int x)
{
	int num=0,NUM=1;
	int X;
	while(x/10!=0)
	{
		X=x%10;
		g2.push(X);
		x=x/10;
		if(x<10&&x>0) g2.push(x);
	}
	while(!g2.empty())
	{
		num+=g2.top()*NUM;
		g2.pop();
		NUM=NUM*10;
	}
	return num;
}
int f(int x)
{
	int X,Y;
	X=G2(x),Y=G1(x);
	int num;
	num=X-Y;
	return num;
} 
int main()
{
	cin>>n>>k;
	while(k!=0)
	{
		n=f(n);
		k--;
	}
	cout<<n;
	return 0;
}