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))
	}
}

C++: #include "stdio.h"
#include "math.h"

#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

const int INF = 10000000;
using namespace std;
typedef long long  ll;

/** Problem0259 : All Numbers Lead to 6174 **/
int main()
{
	string s;
	int  min, max, num[4], n;
	
	while (cin>>s, s!="0000") {
		n=1;
		if (s=="6174") {
			cout << 0 << endl;
			continue;
		} else if (s[0]==s[1] && s[0]==s[2] && s[0]==s[3]) {
			cout << "NA" << endl;
			continue;
		}
		while (1) {
			min=0; max=0;
			fill(num, num+4, 0);
			rep(i, s.length()) {
				num[i] = s[i]-'0';
			}
			sort(num, num+4);
			rep(i, 4) {
				min*=10;
				min+=num[i];
			}
			reverse(num, num+4);
			rep(i, 4) {
				max*=10;
				max+=num[i];
			}
			int nn = max-min;
			if (nn == 6174) {
				cout << n << endl;
				break;
			} else {
				s="";
				rep(i, 4) {
					s+= nn%10;
					nn/=10;
				}
			}
			n++;
		}
	}
	
	return 0;
}

C++: #include<iostream>
using namespace std;
int main()
{
    int input = 0;
    int digit[4];
    int max, min;
    int base[4]={1000,100,10,1};
    int c=0,i,j,tmp;

    while(input != 6174)
    {
        cin >> input;
        if(input == 0) return 0;
        if(input % 1111 == 0){
	    cout << "NA" << endl;
	    return 0;
        }
        for(i=0; i<4; i++) // 各桁を取り出す
        {
            digit[i]= input / base[i];
            input = input % base[i]; // Highestを除く
        }
        for(i=0;i<3;i++) // sort
        {
            for(j=i+1;j<4;j++)
            {
                if(digit[i]<digit[j])
                {
                    tmp=digit[i];
                    digit[i]=digit[j];
                    digit[j]=tmp;
                }
            }
        }
	max = min = 0;
        for(i=0;i<4;i++)
        {
            max += (digit[i]*base[i]);
            min += (digit[i]*base[3-i]);
        }
        input = max-min;
        c++;
    }
    cout << c << endl;
    return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int n;
	int l,s;
	int i,j,temp;
	int keta[4];
	int cnt;
	
	scanf("%d",&n);
	
	while(n!=0000){
		if(n%1111==0){
			printf("NA\n");
			scanf("%d",&n);
		return 0;
		}
		
		cnt=0;
		while(n!=6174){
			for(i=0;i<4;i++){
				keta[i]=n%10;
				n=n/10;
			}
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(keta[i]<keta[j]){
						temp=keta[i];
						keta[i]=keta[j];
						keta[j]=temp;
					}
				}
			}
			l=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];

			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(keta[i]>keta[j]){
						temp=keta[i];
						keta[i]=keta[j];
						keta[j]=temp;
					}
				}
			}
			s=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];
			n=l-s;
			cnt++;
		}
	printf("%d\n",cnt);
	scanf("%d",&n);
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <math.h>

using namespace std;



int main(){

	int N;

	while (cin >> N){

		if (N == 0){

			break;

		}else if(N % 1111 == 0){
			cout << "NA\n";
			continue;
		}
		int cnt = 0;
		while(N != 6174){
			int n[4];
			for(int i = 0; i < 4; i++){
				n[i] = N / pow(10,3 - i);
				N %= (int)pow(10,3 - i);
			}
			sort(n,n + 4,greater<int>());
			int L = n[0]*1000 + n[1]*100 + n[2]*10 + n[3];
			sort(n,n + 4);
			int S = n[0]*1000 + n[1]*100 + n[2]*10 + n[3];
			N = L - S;
			cnt++;
		}
		cout << cnt << endl;

	}

	return 0;

}

C++: #include<iostream>

using namespace std;

bool func_a(int *N, int *count);

bool main()
{
	int N = 0;
	int count = 0;
	bool flag = true;

	cin >> N;
	
	if(N == 6174)
	{
		cout << 0 << endl;
		flag = false;
	}

	for(int i = 0; i <= 9; i++)
	{
		if(N == i + i * 10 + i * 100 + i * 1000)
		{
			cout << "NA" << endl; 
			flag = false;
		}
	}
	
	if(flag)
	{
		while( func_a(&N, &count) ){};

		cout << count << endl;
	}

	return 0;
}

bool func_a(int *N, int *count)
{	
	int num = *N;
	int Nmax[4];
	int Nmin[4];
	int L = 0, S = 0;

	*count += 1;

	for(int i = 0; i < 4; i++)
	{
		Nmax[i] = Nmin[i] = num % 10;
		num /= 10;
	}

	for(int i = 0; i < 4; i++)
	{
		for(int j = 0; j < 4; j++)
		{
			if(Nmax[i] < Nmax[j])
			{
				int tmp = Nmax[i];
				Nmax[i] = Nmax[j];
				Nmax[j] = tmp;
			}
			if(Nmin[i] > Nmin[j])
			{
				int tmp = Nmin[i];
				Nmin[i] = Nmin[j];
				Nmin[j] = tmp;
			}
		}
	}

	int dz[4] = {1, 10, 100, 1000};

	for(int i= 0; i < 4; i++)
	{
		L += Nmax[i] * dz[i];
		S += Nmin[i] * dz[i];
	}

	*N = L - S;

	if(*N == 6174)
	{
		return false;
	}
	else
	{
		return true;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(void)
{	
	string n,n1,n2,n3,y3;
	string x1,y1,y2,g="1000";
	int x=0;
	int i,x2;
	int flg=0;
	int a[5],b[5],c[5];
	while(1){
		cin>>n;
		if(n=="0000"){
			break;
		}
		flg=0;
		x=0;
		while(1){
			n1="";
			n2="";
			n3="";
			x1="";
			y1="";
			y2="";
			y3="";
			
			for(i=0;i<4;i++){
				x1=n[i];
				a[i]=stoi(x1);
			}
			if(n=="6174"){
				break;
			}
			if(a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3]){
				flg=1;
				break;
			}
			
			//一回目
			sort(a,a+4,greater<int>());
			/*for(i=0;i<4;i++){
				cout<<a[i];
			}
			cout<<endl;*/
			//x++;
			for(i=0;i<4;i++){
				b[i]=a[i];
				x2=a[i];
				y3=to_string(x2);
				n1+=y3;
			}
			if(n1=="6174"){
				break;
			}
			
			//二回目
			sort(a,a+4);
			/*for(i=0;i<4;i++){
				cout<<a[i];
			}*/
			//cout<<endl;
			//x++;
			for(i=0;i<4;i++){
				c[i]=a[i];
				x2=a[i];
				n2+=to_string(x2);
			}
			//cout<<n2<<endl;
			if(n2=="6174"){
				break;
			}
			//三回目
			//x++;
			for(i=0;i<4;i++){
				int f1=c[i];
				y1+=to_string(f1);
				int f2=b[i];
				y2+=to_string(f2);
			}
			//cout<<y1<<" "<<y2<<endl;
			int m;
			m=stoi(y1);
			int e;
			e=stoi(y2);
			int h;
			h=e-m;
			
			//cout<<m<<" "<<e<<endl;
			//cout<<h<<endl;
			n3=to_string(h);
			if(h<1000){
				n3="0"+n3;
			}
			//cout<<n3<<endl;
			x++;
			if(n3=="6174"){
				break;
			}
			n=n3;
			
		}
		if(flg==1){
			cout<<"NA"<<endl;
		}
		else{
			cout<<x<<endl;
		}
	}
	return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <climits>
using namespace std;

int ans[10010];
char tmp[8];

int dfs(int x){
	if(ans[x] >= 0){
		return ans[x];
	}
	if(ans[x] != -1){
		return INT_MIN;
	}
	ans[x] = INT_MIN;

	sprintf(tmp, "%04d", x);
	sort(tmp, tmp + 4);
	int b = strtol(tmp, 0, 10);
	reverse(tmp, tmp + 4);
	int a = strtol(tmp, 0, 10);
	ans[x] = dfs(a - b) + 1;
	return ans[x];
}


int main(){
	for(int i = 0; i < 10005; ++i){
		ans[i] = -1;
	}
	ans[6174] = 0;

	int n;
	while(scanf("%d", &n), n){
		int a = dfs(n);
		printf(a < 0 ? "NA\n" : "%d\n", a);
	}
}