 #include<bits/stdc++.h>
 using namespace std;
 typedef long long ll;
  
 typedef vector<int> vi;
 typedef vector<ll> vl;
 typedef vector<vi> vvi;
 typedef set<int> si;
 typedef set<ll> sl;
 typedef queue<int> qi;
 typedef queue<ll> ql;
 typedef stack<int> sti;
 typedef deque<int> dqi;
 typedef priority_queue<int> pqi;
 typedef priority_queue<ll> pql;
 typedef deque<ll> dql;
 typedef vector<vl> vvl;
 typedef pair<int,int> pii;
 typedef pair<double, double> pdd;
 typedef pair<ll, ll> pll;
 typedef vector<pii> vii;
 typedef vector<pll> vll;
 typedef double dl;
                    
 #define PB push_back
 
 #define F first
 #define S second
 #define MP make_pair
 #define el  '\n'
 #define sl(x) (int)x.length()
 #define all(a) (a).begin(),(a).end()
 #define sz(x) (int)x.size()
 #define mid(l,r) ((r+l)/2)
 #define left(node) (node*2)
 #define right(node) (node*2+1)
 #define mx_int_prime 999999937
#define loop(i,n)        for(int i=0;i<n;i++) 

 const double PI = acos(-1);
 const double eps = 1e-9;
 const int inf = 2000000000;
 const ll infLL = 9000000000000000000;
 #define MOD 1000000007
 #define in(n)  ll n;cin>>n;         
 #define mem(a,b) memset(a, b, sizeof(a) )
 #define gcd(a,b) __gcd(a,b)
 #define sqr(a) ((a) * (a))
 #define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
 #define fraction(a) cout.unsetf(ios::floatfield); cout.precision(a); cout.setf(ios::fixed,ios::floatfield);
 typedef vector<int>::iterator vit;
 typedef set<int>::iterator sit;
 int dx[] = {0, 0, +1, -1};
 int dy[] = {+1, -1, 0, 0};
 //int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
 //int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};
int t; ll c[69], a[69];

  
 
  
  int solve(int x){
      string s = to_string(x);
    sort(s.begin(), s.end());
    string t(s.rbegin(), s.rend());
    return stoi(t) - stoi(s);
     
}
    
  
      
 
 int main(){

 int t,k;
 cin>>t>>k;


   while(k--){
  t=solve(t);
      
   }
cout<<t<<el;

    return 0 ;
 }
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n);i++)
using ll = long long;
const ll INF = 1e9+7;
using P = pair<int,int>;



int main(){
    string s;
    int k;
    cin >> s >> k;
    rep(i,k){
        string g1 = s;
        string g2 = s;
        sort(g1.begin(), g1.end(), greater<int>());
        sort(g2.begin(), g2.end());
        ll x = stoi(g1) - stoi(g2);
        stringstream iss;
        iss << x;
        s = iss.str();
    }
    cout << s << endl;
}