import Data.List

main = interact $ unlines . map (show' . recurring_decimals . map read . words) . takeWhile (/= "0 0") . lines
 where show' = unwords . map show

recurring_decimals [a0,l] = [j, ai, i-j]
 where
  ((j,aj),(i,ai)) = (head as', last as')
  as' = head . filter (\xs -> length xs >= 2 && (snd.head) xs == (snd.last) xs) . subsequences $ as
  as = take 21 . iterate (\(i,a) -> (i+1, amax a - amin a)) $ (0,a0)
  amax = read . take l . reverse . sort . (replicate l '0' ++) . show
  amin = read . sort . show