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

#define ll long long int
#define ull unsigned long long int


/**********************************************/


void solve(ll n, ll k) {
	ll x = n;
	while (k--) {
		string s = to_string(x);
		sort(s.begin(), s.end(), greater<char>());
		ll g1 = stoll(s);


		reverse(s.begin(), s.end());
		ll g2 = stoll(s);

		x = g1 - g2;
	}

	cout << x << endl;
}


int main() {

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	ll t = 1;
	// cin >> t;
	while (t--) {
		ll n, k;
		cin >> n >> k;

		solve(n, k);
	}
}


#include <bits/stdc++.h>

#define int             long long int
#define vi              vector<int>
#define vvi             vector<vector<int>>
#define u_m             unordered_map<int,int>
#define u_s             unordered_set<int>
#define pii             pair<int,int>
#define pb(a)           push_back(a)
#define forr(a,n)       for(int i=a;i<n;i++)
#define forrr(j,a,n)    for(int j=a;j<n;j++)
#define mp              make_pair
#define ff              first
#define ss              second
#define clr(val)        memset(val, 0, sizeof(val))

#define read(a) for(auto &i:a) cin>>i
#define display(a) for(auto i:a) cout<<i<<" "
#define MOD 1000000007

inline int add(int a, int b){ int c = (a%MOD) + (b%MOD); if(c >= MOD) c -= MOD; return c; }
inline int mul(int a, int b){ return ((a%MOD) * 1ll * (b%MOD)) % MOD; }
inline int sub(int a, int b){ int c = (a%MOD) - (b%MOD); if(c < 0) c += MOD; return c; }

using namespace std;
void solve();
int32_t main()
{

    solve();

}
void solve(){
   string n;int k;
   cin>>n>>k;
   int a = stoi(n);
   while(k--){
        int b=0;
        sort(n.begin(),n.end());
        string x = n;
        reverse(n.begin(),n.end());
        string y =n;
        b = stoi(y)-stoi(x);
        if(a==b)
            break;
        else
        {a = b;n=to_string(b);}
   }
    cout<<a<<endl;
}
