Go: package main

import (
	"fmt"
	"sort"
)

var memo []int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if memo[v] == 0 {
		a := make([]int, 4)
		for i := 0; i < 4; i++ {
			a[i] = v % 10
			v /= 10
		}
		sort.Slice(a, func(i, j int) bool {
			return a[i] > a[j]
		})
		l := int(0)
		for i := 0; i < 4; i++ {
			l *= 10
			l += a[i]
		}
		sort.Slice(a, func(i, j int) bool {
			return a[i] < a[j]
		})
		s := int(0)
		for i := 0; i < 4; i++ {
			s *= 10
			s += a[i]
		}
		memo[v] = solve(l-s) + 1
	}
	return memo[v]
}

func main() {
	var v int
	memo = make([]int, 10000)
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

Python: while True:
    num = input()
    if int(num) == 0: break
    if len(set(num)) == 1:
        print("NA")
        continue
    cnt = 0
    while num != "6174":
        cnt += 1
        L = ''.join(sorted(num))
        S = ''.join(reversed(sorted(num)))
        num = str(int(S) - int(L)).zfill(4)
    print(cnt)

Python: while 1:
	line = raw_input()
	if line == line[0] * 4:
		if line[0] == "0":
			break
		print("NA")
		continue
	
	line = list(line)
	c = 0
	while 1:
		if "".join(line) == "6174":
			print(c)
			break
		else:
			line.sort()
			line = list(str(int("".join(line[::-1])) - int("".join(line))))
			c += 1

Python: # AOJ 0259: All Numbers Lead to 6174
# Python3 2018.6.25 bal4u

while True:
	n = input()
	if n == "0000": break
	if len(set(n)) == 1:
		print("NA")
		continue
	ans = 0
	while n != "6174":
		ans += 1
		l = sorted(n)
		s = l[::-1]
		n = str(int(''.join(s))-int(''.join(l)))
	print(ans)

Python: from functools import reduce
while True:
    n = input()
    if n == '0000': break
    if len(n) < 4: n = n.zfill(4)
    if reduce(lambda x,y: x and y, [n[0] == s for s in n]):
        print('NA')
    else:
        cnt = 0
        while n != '6174':
            l = ''.join(sorted(n, reverse=True))
            s = l[::-1]
            n = str(int(l)-int(s))
            if len(n) < 4: n = n.zfill(4)
            cnt += 1
        print(cnt)

Python: def count6174(n):
    if n[0] == n[1] == n[2] == n[3]:
        return 'NA'
    count = 0
    while n != '6174':
        n = list(n)
        l = int(''.join(sorted(n, reverse=True)))
        s = int(''.join(sorted(n)))
        n = '{:04}'.format(l - s)
        count += 1
    return count

import sys
f = sys.stdin

while True:
    n = f.readline().strip()
    if n == '0000':
        break
    print(count6174(n))

Python: from functools import reduce
while True:
	n = input()
	if n == "0000":
		break
	n = n if len(n) >= 4 else n.zfill(4)
	if reduce(lambda x,y:x and y,[n[0] == i for i in n]):
		print("NA")
	else:
		cnt = 0
		while n != "6174":
			s = ''.join(sorted(n))
			l = ''.join(sorted(n,reverse = True))
			n = str(int(l)-int(s))
			n = n if len(n) >= 4 else n.zfill(4)
			print(n)
			cnt += 1
		print(cnt)

Python: import copy

while 1:
    n = list(input())
    if n == ["0", "0", "0", "0"]:
        break

    tmp = int(n[0])*1000 + int(n[1])*100 + int(n[2])*10 + int(n[3])
    if tmp % 1111 == 0:
        print("NA")
        continue

    cnt = 0
    while n != ["6", "1", "7", "4"]:
        n.sort(reverse=True)
        l = copy.deepcopy(n)
        n.sort()
        s = copy.deepcopy(n)
        l = int(l[0])*1000 + int(l[1])*100 + int(l[2])*10 + int(l[3])
        s = int(s[0])*1000 + int(s[1])*100 + int(s[2])*10 + int(s[3])
        n = list(str(l-s).zfill(4))
        cnt += 1

    print(cnt)

Python: while 1:
    a = list(input())
    if a == ["0", "0", "0", "0"]:break
    if a[0] == a[1] == a[2] == a[3]:print("NA")
    else:
        c = 0
        while 1:
            if a == ["6", "1", "7", "4"]:break
            c += 1
            s = int("".join(sorted(a)))
            l = int("".join(sorted(a, reverse = True)))
            a = list("{:04d}".format(l - s))
        print(c)

Python: # -*- coding: utf-8 -*-
"""
All Numbers Lead to 6174
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0259

"""
import sys


Memo = {d*4: 'NA' for d in '0123456789'}
Memo['6174'] = 0

def solve(N):
    x = N
    res = []
    while x not in Memo:
        res.append(x)
        l = ''.join(sorted(x, reverse=True))
        s = ''.join(sorted(x))
        x = '{:04d}'.format(int(l) - int(s))
    for i, r in enumerate(res):
        Memo[r] = len(res) - i + Memo[x]
    return Memo[N]


def main(args):
    while True:
        N = input()
        if N == '0000':
            break
        ans = solve(N)
        print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Python: while True :
    n = int(input())
    if n == 0 :
        break
    
    else :
        ans = 0
        while True :
            
            if n == 6174 :
                print(ans)
                break
            
            else :
                A = int(n / 1000)
                B = int((n%1000) / 100)
                C = int((n%100) / 10)
                D = n%10
                
                if A == B and A == C and A == D :
                    print("NA")
                    break
                
                else :
                    LIST = []
                    LIST.append(A)
                    LIST.append(B)
                    LIST.append(C)
                    LIST.append(D)
                
                    LIST.sort()
                    S = LIST[0] * 1000 + LIST[1] * 100 + LIST[2] * 10 + LIST[3]
                    L = LIST[3] * 1000 + LIST[2] * 100 + LIST[1] * 10 + LIST[0]
                    
                    n = L - S
                    ans += 1

Python: while 1:
    ans = 0
    n = int(input())
    if n == 0:
        break
    flag = True
    while n != 6174:
        ans += 1
        num = [n-n//10*10, n//10-n//100*10, n//100-n//1000*10, n//1000]
        a = sorted(num)
        b = sorted(num, reverse = True)
        if a == b:
            print("NA")
            flag = False
            break
        n = 0
        for i in range(4):
            n += (a[i]-b[i])*10**i
    if flag:
        print(ans)

Python: while 1:
    n=int(input())
    if n==0:break
    if n%1111==0:print("NA")
    else:
        count=0
        while 1:
            if n==6174:
                print(count)
                break
            else:
                n_min=[]
                for i in str(n).zfill(4):
                    n_min.append(i)
                n_min.sort()
                n_max=n_min[::-1]
                n=int("".join(n_max))-int("".join(n_min))
                count+=1

Python: while True:
  n = input()
  if n == "0000":
    break
  if n.count(n[0]) == 4:
    print("NA")
    continue
  cnt = 0
  while n != "6174":
    cnt += 1
    l = int("".join(sorted(list(str(n)), reverse=True)))
    s = int("".join(sorted(list(str(n)))))
    n = str(l - s)
  print(cnt)

Python: # coding: utf-8
# Your code here!

while True:
    N = input()
    if N == "0000":
        break

    nums = [int(N[i]) for i in range(4)]
    ans = 1
    while True:
        nums.sort()
        S = nums[3]*1000 + nums[2]*100 + nums[1]*10 + nums[0]
        T = nums[0]*1000 + nums[1]*100 + nums[2]*10 + nums[3]
        if S - T == 6174:
            break
        elif S - T == 0:
            ans = -1
            break
        else:
            K = S - T
            nums[0] = K // 1000
            K -= nums[0]*1000
            nums[1] = K // 100
            K -= nums[1]*100
            nums[2] = K // 10
            K -= nums[2]*10
            nums[3] = K
            ans = ans + 1

    if ans == -1:
        print("NA")
    elif N == "6174":
        print(0)
    else:
        print(ans)

Python: while 1:
    n,c=input(),0
    if int(n)==0:break
    if n.count(n[0])==4:
        print('NA')
        continue
    while n!='6174':
        n=''.join(sorted(n))
        n = str(int(n[::-1]) - int(n)).zfill(4)
        c+=1
    print(c)