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 <algorithm>
#include <functional>
using namespace std;

int N;
int Ns, Nl;
int num[4];
int S[4], L[4];
int result;

int main() {
    while (true) {
        result = 0;
        cin >> N;
        
        if (!N) break;
        
        num[0] = N / 1000;
        N %= 1000;
        num[1] = N / 100;
        N %= 100;
        num[2] = N / 10;
        N %= 10;
        num[3] = N;
        
        
        while (true) {
            Ns = 0; Nl = 0;
            int judge = 0;
            for (int i = 0; i < 4; i++) {
                if (num[0] == num[i]) judge++;
            }
        
            if (judge == 4) {
                cout << "NA" << endl;
                break;
            }
            
            sort(num, num + 4);
            for (int i = 0; i < 4; i++) {
                S[i] = num[i];
            }
            
            Ns += S[0] * 1000;
            Ns += S[1] * 100;
            Ns += S[2] * 10;
            Ns += S[3];
            
            sort(num, num + 4, greater<int>());
            for (int i = 0; i < 4; i++) {
                L[i] = num[i];
            }
            
            Nl += L[0] * 1000;
            Nl += L[1] * 100;
            Nl += L[2] * 10;
            Nl += L[3];
            
            N = Nl - Ns;
            
            num[0] = N / 1000;
            N %= 1000;
            num[1] = N / 100;
            N %= 100;
            num[2] = N / 10;
            N %= 10;
            num[3] = N;
            
            result++;
            
            if(num[0] == 6 && num[1] == 1 && num[2] == 7 && num[3] == 4) {
                cout << result << endl;
                break;
            }
        }
        
    }
    
    return 0;
}

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

int power(int n)
{
    int num=1;
    for(int i=0;i<n;i++)num*=10;
    return num;
}
int main()
{
    string str;
    int ans=6174;
    while(cin>>str&&str!="0000"){
        int up[4],down[4];
        if(str!="6174"){
            for(int i=0;i<4;i++)up[i]=str[i]-'0';
            bool kawa=0;
            for(int i=1;!kawa;i++){
                for(int j=0;j<4;j++){
                    for(int k=3;k>j;k--)if(up[k]<up[k-1])swap(up[k],up[k-1]);
                }
                for(int j=0;j<4;j++)down[j]=up[3-j];
                int num[2]={};
                for(int j=0;j<4;j++){
                    num[0]+=down[j]*power(3-j);
                    num[1]+=up[j]*power(3-j);
                }
                num[0]-=num[1];
                if(num[0]==ans||num[0]==0){
                    if(num[0]==0)cout<<"NA\n";
                    else cout<<i<<endl;
                    kawa=1;
                }
                up[0]=num[0]/1000;
                up[1]=num[0]%1000/100;
                up[2]=num[0]%100/10;
                up[3]=num[0]%10;
            }
        }
        else cout<<"0\n";
    }
}

C++: #include <vector>
#include <algorithm>
#include <iostream>
#include <functional>

using namespace std;

int func2(int *temp) {
    int n = 0;
    
    n += (temp[0] * 1000);
    n += (temp[1] * 100);
    n += (temp[2] * 10);
    n += temp[3];
       
    return n;
}

int main() {
    while (true) {
        int N = 0, Nm[4] = {0};
        int count = 0;
        int Lm[4] = {0}, Sm[4]={0};
        int S = 0, L = 0;
        
	cin >> N;
        
        Nm[0] = N / 1000;
        N %= 1000;
        Nm[1] = N / 100;
        N %= 100;
        Nm[2] = N / 10;
        N %= 10;
        Nm[3] = N;
        
        if((Nm[0] == 0 && Nm[1] == 0) && (Nm[2] == 0 && Nm[3] == 0))    break;
        
        if (!((Nm[0] == Nm[1]) && (Nm[2] == Nm[3]))) {
            while (true) {
                if (Nm[0] == 6 && Nm[1] == 1 && Nm[2] == 7 && Nm[3] == 4) {
                    break;
                }

                //Sを作成
                sort(Nm, Nm + 4);
                for (int i = 0; i < 4; i++) {   
                    Sm[i] = Nm[i];
                }   
                S = func2(Sm);

                //Lを作成
                sort(Nm, Nm + 4, greater<int>());
                for (int i = 0; i < 4; i++) {
                    Lm[i] = Nm[i];
                }
                L = func2(Lm);

                N = L - S;
                count++;

                Nm[0] = N / 1000;
                N %= 1000;
                Nm[1] = N / 100;
                N %= 100;
                Nm[2] = N / 10;
                N %= 10;
                Nm[3] = N;
            }
            cout << count << endl;
        }
        
        if(((Nm[0] == Nm[1]) && (Nm[2] == Nm[3]))) {
            cout << "NA" << endl;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

string get(int x){
    string s = to_string(x);
    return string(4-s.size(), '0') + s;
}

int main() {
    map<string, int> dp;
    dp["6174"] = 0;
    auto dfs = [&](string s, auto &&f){
        if(dp.count(s)) return dp[s];
        string a = s, b = s;
        sort(a.begin(),a.end());
        sort(b.begin(),b.end(), greater<>());
        return dp[s] = f(get(stoi(b)-stoi(a)), f)+1;
    };
    for (int i = 0; i <= 9999; ++i) {
        if(i%1111 == 0) dp[get(i)] = -1;
        else dfs(get(i), dfs);
    }
    string s;
    while(cin >> s, s != "0000"){
        if(dp[s] < 0) puts("NA");
        else cout << dp[s] << "\n";
    }
    return 0;
}

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

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int main()
{
	string s;
	while(cin >> s)
	{
		if(s == "0000") break;
		
		bool flag = true;
		rep(i,s.size()) if(s[0] != s[i]) flag = false;

		if(flag)
		{
			cout << "NA" << endl;
		}
		else
		{
			int cnt = 0;
			while(true)
			{
				if(s == "6174")
				{
					cout << cnt << endl;
					break;
				}
			
				string temp = s,temp2 = s;
				sort(temp.begin(),temp.end());
				sort(temp2.begin(),temp2.end(),greater<char>());

				int t = atoi(temp.c_str());
				int t2 = atoi(temp2.c_str());

				stringstream ss;
				ss << t2 - t;

				s = ss.str();
				cnt++;
			}
		}

	}
}