import Data.List
import Control.Monad

solve a x
 | x == 6174 = Just a
 | only ys = Nothing
 | otherwise = solve (a + 1) (read (reverse zs) - read zs)
 where 
  ys = format 4 x
  zs = sort ys
only (x:xs) = all (==x) xs
format n x = replicate (n - length ys) '0' ++ ys
 where ys = show x


main = do
 s <- getLine
 when (s /= "0000") $ do
  case solve 0 (read s) of
   Just x -> print x
   Nothing -> putStrLn "NA"
  main