#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;

ll n,k;
ll a[100];

void YPO()
{
    ll len=0,i;
    while(n)
    {
        a[len]=n%10;
        n/=10;
        len++;
    }
    sort(a,&a[len]);
    ll x1=0,x2=0;
    for(i=0;i<len;i++)
    {
        x1*=10;
        x1+=a[i];
    }
    for(i=len-1;i>=0;i--)
    {
        x2*=10;
        x2+=a[i];
    }
    n=x2-x1;
}


int main()
{
    ll i,j;
    scanf("%lld%lld",&n,&k);
    for(i=0;i<k;i++)
    {
        YPO();
    }
    printf("%lld\n",n);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,x;
    cin>>n>>x;
    int a[n];
for(int i=0;i<n;i++){
    cin>>a[i];
    if(a[i] == x)
    a[i] = 0;
}
for(int i=0;i<n;i++){
    cin>>a[i];
    if(a[i] != 0)
     cout<<a[i]<<" ";
}

   
}