#include <bits/stdc++.h>

#pragma region ppd
#define ll long long
#define ld long double
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define for0(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define forr(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define forab(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define foriter(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define endl '\n'
#define INF (ll)(1e9)
#define EPS 1e-9
#define MOD (ll)(1e9 + 7)
#define PI 3.1415926535897932384626433832795
#define set set
#define mset multiset
#define uset unordered_set
#define umset unordered_multiset
#define map map
#define mmap multimap
#define umap unordered_map
#define ummap unordered_multimap
#define FILEIO                        \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout);

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vll;
typedef vector<vll> vvll;
typedef pair<i64, i64> pll;

template <class T>
bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T>
bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

template <class T> void re(T &x) { cin >> x; }
template <class Arg, class... Args> void re(Arg &first, Args &...rest) { re(first); re(rest...); }
template <class T> void re(vector<T> &a) { for0(i, a.size()) re(a[i]); }

template <class T> void pr(T &x) { cout << x << " "; }
template <class Arg, class... Args> void pr(Arg &first, Args &...rest) { pr(first); pr(rest...); }
template <class T> void pr(vector<T> &a) { for0(i, a.size()) pr(a[i]); }


#pragma endregion ppd

//=============================================================================//
ll g1(ll n) {
    mset<ll, greater<ll>> s;
    while(n) {
        s.insert(n%10);
        n/=10;
    }
    ll ret = 0;
    for(auto &d: s) {
        ret = ret*10 + d;
    }
    
    return ret;
}

ll g2(ll n) {
    mset<ll> s;
    while(n) {
        s.insert(n%10);
        n/=10;
    }
    ll ret = 0;
    for(auto &d: s) {
        ret = ret*10 + d;
    }
    
    return ret;
}

void solve()
{
    ll n, k; re(n, k);

    for0(i, k) {
        ll newn = g1(n) - g2(n);
        //cout<<endl<<endl;
        n = newn;
    }

    pr(n);
}

int main()
{
#ifndef ONLINE_JUDGE
    FILEIO
    auto start = chrono::high_resolution_clock::now();
#endif
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    //==================================================================================

    int T = 1;
    //cin >> T;
    while (T--)
    {
        solve();
        cout << endl;
    }

//==================================================================================
#ifndef ONLINE_JUDGE
    auto finish = chrono::high_resolution_clock::now();

    chrono::duration<double> elapsed = finish - start;
    cout << "\nTime: " << elapsed.count() << " s\n";
#endif

    return 0;
}
#include<bits/stdc++.h>
using namespace std;
int n,k,nn,minn,maxx;
string nts(int x)
{
	string res="";
	while(x)
	{
		res=(char)(x%10+'0')+res;
		x/=10;
	}
	return res;
}
int stn(string x)
{
	int res=0;
	for(int i=0;i<x.size();i++)
	{
		res*=10;
		res+=x[i]-'0';
	}
	return res;
}
int main()
{
	cin>>n>>k;
	for(int i=0;i<k;i++)
	{
		string s=nts(n);
		sort(s.begin(),s.end());
		minn=stn(s);
		reverse(s.begin(),s.end());
		maxx=stn(s);
		nn=maxx-minn;
		if(nn==n)
		{
			cout<<n;
			return 0;
		}
		n=nn;
	}
	cout<<n;
    return 0;
}