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 <vector>
#include <cstring>
#include <climits>
#include <algorithm>
#include <map>
#include <sstream>
using namespace std;

int toInt(string s) {
    stringstream ss(s);
    int ret;
    ss >> ret;
    return ret;
}

string toStr(int n) {
    stringstream ss;
    ss << n;
    return ss.str();
}

string next(string N) {
    sort(N.begin(), N.end());
    int S = toInt(N);
    reverse(N.begin(), N.end());
    int L = toInt(N);
    string ret = "0000" + toStr(L-S);
    return ret.substr((int)ret.size()-4);
}

int main() {
    string N;
    while (cin >> N, N != "0000") {
        if (N[0] == N[1] && N[1] == N[2] && N[2] == N[3]) {
            cout << "NA" << endl;
            continue;
        }
        int cnt = 0;
        while (N != "6174") {
            N = next(N);
            ++ cnt;
        }
        cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
string itos(int a){
	string s="";
	while(a){
		s=(char)(a%10+'0')+s;
		a/=10;
	}
	while(s.size()!=4)s="0"+s;
	return s;
}
int main(){
	string s;
	while(cin>>s,s!="0000"){
		int co=0;
		while(co!=1000){
			if(s=="6174")break;
			sort(all(s));
			string tmp=s;
			reverse(all(tmp));
			int sa=atoi(&tmp[0])-atoi(&s[0]);
			s=itos(sa);
			co++;
		}
		if(co!=1000)cout<<co<<endl;
		else cout<<"NA"<<endl;
	}
}

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

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, init, n) for (int i = init; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define fi first
#define se second

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct
{
    int to, cost;
};

const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int next(int N)
{
    string S = to_string(N), L;

    while (S.size() < 4)
        S = "0" + S;

    sort(ALL(S));

    REP(i, 4)
    L += S[3 - i];

    return stoi(L) - stoi(S);
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N;

    while (cin >> N, N)
    {
        int ans = 0;

        for (;;)
        {
            if (N % 1111 == 0)
            {
                cout << "NA" << endl;
                break;
            }
            else if (N == 6174)
            {
                cout << ans << endl;
                break; 
            }

            N = next(N);

            ans++;
        }
    }

    return 0;
}

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

std::string sub(const std::string& a, const std::string& b) {
    int a_ = 0, b_ = 0;

    int k = 1;
    for(int i = 3; i >= 0; --i) {
        a_ += k * (a[i] - '0');
        b_ += k * (b[i] - '0');

        k *= 10;
    }

    char hoge[4];
    sprintf(hoge, "%04d", a_ - b_);

    return hoge;
}

int func(int i, const std::string& s) {
    if(s == "6174") {
        return i;
    }

    auto a = s, b = s;

    std::sort(a.begin(), a.end());
    std::sort(b.begin(), b.end(), std::greater<char>());

    return func(i + 1, sub(b, a));
}

inline
bool same(const std::string& a) {
    bool same = true;

    for(int i = 1; i < a.size(); ++i) {
        if(a[i - 1] != a[i]) {
            same = false;
        }
    }

    return same;
}

int main() {
    std::string  n;

    while(std::cin>> n, n != "0000") {
        if(same(n)) {
            std::cout << "NA" << std::endl;

            continue;
        }

        std::cout << func(0, n) << std::endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  while(cin>>n,n){
    for(int i=0;i<4000;i++){
      if(n==6174){cout<<i<<endl;break;}
      vector<int>v;
      for(int j=0;j<4;j++){
        v.push_back(n%10);
        n/=10;
      }
      sort(v.begin(),v.end());
      if(v[0]==v[3]){cout<<"NA"<<endl;break;}
      int p1,p2;
      p1=v[0]+v[1]*10+v[2]*100+v[3]*1000;
      p2=v[3]+v[2]*10+v[1]*100+v[0]*1000;
      n=p1-p2;
    }
  }
}

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

int main()
{
    int N, L, S;
    while(cin>>N, N) {
        vector<int> v(4);
        int ans = N;
        for(int i=0; i<4; i++) {
            v[i] = ans%10;
            ans /= 10;
        }
        if(v[0]==v[1] && v[1]==v[2] && v[2]==v[3]) {
            cout<<"NA\n";
            continue;
        }
        while(N != 6174) {
            ans++;
            for(int i=0; i<4; i++) {
                v[i] = N%10;
                N /= 10;
            }
            sort(v.begin(), v.end());
            L = v[0]*1 + v[1]*10 + v[2]*100 + v[3]*1000;
            S = v[3]*1 + v[2]*10 + v[1]*100 + v[0]*1000;
            N = L-S;
        }
        cout<<ans<<endl;
    }
}

C++: #include<stdio.h>
int main(void)
{
	int n,i,j,c,p[5],a,x=0,z[5],flg;
	while(1){
		scanf("%d",&n);
		if(n==0000) break;
		flg=0;
		c=0;
		while(1){
			if(n==6174) break;
			for(i=0;i<4;i++){
				p[i]=n%10;
				a=n/10;
				n=a;
			}
			if(p[0]==p[1]&&p[0]==p[2]&&p[0]==p[3]){
				flg=1;
				printf("NA\n");
				break;
			}
			c++;
			for(i=0;i<4;i++) z[i]=p[i];
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(z[i]<z[j]){
						x=z[i];
						z[i]=z[j];
						z[j]=x;
					}
				}
			}
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(p[i]>p[j]){
						x=p[i];
						p[i]=p[j];
						p[j]=x;
					}
				}
			}
			n=(z[0]*1000+z[1]*100+z[2]*10+z[3])-(p[0]*1000+p[1]*100+p[2]*10+p[3]);
		}
		if(flg!=1) printf("%d\n",c);
	}
	return 0;
}

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

string operator-(const string &a, const string &b) {
	char buff[128];
	sprintf(buff, "%04d", stoi(a) - stoi(b));
	return string(buff);
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	const string goal = "6174";

	for(string n; cin >> n && n != "0000";) {
		if(all_of(n.begin(), n.end(), [&](char c) { return c == n.front(); })) {
			cout << "NA" << endl;
			continue;
		}

		int cnt = 0;
		while(n != goal) {
			string L(n), S(n);
			sort(L.rbegin(), L.rend());
			sort(S.begin(), S.end());
			n = L - S;
			++cnt;
		}

		cout << cnt << endl;
	}

	return EXIT_SUCCESS;
}