Go: package main

import (
	"fmt"
	"sort"
	"strconv"
)

func main() {
	var v int
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		count := 0
		for v != 6174 {
			str := ([]byte)(strconv.Itoa(v))
			sort.Slice(str, func(i, j int) bool {
				return str[i] < str[j]
			})
			s, _ := strconv.Atoi(string(str))
			sort.Slice(str, func(i, j int) bool {
				return str[i] > str[j]
			})
			l, _ := strconv.Atoi(string(str))
			v = l - s
			count++
		}
		fmt.Println(count)
	}
}

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

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);
    return toStr(L-S);
}

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

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 ans = 0;
        while(n != "6174"){
            string l = n, s = n;
            sort(l.begin(), l.end(), greater<char>()); sort(s.begin(), s.end());
            int t = atoi(l.c_str()) - atoi(s.c_str());
            for(int i = 3; i >= 0; --i){
                n[i] = '0' + t % 10;
                t /= 10;
            }
            ++ans;
        }
        cout << ans << endl;
    }
}

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

using namespace std;

int d[4];
string in;
int data[10000];

void solve(){
  bool f = true;
  for(int i=1;i<4;i++) if(in[0] != in[i]) f = false;
  if(f) cout << "NA" << endl;
  else{
    int ans = 0;
    for(int i=0;i<4;i++) d[i] = in[i] - '0';
    while(1){
      if(d[0] == 6 && d[1] == 1 && d[2] == 7 && d[3] == 4) break;
      int l = 0, s = 0;
      sort(d, d+4);
      for(int i=0;i<4;i++){
        s *= 10;
        s += d[i];
        l *= 10;
        l += d[3-i];
      }
      ans++;
      int tmp  = l - s;
      if(data[tmp] != -1){
        ans += data[tmp];
        break;
      }
      for(int i=0;i<4;i++){
        d[3-i] = tmp % 10;
        tmp /= 10;
      }
    }
    int tmp = 0;
    for(int i=0;i<4;i++){
      tmp *= 10;
      tmp += d[3-i];
    }
    data[tmp] = ans;
    cout << ans << endl;
  }
}

main(){
  fill(data, data+10000, -1);
  while(cin >> in){
    if(in == "0000") break;
    solve();
  }
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

signed main() {
	int A[4];
	for (string s; cin >> s&&s != "0000";) {
		int cnt = 0;
		if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
			cout << "NA" << endl;
			continue;
		}
		while (s != "6174") {
			cnt++;
			rep(i, 0, 4) {
				A[i] = s[i] - '0';
			}
			sort(A, A + 4);
			int L = 0, S = 0;
			rep(i, 0, 4) {
				L *= 10;
				S *= 10;
				L += A[3 - i];
				S += A[i];
			}
			s = to_string(L - S);
		}
		cout << cnt << endl;
	}
	return 0;
}