Go: package main

import (
	"fmt"
	"sort"
	"strconv"
)

var memo []int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if memo[v] == 0 {
		str := ([]byte)(strconv.Itoa(v))
		sort.Slice(str, func(i, j int) bool {
			return str[i] < str[j]
		})
		s, _ := strconv.Atoi(string(str))
		sort.Slice(str, func(i, j int) bool {
			return str[i] > str[j]
		})
		l, _ := strconv.Atoi(string(str))
		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:
    n = input()

    if n == "0000":
        break
    if n.count(n[0]) == 4:
        print("NA")
        continue

    cnt = 0
    while n != "6174":
        n, l = sorted(list(n)), sorted(list(n), reverse=True)
        n = str(int("".join(l)) - int("".join(n))).zfill(4)
        cnt += 1

    print(cnt)

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

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 = str(int(''.join(reversed(sorted(list(n))))) - int(''.join(sorted(list(n))))).zfill(4)
        c+=1
    print(c)

Python: # Aizu Problem 0259: All Numbers Lead to 6174

import sys, math, os, bisect

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def solve(digits):
    steps = 0
    while digits != ['6', '1', '7', '4']:
        L = int(''.join(sorted(digits)))
        S = int(''.join(sorted(digits, reverse=True)))
        digits = [_ for _ in str(S - L)]
        while len(digits) < 4:
            digits = ['0'] + digits
        steps += 1    
    return steps

    
while True:
    digits = [_ for _ in input().strip()]
    if digits == ['0'] * 4:
        break
    print("NA" if len(set(digits)) == 1 else solve(digits))

Python: while True:
    N = input()
    if N == "0000":
        break
    ans = 0
    if int(N) % 1111 == 0:
        print("NA")
        continue
    while int(N) != 6174:
        a, b, c, d = N[0], N[1], N[2], N[3]
        S =  sorted([a, b, c, d])
        L = int([''.join(S[::-1])][0])
        S = int([''.join(S)][0])
        ans += 1
        N = str(L-S)
    print(ans)

Python: while True:
    N = input()
    if N == "0000":
        break
    ans = 0
    if int(N) % 1111 == 0:
        print("NA")
        continue
    while N != '6174':
        N = list(N)
        S = int(''.join(sorted(N)))
        L = int(''.join(sorted(N)[::-1]))
        ans += 1
        N = str(L-S).zfill(4)
    print(ans)

Python: from functools import reduce
while True:
    n = input()
    if n == '0000': break
    if len(n) < 4:
        n = '0'*(4-len(n)) + n
    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 = ''.join(sorted(n))
            n = str(int(l)-int(s))
            if len(n) < 4:
                n = '0'*(4-len(n)) + n
            cnt += 1
        print(cnt)

Python: NA = [ "1111", "2222", "3333", "4444", "5555",
              "6666", "7777", "8888", "9999" ]
while True:
    st = input()
    if st == "0000":
        break
    if st in NA:
        print("NA")
        continue
    cnt = 0
    while st != "6174":
        cnt += 1
        l = "".join(sorted(st,reverse=True))
        s = "".join(sorted(st))
        st = str(int(l) - int(s)).zfill(4)
    print(cnt)

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 = ''.join(sorted(n))
            n = str(int(l)-int(s))
            if len(n) < 4: n = n.zfill(4)
            cnt += 1
        print(cnt)

Python: def to_number(n):
    res = 0
    for d in n:
        res = res*10 + d
    return res

if __name__ == "__main__":
    while True:
        n = input()
        if n == '0000':
            break
        cnt = 0
        used = set()
        while n != '6174':
            if n in used:
                cnt = 'NA'
                break
            used.add(n)
            n = list(map(int,list(n)))
            n.sort()
            a = to_number(n[::-1])
            b = to_number(n)
            a -= b
            n = str(a)
            while len(n) < 4:
                n = '0'+n
            cnt += 1
        print(cnt)

Python: import sys
import copy
def main():
    for line in sys.stdin:
        n = int(line)
        if n == 0:
            break
        else:
            if n == 1111 or n == 2222 or\
                n == 3333 or n == 4444 or\
                n == 5555 or n == 6666 or\
                n == 7777 or n == 8888 or\
                n == 9999:
                print("NA")
                break
            else:
                cnt = 0
                while True:
                    if n == 6174:
                        print(cnt)
                        break
                    else:
                        A = list(str(n))
                        for x in range(len(A)):
                            A[x] = int(A[x])
                        B = copy.deepcopy(A)
                        A.sort(reverse = True)
                        B.sort()
                        for x in range(len(A)):
                            A[x] = str(A[x])
                        for x in range(len(B)):
                            B[x] = str(B[x])
                        aa = ""
                        for a in A:
                            aa += a
                        A = int(aa)
                        bb = ""
                        for b in B:
                            bb += b
                        B = int(bb)
                        n = A - B
                        cnt += 1


if __name__ == "__main__":
    main()

Python: while True:
    N = input()
    if N == "0000":
        break
    ans = 0
    if int(N) % 1111 == 0:
        print("NA")
        continue
    while int(N) != 6174:
        N = list(N)
        S = int(''.join(sorted(N)))
        L = int(''.join(sorted(N)[::-1]))
        ans += 1
        N = str(L-S)
    print(ans)

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)
			cnt += 1
		print(cnt)

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

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 = "".join(sorted(list(n), reverse=True))
    s = "".join(sorted(list(n)))
    n = str(int(l) - int(s)).zfill(4)
  print(cnt)