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>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <limits>
#include <cmath>
#include <cassert>

using namespace std;
using ll = long long;

const int INF = 1<<30;
const int MOD = (int)1e9 + 7;
const int MAX_N = (int)1e5 + 5;
#define debug(x) cout << #x << ": " << x << endl

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    string N;
    while(cin >> N, N != "0000")
    {
        int cnt = 0;
        char ch = N.front();
        bool ok = false;
        for(int i = 0; i < N.size(); i++) if(ch != N[i]) ok = true;
        if(ok == false)
        {
            cout << "NA" << endl;
            continue;
        }
        while(N != "6174")
        {
            string L = N, S = N;
            sort(L.begin(), L.end(), greater<char>());
            sort(S.begin(), S.end());
            N = to_string(stoi(L) - stoi(S));
            while(N.size() != 4) N.insert(N.begin(), '0');
            cnt++;
        }
        cout << cnt << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
 
const std::string answer="6174";
 
int main()
{
    std::string in_str;
        std::cin>>in_str;
        int L,S;
        int i;
        while(in_str[0]!='0'||in_str[1]!='0'||in_str[2]!='0'||in_str[3]!='0'){
                if(in_str[0]!=in_str[1]||in_str[1]!=in_str[2]||in_str[2]!=in_str[3]){
                        for(i=0;in_str!=answer;i++){
                {
                    std::stringstream sss;
                                std::sort(in_str.begin(),in_str.end());
                                sss<<in_str;
                                sss>>S;
                }
                {
                    std::stringstream sss;
                                std::sort(in_str.begin(),in_str.end(),std::greater<char>());
                                sss<<in_str;
                                sss>>L;
                }
                L-=S;
                {
                    std::stringstream sss;
                                sss<<L;
                                in_str=sss.str();
                }
                        }
                        std::cout<<i<<std::endl;
                }else{
                        std::cout<<"NA"<<std::endl;
                }
                std::cin>>in_str;
        }
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <cstdlib>

using namespace std;

int main(){

	while(1){
		int n=0,l,s;
		int cnt = 0;
		string tmp;

		cin >> tmp;
		n = atoi(tmp.c_str());

		if(n == 0)break;
		if(n % 1111 == 0){
			printf("NA\n");
			continue;
		}

		while(n != 6174){
			sort(tmp.begin(),tmp.end());
			s = atoi(tmp.c_str());

			sort(tmp.begin(),tmp.end(),greater<char>());
			l = atoi(tmp.c_str());
			n = l - s;

			for(int i=0,p=1000;i<4;i++,p/=10){
				tmp[i] = '0' + n/p;
				n %= p;
			}
			n = atoi(tmp.c_str());
			cnt++;
		}
		printf("%d\n", cnt);
	}
	return 0;
}

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

int main()
{
  while (true) {
    int n;
    cin >> n;

    if (n == 0) break;
    if (n % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }

    int cnt = 0;
    while (n != 6174) {
      cnt++;
      int l, s;
      int a[4];
      for (int i = 0; i < n; i++) {
        a[i] = n % 10;
        n /= 10;
      }

      sort(a, a + 4);
      s = a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3];
      l = a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0];

      n = l - s;
    }
    cout << cnt << endl;
  }
}

C++: #include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

string toStr(int a)
{
    ostringstream oss;
    oss << setfill('0') << setw(4) << a;
    return oss.str();
}

int main()
{
    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        int ret = 0;
        while(n != 0 && n != 6174){
            string s = toStr(n);
            string t = s;
            sort(s.rbegin(), s.rend());
            sort(t.begin(), t.end());

            n = atoi(s.c_str()) - atoi(t.c_str());
            ++ ret;
        }

        if(n == 0)
            cout << "NA" << endl;
        else
            cout << ret << endl;
    }
}

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

int compute(string num){
  if ( num[0] == num[1] && num[1] == num[2] && num[2] == num[3] ) return -1;

  int cnt = 0;
  string L, S;
  while(1){
    if ( num == "6174" ) break;
    L = S = num;
    sort(L.begin(), L.end());
    reverse(L.begin(), L.end());
    sort(S.begin(), S.end());
    int x = atoi(L.c_str()) - atoi(S.c_str());
    num[0] = '0' + x/1000; x %= 1000;
    num[1] = '0' + x/100; x %= 100;
    num[2] = '0' + x/10; x %= 10;
    num[3] = '0' + x;
    cnt++;
  }
  return cnt;
}

main(){
  string num;
  while(1){
    cin >> num;
    if ( num == "0000" ) break;
    int a = compute(num);
    if ( a< 0 ) cout << "NA" << endl;
    else cout << a << endl;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

int main()
{
	int m, n[4], mi[4], ma[4], na;
	int	max_int = 0, min_int = 0, n_int, ans;
	bool same = false;
				
	while (1){
		ans = 0;
		
		scanf("%d", &n_int);

		for (int i = 3; i >= 0; i--){
			n[i] = n_int / (int)(pow(10, i)) % 10;
		}

		na = n[0];
		for (int i = 1; i < 4; i++){
			if (na != n[i]) same = true;
		}
		if (!same && na == 0) break;
		if (!same) cout << "NA" << endl;

		while (n_int != 6174){
			max_int = min_int = 0;
			if (!same) break;
			for (int i = 3; i >= 0; i--){
				n[i] = n_int / (int)(pow(10, i)) % 10;
			}
			sort(n, n + 4);

			for (int i = 0; i < 4; i++){
				mi[3 - i] = n[i];
				ma[i] = n[i];
			}

			for (int i = 0; i < 4; i++){
				max_int += ma[i] * (int)(pow(10, i));
				min_int += mi[i] * (int)(pow(10, i));
			}
			n_int = max_int - min_int;
			
			ans++;
		}

		if (same) cout << ans << endl;
	}
return (0);
}

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

using namespace std;

int main()
{
	string S;

	while (true)
	{
		cin >> S;

		if (S == "0000") { break; }

		if (S[0] == S[1] && S[1] == S[2] && S[2] == S[3])
		{
			cout << "NA" << endl;
		}
		else
		{
			int count_ = 0;

			while (S != "6174")
			{
				string S1 = S, S2 = S;

				sort(S1.begin(), S1.end(), greater<char>());
				sort(S2.begin(), S2.end());

				S = to_string(stoi(S1) - stoi(S2));

				while (S.size() < 4)
				{
					S = '0' + S;
				}

				count_++;
			}

			cout << count_ << endl;
		}
	}

	return 0;
}