#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[15];
bool cmp (int a, int b) {
	return a > b;
}
int g2(int x) {
	int ind = 0, res = 0;
	while (x) {
		a[ind ++] = x % 10;
		x /= 10;
	}
	sort(a, a + ind);
	for (int i = 0; i < ind; i ++) {
		if (a[i] != 0) {
			res *= 10;
			res += a[i];
		}
	}
	return res;
}
int g1(int x) {
	int ind = 0, res = 0;
	while (x) {
		a[ind ++] = x % 10;
		x /= 10;
	}
	sort(a, a + ind, cmp);
	for (int i = 0; i < ind; i ++) {
		res *= 10;
		res += a[i];
	}
	return res;
}
int f(int x) {
	return g1(x) - g2(x);
}
int main() {
	int n, k, tmp, flag = 0;
	scanf("%d %d", &n, &k);
	for (int i = 0; i < k; i ++) {
		tmp = f(n);
		if (tmp == n) {
			printf("%d\n", tmp);
			flag = 1;
			break;
		}
		n = tmp;
	}
	if (!flag) printf("%d\n", n);
	return 0;
}
// Umang_US

#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pob pop_back
#define vi vector<ll>
#define mp make_pair
#define sz size()
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define rep(i,n) for(ll i=0;i<n;i++)
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define maxx 2*100005
#define sec second
#define fi first
#define be begin()
#define en end()
#define sortv(v) sort(v.begin(),v.end());
#define sortr(v) sort(v.rbegin(),v.rend());
#define w() ll _;cin>>_;while(_--)
#define edge pair<ll,ll>
#define pq priority_queue<ll>
#define mod 1000000007
#define Endl "\n"
#define cy cout<<"YES"<<endl;
#define cn cout<<"NO"<<endl;
#define lb(v,x) std::lower_bound(v.begin(),v.end(),x)
#define ub(v,x) std::upper_bound(v.begin(),v.end(),x)
 
using namespace std;
// multiset<ll>::iterator it=next(s.begin(), i);// iterator for index i
// cout<<fixed<<setprecision(10);
// https://www.youtube.com/watch?v=o2gqyVR7B4I

//------------------------HARDER--------------------------------

int ase(int n)
{
	string s=to_string(n);
	sortv(s);
	
	n=stoi(s);
	return n;
}

int des(int n)
{
	string s=to_string(n);
	sortr(s);
	
	 n=stoi(s);
	return n;
}

int main()
{
	fast;
	
	int n,k;
	cin>>n>>k;
	
	int a[k+3]={0};
	
	int i;
	a[0]=n;
	
	for(i=1;i<=k;i++)
	{
		a[i] = des(a[i-1])-ase(a[i-1]);
	}
	
	cout<<a[k]<<endl;
	return 0;	
}