#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,x) for(ll i = 0; i < x; i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

ll f(ll x){
    string num = to_string(x);
    sort(num.begin(),num.end());
    ll g1 = stol(num);
    sort(num.begin(),num.end(), greater<int>{});
    ll g2 = stol(num);
    return -g1 + g2;
}
int main(){
    ll n,k; cin >> n >> k;
    for(ll i = 0; i < k; i++){
        n = f(n);
    }
    cout << n << endl;
}
#include <bits/stdc++.h>
using namespace std;
#define MOD (long long int)(998244353)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define FOR(i,a,b) for(int i=a; i<(int)(b); i++)
#define ALL(x) (x).begin(),(x).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset((a), 0 ,sizeof(a))
#define PB push_back
#define MP make_pair
#define SP << " " <<
const int INF = 1001001001;
const ll LINF = 100100100100100100;
const double EPS = 1e-10;
const long double PI  = acos(-1.0L);
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
#define chmax(a,b) a = (((a)<(b))?(b):(a))
#define chmin(a,b) a = (((a)>(b))?(b):(a))

__attribute__((constructor))
void initial(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(15);
}


signed main() {
	ll n; int k; cin>>n>>k;
	ll m=n;
	rep(i,k){
		VL a;
		while(m){
			a.PB(m%10);
			m/=10;
		}
		ll b=0LL,c=0LL;
		sort(RALL(a));
		rep(j,a.size()){
			b += a[j];
			if((ll)j<(a.size()-1)) b*=10;
		}
		sort(ALL(a));
		rep(j,a.size()){
			c += a[j];
			if((ll)j<(a.size()-1)) c*=10;
		}
		m=b-c;
	}
	cout<<m<<endl;
	return 0;
}
