#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define LB(a,x) lb(all(a),x)-a.begin()
#define UB(a,x) ub(all(a),x)-a.begin()
#define mod 1000000007
//#define mod 998244353
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
template<class T> using V=vector<T>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline void out(T a){ cout << a << '\n'; }
void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;}
//void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;}


const ll INF=1e18;
const int mx=200005;
//abc192

int main(){
    //オーバーフローは大丈夫ですか？？
    cin.tie(0);ios::sync_with_stdio(false);
    ll n,k;
    cin>>n>>k;
    ll ans=n;
    rep(i,k){
        ll tmp=ans;
        string s=to_string(tmp);
        string t=s;
        string u=s;
        sort(all(t));
        sort(all(u));
        reverse(all(u));
        ans=stoll(u)-stoll(t);
    }
    out(ans);
}
//ペナルティ出しても焦らない　ACできると信じろ！！！
//どうしてもわからないときはサンプルで実験　何か見えてくるかも
//頭で考えてダメなら紙におこせ！！
//  /Applications/Geany.app/Contents/MacOS/geany
#include<bits/stdc++.h>

using namespace std;

#define int long long
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define pairii pair<int,int>
#define ff first
#define ss second
#define all(x) x.begin(),x.end()

const int NUM=1000030;
const int N = 10000000;
vector<int> lp, sieve;
vector<int> pr;
void primefactor();
int binpow(int a, int b);
int binpow(int a, int b, int mod);
int gcd(int a, int b);
int lcm (int a, int b);
bool comp(int a, int b);
int inversemod(int a, int mod);
void calc_sieve();

string getstring(int n)
{
	string s;
	while(n>0)
	{
		s.pb('0'+n%10);
		n/=10;
	}
	sort(all(s));
	return s;
}

int getnum(string s)
{
	//reverse(all(s));
	int ans=0;
	for(int i=0;i<s.size();i++)
	{
		ans=ans*10+s[i]-'0';
	}
	return ans;
}

void test()
{
	int n,k,i,j;
	cin>>n>>k;
	
	for(i=1;i<=k;i++)
	{
		string s=getstring(n);
		string s1=s;
		reverse(all(s1));
		
		n=getnum(s1)-getnum(s);
	}
	
	cout<<n<<endl;
	
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t=1;
	//cin>>t;
	while(t--) test();
	return 0;
}

void calc_sieve() //credits: Anish_Sofat
{
	sieve.resize(NUM+1,0);
	for (int x = 2; x <= NUM; x++)
    	{
        	if (sieve[x]) continue;
        	for (int u = x; u <= NUM; u += x)
        	{
            		sieve[u] = x ;
        	}
    	}
}

void primefactor()
{
	lp.resize(N+1,0);
	for (int i=2; i<=N; ++i) {
		if (lp[i] == 0) {
			lp[i] = i;
			pr.push_back (i);
		}
		for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
			lp[i * pr[j]] = pr[j];
	}
}

int binpow(int a, int b)
{
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

int binpow(int a, int b, int mod)
{
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = (res * a)%mod;
        a = (a * a)%mod;
        b >>= 1;
    }
    return res%mod;
}

int gcd(int a, int b)
{
	if(b==0) return a;
	else return gcd(b,a%b);
}

int lcm (int a, int b)
{
    return ((a / gcd(a, b)) * b);
}

bool comp(int a, int b)
{
	return a>b;
}

int inversemod(int a, int mod)
{
	return binpow(a,mod-2, mod);
}
