Problem:


すべての数は６１７４に通ず

0 から 9 の数字からなる四桁の数 N に対して以下の操作を行う。

 N の桁それぞれの数値を大きい順に並べた結果得た数を L とする

 N の桁それぞれの数値を小さい順に並べた結果得た数を S とする

 差 L-S を新しい N とする(1回分の操作終了)

 新しい N に対して 1. から繰り返す

このとき、全桁が同じ数字(0000, 1111など)である場合を除き、あらゆる四桁の数はいつかは 6174になることが知られている。例えば N = 2012の場合

       1回目 (N = 2012): L = 2210, S = 0122, L-S = 2088

       2回目 (N = 2088): L = 8820, S = 0288, L-S = 8532

       3回目 (N = 8532): L = 8532, S = 2358, L-S = 6174

となり、3 回の操作で 6174 に到達する。

0 から 9 の数字からなる四桁の数が与えられたとき、何回の操作で 6174 に到達するか計算するプログラムを作成して下さい。

入力

入力は複数のデータセットからなる。入力の終わりは 0000 が１つの行で示される。各データセットは以下の形式で与えられる。

N

データセットは１行であり、N (1 ≤ N ≤ 9999) は四桁の数を示す。N < 1000 の場合は上の桁は 0 で埋められている。

データセットの数は 10000 を超えない。

出力

各データセットごとに何回の操作で 6174 に到達したかを１行に出力する。ただし全桁が同じ数字である数が入力として与えられた場合は NA と出力する。

入力例

6174
2012
3333
0000

出力例

0
3
NA



Sample input:
6174
2012
3333
0000


Sample output:
0
3
NA

Submission status: Wrong Answer

Solution code:
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))

Submission status: Wrong Answer

Solution code:
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)
                break
	
	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))

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
import itertools

D=[99999 for i in range(10000)]
D[6174]=0
N=[6174]
s=1
while(len(N)>0):
    M=[]
    for n in N:
        p=(10-n%10)%10
        q=(n-999*p)/90
        if p*999+q*90==n and q<10 and q>-1:
            for a in range(p,10):
                for b in range(q,10):
                    c,d=[b-q, a-p]
                    for X in set(list(itertools.permutations([a,b,c,d]))):
                        r=X[0]*1000+X[1]*100+X[2]*10+X[3]
                        if D[r]>s:
                            D[r]=s
                            M.append(r)
    N=M[:]
    s+=1
while(1):
    v=int(raw_input())
    if v==0000:    break
    if v%1111==0:    print "NA"
    else:
        print D[v]

Submission status: Wrong Answer

Solution code:
while True:
  n = input()
  if not n: break

  s = [("%04d" % n)[i] for i in range(4)]
  if len(set(s)) == 1:
    print "NA"
    continue

  cnt = 0
  while n != 6174:
    cnt += 1
    s = [("%04d" % n)[i] for i in range(4)]
    n = int("".join(reversed(sorted(s)))) - int("".join(sorted(s)))

  print cnt

Submission status: Time Limit Exceeded

Solution code:
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)

Submission status: Time Limit Exceeded

Solution code:
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
	
	c = 0
	while 1:
		if "".join(line) == "6174":
			print(c)
			break
		else:
			line.sort()
			l = int("".join(line[::-1]))
			s = int("".join(line))
			line = list(str(l - s))
			c += 1

Submission status: Time Limit Exceeded

Solution code:
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

Submission status: Time Limit Exceeded

Solution code:
data = [str(i+1)*4 for i in range(9)]
while True:
    n = raw_input()
    if n == '0000': 
        break
    elif n in data:
        print 'NA'
        continue
    c = 0
    while n != '6174':
        L = int(''.join(sorted(list(n), key=int, reverse=True)))
        S = int(''.join(sorted(list(n), key=int)))
        n = str(L-S)
        c += 1
    else:
        print c

Submission status: Time Limit Exceeded

Solution code:
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)

Submission status: Time Limit Exceeded

Solution code:
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)

Submission status: Time Limit Exceeded

Solution code:
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()

Submission status: Time Limit Exceeded

Solution code:
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()

Submission status: Time Limit Exceeded

Solution code:
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)

Submission status: Time Limit Exceeded

Solution code:
# 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)

Submission status: Time Limit Exceeded

Solution code:
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)

Submission status: Time Limit Exceeded

Solution code:
while 1:
    w = raw_input()
    if w=="0000": break
    if int(w)%1111==0:
        print "NA"
        continue
    ans = 0
    while(w!="6174"):
        w = str(int("".join(sorted(w,reverse=True)))-int("".join(sorted(w))))
        ans += 1
    print ans

Submission status: Accepted

Solution code:
def calculate(num,i):
		num.sort(reverse=True)
		if num[0] == num[3]:
			return "NA"
		L = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3]
		S = num[3] * 1000 + num[2] * 100 + num[1] * 10 + num[0]
		new_N = L - S
		if new_N == 6174:
			return i + 1
		input_str = "%04d" % new_N
		return calculate(map(int,input_str),i+1)
ans = []
while(1):
	input_str = raw_input()
	if input_str == '0000':
		break
	elif input_str == '6174':
		ans.append(0)
	else:
		num = map(int,input_str)
		ans.append(calculate(num,0))
for value in ans:
	print value

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
while 1:
	n = raw_input()
	if n == "0000":break
	n = list(n)
	done = False
	for x in xrange(99):
		if "".join(n) == "6174":
			print x
			done = True
			break
		n.sort()
		m = "".join(n)
		n = list(str(int(m[::-1]) - int(m)))
		while(len(n) < 4):n = ['0'] + n
	if not done:
		print "NA"

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
data = [str(i+1)*4 for i in range(9)]
while True:
    n = raw_input()
    if n == '0000': 
        break
    elif n in data:
        print 'NA'
        continue
    c = 0
    while n != '6174':
        L = int(''.join(sorted(list(n), key=int, reverse=True)))
        S = int(''.join(sorted(list(n), key=int)))
        n = str(L-S).zfill(4)
        c += 1
    else:
        print c

Submission status: Accepted

Solution code:
data = [str(i+1)*4 for i in range(9)]
while True:
    n = raw_input()
    if n == '0000': 
        break
    elif n in data:
        print 'NA'
        continue
    c = 0
    while n != '6174':
        L = int(''.join(sorted(n, reverse=True)))
        S = int(''.join(sorted(n)))
        n = str(L-S).zfill(4)
        c += 1
    else:
        print c

Submission status: Accepted

Solution code:
while True:
    n = int(raw_input())
    if n == 0: break
    cnt = 0
    while True:
        if n == 6174: print cnt; break;
        a = []
        for i in range(4):
            x = n/(10**(3-i))
            a.append(x)
            n-= x * (10**(3-i))
        a.sort()
        for i in range(4):
            n += (a[3-i] - a[i]) * (10**(3-i))
        if n == 0:
            print "NA"; break;
        cnt += 1

Submission status: Accepted

Solution code:
def is_repdigit(n):
    b = True
    tmp = list(n)
    for t in tmp:
        if t != tmp[0]:
            b = False
    return b

def completion(n):
    tmp = list(n)
    if len(tmp) < 4:
        while len(tmp) != 4:
            tmp.append('0')
    return "".join(tmp)

while True:
    n = raw_input()
    if n == "0000":
        break
    i = 0
    while True:
        if n == "6174":
            print i
            break

        n = completion(n)
        if is_repdigit(n):
            print "NA"
            break

        tmp = list(n)
        tmp.sort()
        s = "".join(tmp)
        tmp.reverse()
        l = "".join(tmp)
        n = str(int(l) - int(s))
        i += 1

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))).zfill(4)
        cnt += 1

    print(cnt)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# 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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# 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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
while True:
  n = int(raw_input())
  if not n: break

  s = [("%04d" % n)[i] for i in range(4)]
  if len(set(s)) == 1:
    print "NA"
    continue

  cnt = 0
  while n != 6174:
    cnt += 1
    s = [("%04d" % n)[i] for i in range(4)]
    n = int("".join(reversed(sorted(s)))) - int("".join(sorted(s)))
  print cnt

Submission status: Accepted

Solution code:
while True:
	n = raw_input()
	if n == "0000": break
	if len(set(iter(n))) == 1:
		print "NA"
		continue
	count = 0
	while n != "6174":
		s = int("".join(sorted(n)))
		l = int("".join(sorted(n, reverse=True)))
		n =  "%04d" % (l - s)
		count += 1
	print count

Submission status: Accepted

Solution code:
while True:
    N=raw_input()
    if N=="0000":break
    cnt=0
    if len(set(N))==1:
        print "NA"
    else:
        while N!="6174":
            N="0"*(4-len(N))+N
            S="".join(sorted(N))
            L=S[::-1]
            N=str(int(L)-int(S))
            cnt+=1
        print cnt

Submission status: Accepted

Solution code:
while 1:
    w = raw_input()
    if w=="0000": break
    if int(w)%1111==0:
        print "NA"
        continue
    ans = 0
    while(w!="6174"):
        w = ("".join(sorted(w))).zfill(4)
        w = str(int(w[::-1])-int(w))
        ans += 1
    print ans

Submission status: Accepted

Solution code:
while 1:
	n=list(raw_input())
	if n==["0"]*4: break
	elif n[0]==n[1]==n[2]==n[3]:
		print "NA"
		continue
	count=0
	while 1:
		if n==["6","1","7","4"]:
			print count
			break
		l=sorted(n)[::-1]
		s=sorted(n)[n.count("0"):]
		n=list(str(int("".join(map(str,l)))-int("".join(map(str,s)))))
		for i in range(4-len(n)):
			n.insert(0,"0")
		count+=1

Submission status: Accepted

Solution code:
while 1:
	n=list(raw_input())
	if n==["0"]*4: break
	elif n[0]==n[1]==n[2]==n[3]:
		print "NA"
		continue
	count=0
	while 1:
		if n==["6","1","7","4"]:
			print count
			break
		l=int("".join(map(str,sorted(n)[::-1])))
		s=int("".join(map(str,sorted(n)[n.count("0"):])))
		n=list(str(l-s))
		for i in range(4-len(n)):
			n.insert(0,"0")
		count+=1

Submission status: Accepted

Solution code:
while 1:
	n=list(raw_input())
	if n==["0"]*4: break
	elif n[0]==n[1]==n[2]==n[3]:
		print "NA"
		continue
	count=0
	while 1:
		if n==["6","1","7","4"]:
			print count
			break
		l=int("".join(map(str,sorted(n)[::-1])))
		s=int("".join(map(str,sorted(n)[n.count("0"):])))
		n=list(str(l-s))
		n=["0"]*(4-len(n))+n
		count+=1

Submission status: Accepted

Solution code:
while 1:
	n=list(raw_input())
	if n==["0"]*4: break
	elif n[0]==n[1]==n[2]==n[3]:
		print "NA"
		continue
	count=0
	while 1:
		if n==["6","1","7","4"]:
			print count
			break
		n=sorted(n)
		l=int("".join(map(str,n[::-1])))
		s=int("".join(map(str,n[n.count("0"):])))
		n=list(str(l-s))
		n=["0"]*(4-len(n))+n
		count+=1

Submission status: Accepted

Solution code:
while 1:
    n=raw_input()
    if n=="0000": break
    if int(n)%1111==0:
        print "NA"
        continue
    ans = 0
    while n!="6174":
        n="".join(sorted(n))
        n=str(int(n[::-1])-int(n)).zfill(4)
        ans+=1
    print ans

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# -*- 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:])

Submission status: Accepted

Solution code:
# 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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Runtime Error

Solution code:
def calculate(num,i):
		num.sort(reverse=True)
		if num[0] == num[3]:
			return "NA"
		L = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3]
		S = num[3] * 1000 + num[2] * 100 + num[1] * 10 + num[0]
		new_N = L - S
		if new_N == 6174:
			return i + 1
		return calculate(map(int,str(new_N)),i+1)
ans = []
while(1):
	input_str = raw_input()
	if input_str == '0000':
		break
	elif input_str == '6174':
		ans.append(0)
	else:
		num = map(int,input_str)
		ans.append(calculate(num,0))
for value in ans:
	print value