$<.map{|s| s.split.map(&:to_i)}.each{|an, l|
  break if an == 0 && l == 0
  (1..Float::INFINITY).inject(Array.new){|xs, i|
    xs << an
    an = (t=("%0#{l}d"%an).split(//).sort; t.reverse.join.to_i - t.join.to_i)
    j = xs.index(an)
    (puts "#{j} #{an} #{i-j}";break)unless j.nil?
    xs
  }
}