#include<bits/stdc++.h>
using namespace std;
int k;
long long n, a[50];
long long g1(long long x){
    int len = 0;
    while(x){
        a[len ++] = x%10;
        x /= 10;
    }
    sort(a, a+len);
    
    for(int i = 0; i < len; ++ i){
        x *= 10;
        x += a[i];
    }
//    cout  << x << endl;
    return x;
}
long long g2(long long x){
    int len = 0;
    while(x){
        a[len ++] = x%10;
        x /= 10;
    }
    sort(a, a+len);
    
    for(int i = len-1; i >= 0; -- i){
        x *= 10;
        x += a[i];
    }
//    cout  << x << endl;
    return x;
}
int main(){
    cin >> n >> k;
    while(k --){
        n = g2(n) - g1(n);
    }
    cout << n <<endl;
}
#include <iostream>
#include <climits>
#include <set>
#include <string>
#include <algorithm>
#include <vector>
#define MAX 1000000007


using namespace std;
#define ll long long
#define dbg if(0)
#define ISRANGE(val,lo,hi) ((lo<=val)&&(val<hi))



int main(){
    ll ans,a,b;
    cin>>a>>b;

    ans=(3-(a+b)%3)%3;

    cout <<ans<<endl;

    return 0;

}