Go: package main

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

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 {
		var s string
		fmt.Scan(&s)
		v, _ = strconv.Atoi(s)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

C++: #include<stdio.h>
int main (){
	int N[4],L,S,c,i,a;
	while(1){
		for(i=0;i!=4;i++){
			scanf("%1d",&N[i]);
		}
		if(N[0]==N[1]&&N[2]==N[3]&&N[1]==N[2]){
			if(N[1]==0)
				break;
			else
				puts("NA");
		}
		else{
			for(i=0;c!=6174;i++){
				for(S=0;S!=4;S++){
					for(a=S;a!=4;a++){
						if(N[S]<N[a]){
							c=N[a];
							N[a]=N[S];
							N[S]=c;
						}
					}
				}
				L=1000*N[0]+100*N[1]+10*N[2]+N[3];
				S=1000*N[3]+100*N[2]+10*N[1]+N[0];
				L-=S;
				c=L;
				N[0]=L/1000;
				L%=1000;
				N[1]=L/100;
				L%=100;
				N[2]=L/10;
				L%=10;
				N[3]=L;
			}
			printf("%d\n",i);
		}
	}
}

C++: //ソートを使いたくなかっただけなんだ
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	while (true)
	{
		int n;
		cin >> n;
		stringstream ss;
		ss << setfill('0') << setw(4) << n;
		string N = ss.str();
		/*
		string N;
		cin >> N;
		*/
		if (N == "0000")
		{
			break;
		}
		else if (N[0]==N[1] && N[1]==N[2] && N[2]==N[3])
		{
			cout << "NA" << endl;
			continue;
		}

		int cnt1 = 0;
		while (N != "6174")
		{
			int cnts[10];
			for (int i=0; i<10; i++)
			{
				cnts[i] = 0;
				unsigned int j = 0;
				while ((j=N.find('0'+i, j)) != string::npos)
				{
					++j;
					++cnts[i];
				}
			}
			string L="0000", S="0000";
			int cnt2 = 0;
			for (int i=0; i<10; i++)
			{
				for (int j=0; j<cnts[i]; j++)
				{
					L[3-cnt2] = S[cnt2] = '0'+i;
					++cnt2;
				}
			}

			stringstream ss_L(L), ss_S(S);
			int i_L, i_S;
			ss_L >> i_L;
			ss_S >> i_S;
			stringstream ss_N;
			ss_N << setfill('0') << setw(4) << i_L-i_S;
			N = ss_N.str();
			++cnt1;
		}
		cout << cnt1 << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<sstream>
#include<string>
using namespace std;
int cnv(string str,bool gFlag){
    sort(str.begin(),str.end());
    if(gFlag)reverse(str.begin(),str.end());
    stringstream ss(str);
    int n;
    ss>>n;
    return n;
}
int main(){
    int n;
    while(cin>>n,n){
        int cnt=0,left=0;
        bool flag=false;
        while(n!=6174){
            stringstream ss;
            ss<<n;
            string str;
            ss>>str;
            n=cnv(str,true)-cnv(str,false);
            cnt++;
            if(left==n){
                flag=true;
                break;
            }
            left=n;
        }
        if(flag)cout<<"NA"<<endl;
        else cout<<cnt<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstdlib>
#define rep(i,a) for( int i = 0; i != (a); ++i )

std::string intToStr( int n )
{
	char str[5];
	sprintf( str, "%04d", n );

	return str;
}

int main()
{
	std::string L, S;
	while( std::cin >> L, L != "0000" )
	{
		bool fl = true;

		rep( i, 3 )
			fl &= L[i+1] == L[i];
		if( fl )
		{
			std::cout << "NA" << std::endl;
			continue;
		}

		int ans = 0;
		while( L != "6174" )
		{
			S = L;
			std::sort( L.rbegin(), L.rend() );
			std::sort( S.begin(), S.end() );

			++ans;

			L = intToStr( atoi( L.c_str() ) - atoi( S.c_str() ) );
		}

		std::cout << ans << std::endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);(i)++)

string N;

int solve(){
  string s = (N);
  int cou = 0;
  while(s != "6174"){
    sort(s.begin(),s.end());
    if(s.front() == s.back()) return -1;
    int S = atoi(s.c_str());
    reverse(s.begin(),s.end());
    int L = atoi(s.c_str());
    s = to_string(L - S);
    while(s.size() < 4) s.push_back('0');
    cou++;
  }

  return cou;
}
int main(){
  while(cin >> N,N != "0000"){
    int ans = solve();
    if(ans == -1){
      cout << "NA" << endl;
    }
    else{
      cout << ans << endl;
    }
  }
}

C++: #include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;

signed main(){
    cin.tie(0); 
    ios::sync_with_stdio(false);
    int n;
    int value[4];
    int count;
    int comp;
    int temp;
    bool judge;
    int l,s;
    cin>>n;
    while( n != 0){
        judge = true;
        for(int i=1;i<=9;i++){
            comp = i;
            for(int j=1;j<4;j++){
                comp = comp*10;
                comp += i;
            }
            if(comp == n){
                judge = false;
                break;
            }
        }
        if(judge){
            count=0;
            while(n != 6174){
                temp = n;
                for(int i=0;i<4;i++){
                    value[i] = temp % 10;
                    temp = temp / 10;
                }
                sort(value,value+4,greater<int>());
                l = 0;
                for(int i=0;i<4;i++){
                    l *= 10;
                    l += value[i];
                }
                sort(value,value+4);
                s = 0;
                for(int i=0;i<4;i++){
                    s *= 10;
                    s += value[i];
                }
                n = l - s;
                count++;
            }
            cout<<count<<"\n";
        }else{
            cout<<"NA\n";
        }
        cin>>n;
    }
    return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include<functional>
using namespace std;
int main(void){
	int n,i,a,j;
	int de,syo,tem[4];
	while(1){
		scanf("%000d",&n);
		if(n==0000)
			break;
		int cnt=0,flg=0;
		while(n!=6174){
			for(i=0;i<4;i++){
				a=1;
				for(j=0;j<3-i;j++){
					a*=10;
				}
				tem[i]=n/a;
				n-=tem[i]*a;
			}
		/*	if(tem[0]==tem[1]==tem[2]==tem[3]){
				flg=1;
				printf("%d %d %d %d\n",tem[0],tem[1],tem[2],tem[3]);
				break;
			}
	*/		sort(tem,tem+4);
			a=1000;
			de=syo=0;
			for(i=0;i<4;i++){
				syo+=tem[i]*a;
				a/=10;
			}
			sort(tem,tem+4,greater<int>());
			a=1000;
			for(i=0;i<4;i++){
				de+=tem[i]*a;
				a/=10;
			}
			if(de==syo){
				flg=1;
				break;
			}
			cnt++;
		//	printf("a=%d %d\n",de,syo);
			n=de-syo;
		}
		if(flg==0){
			printf("%d\n",cnt);
		}
		else {
			printf("NA\n");
		}
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;

int main(){
    int n;
    
    while(cin >> n,n){
        int cnt = 0;
        if(n == 6174){
            cout << cnt << endl;
            continue;
        }
        while(true){
            cnt++;
            int d[4],m=10,NA=0;
            for(int i = 0 ; i < 4 ; i++){
                d[i] = n % m;
                n /= 10;
                if(i&&d[i]==d[i-1]) NA++;
            }
            if(NA == 3){
                cout << "NA" << endl;
                break;
            }
            sort(d,d+4);
            int L=d[3]*1000+d[2]*100+d[1]*10+d[0];
            int S=d[0]*1000+d[1]*100+d[2]*10+d[3];
            if(L-S==6174){
                cout << cnt << endl;
                break;
            }
            else n = L-S;
        }
    }
    
    return 0;
}