#include<bits/stdc++.h>
using  namespace std;
typedef  long long ll;
ll n;
int  k;
string  s;
int main(){


	
	cin>>s>>k;
	for(int i=0;i<k;i++){
	ll  a,b,ant;
		ant=1;
		a=0;
		b=0;
		string  st;
		sort(s.begin(),s.end());
		st=s;
		reverse(st.begin(),st.end());
		for(int i=s.size()-1;i>=0;i--){
			a+=(s[i]-'0')*ant;
			ant*=10;
		}
		ant=1;
		for(int i=st.size()-1;i>=0;i--){
			b+=(st[i]-'0')*ant;
			ant*=10;
		}
		b-=a;
		if(b==0){
			cout<<0;
			
			return 0;
		}
		stringstream ss;
   		ss<<b;
		ss>>s;
	}
	cout<<s;
	
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll n, k;

ll g1(ll x){
  if(x == 0) return 0;
  string S = to_string(x);
  vector<int> V;
  for(char s : S){
    int i = s-'0';
    V.push_back(i);
  }
  sort(V.begin(), V.end(), greater<int>());
  string R = "";
  for(int v : V) R += to_string(v);
  ll res = stoi(R);
  return res;
}

ll g2(ll x){
  if(x == 0) return 0;
  string S = to_string(x);
  vector<int> V;
  for(char s : S){
    int i = s-'0';
    if(i > 0) V.push_back(i);
  }
  sort(V.begin(), V.end());
  string R = "";
  for(int v : V) R += to_string(v);
  ll res = stoi(R);
  return res;
}


ll solve(int i, ll x){
  if(i == k) return x;
  ll calc = g1(x) - g2(x);
  return solve(i+1, calc);
}
int main(){
  cin >> n >> k;
  cout << solve(0, n) << endl;
}