$memo = Array.new(10000)
def f n
  x = $memo[n]
  return x if x
  return 0 if n==6174
  return "NA" if n%1111==0
  str = "%04d" % n
  l = str.chars.sort.reverse.join.to_i
  s = str.chars.sort.join.to_i
  m = l - s
  r = f m
  $memo[n] = r + 1
end

loop do
  s = gets.chomp
  break if s=="0000"
  puts f s.to_i
end
