#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0; i<n; i++)
#define eb(t) emplace_back(t)
typedef long long ll;
typedef long long unsigned int llu;
ll INF = 1000000009;
ll MOD = 1000000007;

char a[12];
char b[12];

ll f(ll x){
    string st = to_string(x);
    ll n = st.size();
    rep(i,n){
        a[i] = st[i];
        b[i] = st[i];
    }
    sort(a,a+n);
    sort(b,b+n,greater<>());
    ll ans_a = 0;
    ll ans_b = 0;
    rep(i,n){
        ans_a *= 10;
        ans_a += a[i]-'0';
        ans_b *= 10;
        ans_b += b[i]-'0';
    }
    return ans_b-ans_a;
}


void solve(){

    ll N,k;
    cin >> N >> k;
    ll p = N;
    rep(i,k){
        p = f(p);
    }
    cout << p << endl;

}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef     long long    ll;
typedef     vector<int> vi;
typedef     vector<long long> vl;
typedef     pair<int, int>pi;
typedef     pair<long long, long long>pl;
#define F   first
#define S   second
#define pb  push_back
#define     all(x)      x.begin() , x.end()
#define mp  make_pair
#define     REP(i,a,b) for(i=a;i<=b;i++)
#define     mem(a)      memset(a , 0 ,sizeof a)
#define     memn(a)     memset(a , -1 ,sizeof a)

int main()
{
  int t = 1;
// scanf("%d", &t);
  while (t--)
  {
    ll n,i,j,x,y,k,p;
    cin>>n>>k;
   
    for(ll j=1;j<=k;j++)
    {
       vector<ll>v;
       p=n;
        while(p!=0)
        {
          v.pb(p%10);
          p/=10;
        }
        sort(all(v));
         x=0;
       for(i=0;i<v.size();i++)
       {
         x=(x*10)+v[i];
       }
         y=0;
       for(i=v.size()-1;i>=0;i--)
       {
         y=(y*10)+v[i];
       }
       //cout<<x<<" "<<y<<endl;
       n=abs(y-x);
       if(n==0) break;


    }
    cout<<n<<endl;
  
}
}

