#include <stdio.h>
#include <stdlib.h>
#define ll long long
#define rep(i,l,r)for(ll i=l;i<r;i++)


ll p10[]={1,10,100,1000,10000,100000};
ll a[1000000];
int main(){
	ll n,k;
	ll cnt=0;
	while(scanf("%lld%lld",&n,&k),k){
		cnt++;
		ll id=0;
		a[n]=cnt*p10[5]+id;
		while(1){
			id++;
			ll x[10]={};
			rep(i,0,k)x[n/p10[i]%10]++;
			ll m=0;
			for(int crr=0;crr<10;){
				if(x[crr]--)m=m*10+crr;
				else crr++;
			}
			ll M=0;
			rep(i,0,k)M=M*10+(m/p10[i]%10);
			if(a[M-m]/p10[5]==cnt){
				printf("%lld %lld %lld\n",a[M-m]%p10[5],M-m,id-a[M-m]%p10[5]);
				break;
			}else{
				a[M-m]=cnt*p10[5]+id;
			}
			n=M-m;
		}
	}
}
