#include<stdio.h>
#include<stdlib.h>
#define rep(i,N) for(int i=0;i<(int)N;i++)
static inline int IN(void)
{
  int x=0,f=0,c=getchar();while(c<48||c>57){f^=(c==45),c=getchar();}
  while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f?-x:x;
}
static inline void OUT(int x){if(x<0){putchar('-'),x=-x;}if(x>=10){OUT(x/10);}putchar(x%10+48);}
static inline int Asc(const void *a,const void *b){return *(int*)a-*(int*)b;}
const int ten[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
static inline int f(int x)
{
  int num[10]={0},d=0,g1=0,g2=0;while(x){num[d++]=x%10;x/=10;}
  qsort(num,d,sizeof(*num),Asc);
  rep(i,d){g1+=num[i]*ten[i];g2+=num[i]*ten[d-1-i];}
  return g1-g2;
}
int main(void)
{
  int N=IN(),K=IN(),temp=0;
  rep(i,K){temp=N;N=f(N);if(!N){return !puts("0");}if(temp==N){return OUT(N),0;}}
  OUT(N);
}
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string.h>
#define s(n)                        scanf("%d",&n)
#define sc(n)                       scanf("%c",&n)
#define sl(n)                       scanf("%I64d",&n)
#define sf(n)                       scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)
#define INF                         (int)1e9
#define EPS                         1e-9
#define bitcount                    __builtin_popcount
#define gcd                         __gcd
#define forall(i,a,b)               for(int i=a;i<b;i++)
#define foreach(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())
#define pb                          push_back
#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((int)(a.size()))
#define mp                          make_pair
#define MOD                         1000000007
typedef long long ll;
/*For you see , each day i love you
 more.Today more than yesterday
  less than tomorrow.*/
using namespace std;
int g1(int x)
{
	vector<int> vec;
	while(x)
	{
		vec.push_back(x%10);
		x/=10;
	}
	sort(vec.begin() , vec.end());
	for(int i = (int)vec.size()-1;i>=0;i--)
	{
		x*=10;
		x+=vec[i];	
	}
	return x;
}
int g2(int x)
{
	vector<int> vec;
	while(x)
	{
		vec.push_back(x%10);
		x/=10;
	}
	sort(vec.begin() , vec.end());
	for(int i = 0;i < vec.size() ;i++)
	{
		x*=10;
		x+=vec[i];	
	}
	return x;
}
int f(int x)
{
	return g1(x) - g2(x);
}

int main()
{
	int n,k;cin>>n>>k;
	int a = n;
	for(int i = 1;i<=k;i++)
	{
		a = f(a);	
	}
	cout<<a<<endl;
	return 0;
}