Go: package main

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

var memo []int8

func solve(v int) int8 {
	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([]int8, 10000)
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

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

Python: while 1:
    N = input()
    if N=='0000':break
    if N[0] ==N[1] == N[2] == N[3]:
        print('NA')
        continue
    cnt = 0
    while N != '6174':
        x = int(''.join(reversed(sorted(list(N))))) - int(''.join(sorted(list(N))))
        N = str(x).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(str(l - s))
        print(c)

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:
    number=int(input())
    if number==0000:
            break
    trial=0
    while True:
        if number==6174:
            print(trial)
            break
        box=[]
        for i in range(4):
            a=number%10
            number=int(number/10)
            box.append(a)
        if box[0]==box[1] and box[2]==box[3] and box[1]==box[2]:
            print("NA")
            break
        box.sort()
        l=0
        
        for i in range(4):
            l+=box[i]*10**i
        box.sort(reverse=True)
        s=0
        for i in range(4):
            s+=box[i]*10**i
        number=l-s
        trial+=1

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: 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")
            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

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

    cnt = 0
    while n != "6174":
        L, S = list(n), list(n)
        L.sort(reverse=True)
        S.sort()
        n = str(int("".join(L)) - int("".join(S)))
        cnt += 1

    print(cnt)

Python: while (True):
    N = input()
    
    if N=='0000':
        break
    
    if N==N[0]*4:
        print('NA')
        continue
    
    cnt = 0
    
    while N!='6174':
        l = [int(Ni) for Ni in N]
        l.sort()
        L = 1000*l[3]+100*l[2]+10*l[1]+l[0]
        S = 1000*l[0]+100*l[1]+10*l[2]+l[3]
        N = str(L-S)
        N = '0'*(4-len(N))+N
        cnt += 1
    
    print(cnt)

Python: while True:
    N = input()
    if N == "0000":
        break
    ans = 0
    if all([1 if s == N[-1] else 0 for s in N[:-1]]):
        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: # AOJ 0259: All Numbers Lead to 6174
# Python3 2018.6.25 bal4u

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

Python: while 1:
	line = list(raw_input())
	if line[0] == line[1] and line[1] == line[2] and line[2] == line[3]:
		if line[0] == "0":
			break
		print("NA")
		continue
	elif "".join(line) == "6174":
		print(0)
	
	c = 0
	while 1:
		line.sort()
		l = int("".join(line[::-1]))
		s = int("".join(line))
		l_s = l - s
		c += 1

		if l_s == 6174:
			print(c)
			break
		else:
			line = list(str(l_s))

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

Python: while True:
    n = 0
    int1 = input()
    int2 = [int(a) for a in int1]
    if int2[0] == int2[1] == int2[2] == int2[3]:
        if int2[0] == 0:
            break
        else:
            print("NA")
            continue
    else:
        while True:
            int1 = int(int1)
            if int1 == 6174:
                print(n)
                break
            else:
                int2.sort()
                num1 = int2[0] * 1000 + int2[1] * 100 + int2[2] * 10 + int2[3]
                int2.reverse()
                num2 = int2[0] * 1000 + int2[1] * 100 + int2[2] * 10 + int2[3]
                int1 = num2 - num1
                int2 = [int(b) for b in str(int1)]
                n += 1
                if len(int2) < 4:
                    int2.insert(0,0)
                continue

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)