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<iostream>
#include<string>
using namespace std;

int main()
{
    string str;

    while( cin >> str )
    {
        if( str == "0000" )
            break;
        if( str[0] == str[1] && str[2] == str[3] && str[0] == str[2] )
            cout << "NA" << endl;
        else
        {
            int cnt = 0;
            while( str != "6174" )
            {
                for( int i = 0; i < 4; i++ )
                {
                    for( int j = 3; j >= i + 1; j-- )
                    {
                        if( str[j] < str[j-1] )
                        {
                            char temp = str[j];
                            str[j] = str[j-1];
                            str[j-1] = temp;
                        }
                    }
                }

                int l = 0, r = 0, d = 1;
                for( int i = 0; i < 4; i++ )
                {
                    l += ( str[i] - '0' ) * d;
                    r += ( str[3-i] - '0' ) * d;
                    d *= 10;
                }

                l -= r;
                d = 1000;
                for( int i = 0; i < 4; i++ )
                {
                    str[i] = ( l / d ) % 10 + '0';
                    d /= 10;
                }
                cnt++;
            }
            cout << cnt << endl;
        }
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include <strstream>
#include<algorithm>
#include<cstdio>

#include<functional>

using namespace std;


string IntToString(int number)
{
stringstream ss;
ss << number;
return ss.str();
}

int main(){
	string s;
	while(cin>>s)
	{
		if(s=="0000")break;
		int count=0;
		while (1)
		{
			
			if(s=="6174"){
				puts("0");
				break;
			}
			
			vector<int>n;
			
			for(int i=0; i<4; i++)
			{
				
				n.push_back( (int)s[i]-48 );
			}
			
			sort( n.begin(),n.end(),greater<int>() );
			int L=0,S=0;

			if( (n[0]+n[1]) - (n[2]+n[3]) == 0){
				puts("NA");
				break; 
			}
			
			L += n[0]*1000;
			L += n[1]*100;
			L += n[2]*10;
			L += n[3];
			
			reverse(n.begin(),n.end());
			
			S += n[0]*1000;
			S += n[1]*100;
			S += n[2]*10;
			S += n[3];
			
			s.clear();
			s = IntToString(L-S);
			count++;
			
			if(L-S==6174){
				cout<<count<<endl;
				break;
			}
		}

	}
	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:https://vjudge.net/contest/241120#problem/F>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */
string solve(string S){
    string res;
    for(int i = 0; i < 10;i++){
        if(count(S.begin(),S.end(),'0'+i) == 4) return "NA";
    }
    ll cnt = 0;
    while(true){
        while(S.length() < 4){
            S = "0" + S;
        }
        if(S == "6174") break;
        string L = S;
        sort(L.rbegin(),L.rend());
        sort(S.begin(),S.end());
        S = to_string(atoll(L.c_str())-atoll(S.c_str()));
        cnt++;
    }
    res = to_string(cnt);
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    string S;
    while(cin >> S){
        if(S == "0000") break;
        cout << solve(S) << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

string toString(int n){
  string ret = "";
  while(n){
    ret += (n % 10) + '0';
    n /= 10;
  }
  reverse(ret.begin(), ret.end());
  return ret;
}

int main(){
  string s;

  while(cin >> s){
    if(s == "0000") break;    
    
    if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]){
      cout << "NA" << endl;
      continue;
    }
    
    int cnt = 0;
    while(s != "6174"){
      string L = s, S = s;
      sort(L.rbegin(), L.rend());
      sort(S.begin(), S.end());
      int nex = atoi(L.c_str()) - atoi(S.c_str());
      s = toString(nex);
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main()
{
    string N;
    while ( cin >> N, N != "0000" ) {
        int cnt = 0;
        set < string > st;

        while ( N != "6174" && st.find(N) == st.end() ) {
            string L = N, S = N;
            int val = 0;
            st.insert(N);
            sort(rbegin(L), rend(L));
            sort(begin(S), end(S));
            val = atoi(L.c_str()) - atoi(S.c_str());
            N = to_string(val);
            while ( N.size() < 4 ) {
                N = '0' + N;
            }
            cnt++;
        }

        if ( N == "6174" ) {
            cout << cnt << endl;
        } else {
            cout << "NA" << endl;
        }
    }

    return ( 0 );
}

C++: #include<stdio.h>

int main(){

char ty;
char c1,c2,c3,c4,ch5;
int n1=0,n2=0,n3=0,n4=0,N=0,S=0,P=0,K=0;
int n[5]={};
int F=0;
while(1){
//n1=c1-'0';n2=c2-'0';n3=c3-'0';n4=c4-'0';
	
	bool f=false;
	do{
	scanf("%c%c%c%c%c",&c1,&c2,&c3,&c4,&ch5);
	ty=c1;
	
     if(ty==c1&&ty==c3&&ty==c4&&ty==c2)f=false;
	else f=true;

	if(ty=='0')f=true;
	if(f==false)printf("NA\n");

	}while(f!=true);
	if(c1=='0'&&c2=='0'&&c3=='0'&&c4=='0')break;
	F++;
	if(F==10001)break;
	
	for(int i=0;i<4;i++)
	n[i]=0;
int cnt=0;


n[0]=c1-'0';n[1]=c2-'0';n[2]=c3-'0';n[3]=c4-'0';


for(int i=0;i<3;i++)
for(int j=3;j>i;j--)
if(n[j]>n[j-1]){int t=n[j];n[j]=n[j-1];n[j-1]=t;}

N=1000*n[0]+100*n[1]+10*n[2]+n[3];
S=1000*n[3]+100*n[2]+10*n[1]+n[0];

P=N-S;
if(P!=6174)cnt++;

while(P!=6174){
N=P;
K=P;n[0]=N/1000;
K=N%1000;n[1]=K/100;
K=N%100;n[2]=K/10;
K=N%10;n[3]=K;

//printf(" %d\n",N);

for(int i=0;i<3;i++)
for(int j=3;j>i;j--)
if(n[j]>n[j-1]){int t=n[j];n[j]=n[j-1];n[j-1]=t;}


N=1000*n[0]+100*n[1]+10*n[2]+n[3];
S=1000*n[3]+100*n[2]+10*n[1]+n[0];

cnt++;
P=N-S;

}
printf("%d\n",cnt);
}
return 0;
}