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<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> pl;

int main()
{
	while (1) {
		string s;
		cin >> s;
		if (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0')
			break;
		if (s == "6174") {
			cout << 0 << endl;
		}
		else if (s[0] == s[1] &&  s[2] == s[3] && s[1] == s[2])
			cout << "NA" << endl;
		else {
			ll ans = 0;
			while (s != "6174") {
				int a[4];
				for (int i = 0; i < 4; i++)
					a[i] = s[i] - '0';

				sort(a, a + 4);

				ll small=0,large=0;
				for (int i = 0; i < 4; i++)
					large += a[i] * (ll)pow(10, i);
				
				reverse(a,a+4);
				for (int i = 0; i < 4; i++) 
					small += a[i] * (ll)pow(10, i);
				
				s = to_string(large - small);
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

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

int main(){
    int n, d[4];
    while(cin >> n){
        if(n % 1111 == 0){
            cout << "NA" << endl;
        }else{
            int tmpn = n;
            for(int i = 3; i >= 0; i--){
                d[i] = tmpn%10;
                tmpn /= 10;
            }
            
            int ans = 0;
            while(n != 6174){
                ans++;
                sort(d, d+4);
                n = (1000*d[3] + 100*d[2] + 10*d[1] + d[0]) -
                        (1000*d[0] + 100*d[1] + 10*d[2] + d[3]);
                tmpn = n;
                for(int i = 3; i >= 0; i--){
                    d[i] = tmpn%10;
                    tmpn /= 10;
                }
            }
            
            cout << ans << endl;
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
using i64 = int64_t;
i64 MOD = 1e9 + 7;

i64 solve(){
    
    string n;
    cin >> n;
    int cnt = 0;
    if(n == "0000")
        return MOD;
    if(n[0] == n[1] && n[0] == n[2] && n[0] == n[3]){
        cout << "NA" << endl;
        return 1;
    }
    for(; n != "6174"; ++cnt){
        auto f = [&]{
            i64 v = 0;
            int b = 1000;
            for(int i = 0; i < 4; ++i){
                v += (n[i] - '0') * b;
                b /= 10;
            }
            return v;
        };
        auto g = [&](i64 t){
            string s = "0000";
            int b = 1000;
            for(int i = 0; i < 4; ++i){
                s[i] += (t / b) % 10;
                b /= 10;
            }
            return s;
        };
        sort(n.begin(), n.end());
        i64 t1 = f();
        reverse(n.begin(), n.end());
        i64 t2 = f();
        n = g(t2 - t1);
    }
    cout << cnt << endl;
    
    return true;
}

signed main(){
    while(solve() != MOD);
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<sstream>
#include<queue>
#include<iomanip>
using namespace std;
int latte(string str){
    stringstream ss(str);
    int n;
    ss>>n;
    return n;
}
struct data{
    string str;
    int n;
    data(string a,int b):str(a),n(b){}
    data(){}
};
int main(){
    string str="";
    while(cin>>str,str!="0000"){
        int memo[10000];
        fill(memo,memo+10000,-1);
        queue<data>Q;
        Q.push(data(str,0));
        while(!Q.empty()){
            data ins=Q.front();Q.pop();
            str=ins.str;int n=ins.n;
            if(memo[latte(str)]!=-1)continue;
            memo[latte(str)]=n;
            string a,b;
            a=b=str;
            sort(a.rbegin(),a.rend());sort(b.begin(),b.end());
            int to=latte(a)-latte(b);
            stringstream ss;
            ss<<to;
            string to_;
            ss>>setw(4)>>setfill('0')>>to_;
            if(to_.length()!=4)for(int i=0;i<4-to_.length();i++)to_="0"+to_;
            //cout<<a<<"-"<<b<<"="<<to_<<endl;
            Q.push(data(to_,n+1));
        }
        if(memo[6174]==-1)cout<<"NA"<<endl;
        else cout<<memo[6174]<<endl;
    }
}

C++: #include <stdio.h>
#include <stdlib.h>
const int TARGET = 6174;
const int LEN = 4;

char num[LEN];

void sort(int reverse){
  int i, j, tmp;
  for(i = 0; i < LEN; ++i)
    for(j = i + 1; j < LEN; ++j){
      if((reverse == 0 && num[i] > num[j])
        || (reverse == 1 && num[i] < num[j])){
        tmp = num[i];
        num[i] = num[j];
        num[j] = tmp;
      }
    }
}

void convert(int n){
  int i;
  for(i = LEN - 1; i >= 0; --i){
    num[i] = (n % 10) + '0';
    n /= 10;
  }
}

int isSame(){
  int i, j;
  for(i = 0; i < LEN; ++i)
    for(j = i + 1; j < LEN; ++j)
      if(num[i] != num[j])
        return 0;
  return 1;
}

int main(void) {
  int n, i, cnt = 0;
  scanf("%d", &n);
  convert(n);
  if(isSame()){
    printf("NA\n");
    return 0;
  }

  while(atoi(num) != TARGET){
    sort(0);
    int s = atoi(num);
    sort(1);
    int b = atoi(num);
    convert(b - s);
    ++cnt;
  }

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

C++: #include<iostream>

using namespace std;

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

int 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"; 
			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;
	}
}