#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int as(const void *a, const void *b) {
  return *(int *)a - *(int *)b;
}

int de(const void *a, const void *b) {
  return *(int *)b - *(int *)a;
}


int re_number(int *s,int l){
    int sum,i,ll;
    sum = 0;
    ll = l - 1;
    for(i = 0; i < l ; i++){
        sum += s[i] * pow(10,ll);
        ll--;
    }
    return sum;

}

void make_string(int *s,int l,int num){

    int count = l-1;
    int i;
        
    for(i = 0 ; i < l ; i++){
        s[i] = num / (int)pow(10,count);
        num = num % (int)pow(10,count);
        count--;
            
    }

}
typedef struct{
    int x;
    int value;
}MAX_MIN;

int struct_sort( const void *p, const void *q ) {
    return ((MAX_MIN*)p)->value - ((MAX_MIN*)q)->value;
}


int main(){

    int s[6],l,i,j,num,k,t;
    int max, min;
    MAX_MIN a[20];
    int ij[20];
    int dis_min;
    int x1_dmin,x2_dmin,value_dmin,i_min,j_min;
    int a0;

    while(1){
        scanf("%d %d",&num,&l);
        if(num == 0 && l == 0)
            break;

         for(i = 0; i < 6 ; i++){
             s[i] == 0;
         }   

    
        make_string(s,l,num);
        a[0].value = re_number(s,l);
        a0 = a[0].value;
        a[0].x = 0;
        for(i = 1; i < 20 ; i++ ){
        ij[i] = 0;
        max = 0;
        min = 0;
        qsort(s,l,sizeof(int),as);
        min = re_number(s,l);
        qsort(s,l,sizeof(int),de);
        max = re_number(s,l);
        make_string(s,l,max-min);
        a[i].value = re_number(s,l);
        a[i].x = i;

        //printf("%d\n",a[i].value);
        }
        qsort(a,20,sizeof(MAX_MIN),struct_sort);
        dis_min = 20;
        /*
        for(i = 0; i < 20; i++)
            printf("sort = %d, x = %d\n",a[i].value,a[i].x);
          */
        x1_dmin = 0;x2_dmin = 0; value_dmin = -1; j_min = 20,i_min = 20;j = 0;k = 0;
        for(i = 0; i < 20 ; i++){
            t = 0;
            //printf("i = %d\n",i);
            //printf("value of : %d\n",a[i].value);
            if(a[i].value == a[i+1].value){
                j = i;  
                while(a[j].value == a[i].value){
                    ij[t] = a[j].x;
                    t++;
                    j++;

                }
                qsort(ij,t,sizeof(int),as);
                x1_dmin = ij[0];//j
                x2_dmin = ij[1];//i
                //printf("%d %d\n",j_min,x1_dmin);
                if(j_min > x1_dmin){
                    j_min = x1_dmin;
                    i_min = x2_dmin;
                    
                    value_dmin = a[i].value;
                }
                i = j-1;       
            }
        }
            
    if(value_dmin == -1){
        if(a[0].value == a0){
            printf("%d %d %d\n",a[1].x,a[1].value,20 - a[1].x);
        }else{
            printf("%d %d %d\n",a[0].x,a[0].value,20 - a[0].x);
        }
    }else{        
        printf("%d %d %d\n",j_min,value_dmin,i_min - j_min);
    }

    }
    return 0;

}

