#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(i=a;i<b;i++)
#define pb push_back
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define hell 1000000007
using namespace std;

ll pr (ll x , ll y)
{
    ll ans=1;
    if(y==0) return 1;
    while(y>0)
    {
        if(y%2) ans*=x;
        x*=x;
        y/=2;
        ans%=hell;
        x%=hell;
    }
    return ans;

}


void multiply(ll F[2][2], ll M[2][2]) {
   int a = (F[0][0] * M[0][0])%hell + (F[0][1] * M[1][0])%hell;
   int b= (F[0][0] * M[0][1])%hell + (F[0][1] * M[1][1])%hell;
   int c = (F[1][0] * M[0][0])%hell + (F[1][1] * M[1][0])%hell;
   int d = (F[1][0] * M[0][1])%hell+ (F[1][1] * M[1][1])%hell;
   a%=hell;
   b%=hell;
   c%=hell;
   d%=hell;
   F[0][0] = a;
   F[0][1] = b;
   F[1][0] = c;
   F[1][1] = d;
}
void power(ll F[2][2], ll n) {
   if (n == 0 || n == 1)
      return;
   ll M[2][2] = {{1,1},{1,0}};
   power(F, n / 2);
   multiply(F, F);
   if (n % 2 != 0)
      multiply(F, M);
}
ll ff(ll n) {
   ll F[2][2] = {{1,1},{1,0}};
   if (n == 0)
      return 0;
   power(F, n - 1);
   return F[0][0];
}


ll inv ( ll x)
{
    return pr(x, hell - 2);
}

void solve()
{
  ll n,m,i,j,k,c=0,y=0,ans=0,a=0,b;
  string s;
  cin>>s>>k;
  rep(i,0,k)
  {

      string smin=s,smax=s;
      sort(all(smin));
      sort(all(smax));
      reverse(all(smin));
      int lmax = stoi(smin);
      int lmin = stoi(smax);
      int aa = lmax - lmin;
      //cout<<aa<<endl;
      s = to_string(aa);
      
  }
  cout<<s;




  }

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    //cin>>t;
    while(t--)
    solve();
}

#include<bits/stdc++.h>

using namespace std;
 
#define  GODSPEED       ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(15);
#define  f              first                 
#define  s              second
#define  newl           cout<<"\n";
#define  pb             push_back  
#define  mset(a,x)      memset(a,x,sizeof(a))
#define  debv(a)        for(auto it: a)cout<<it<<" ";newl;
#define  deb1(a)        cout<<a<<"\n";
#define  deb2(a,b)      cout<<a<<" "<<b<<"\n";
#define  deb3(a,b,c)    cout<<a<<" "<<b<<" "<<c<<"\n";
#define  deb4(a,b,c,d)  cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
#define  uniq(a)        a.resize(unique(a.begin(), a.end()) - a.begin());
#define  all(a)         a.begin(),a.end()           
 
typedef  long long             ll;
typedef  long double           ld;
typedef  pair<ll,ll>           pll;
typedef  vector<ll>            vll;
typedef  vector<pll>           vpll;

const ll   N     =  1e5+5;
const ll   mod   =  998244353;
const ll   INF   =  0x7f7f7f7f7f7f7f7f;
const int  INFi  =  0x7f7f7f7f;

ll n, k;

void solve(){
	cin >> n >> k;
	ll ans = 0;
	for(int i = 2; i <= 2 * n; i++){
		ll sum = k + i;
		ll x = i;
		if(sum > 2 * n or sum < 2) continue;
		if(x > n) x -= 2 * (x - (n + 1));
		if(sum > n) sum -= 2 * (sum - (n + 1));
		ans += (x - 1) * (sum - 1);
	}
	deb1(ans)
}

int main(){
	GODSPEED;
	int test = 1;
	//cin >> test;
	for(int i = 1; i <= test; i++){
		solve();
	}
}