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<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];
				//dump(L); dump(S);
			}
			stringstream ss;
			ss << setfill('0') << setw(4) << L - S;
			s = ss.str();
		}
		cout << cnt << endl;
	}
	return 0;
}

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

int rec(string N, int cnt) {
  vector<char> LN(4), SN(4);
  int L = 0, S = 0;
  for (int i = 0; i < 4; i++) {
    LN[i] = SN[i] = N[i] - '0';
  }
  sort(ALL(LN));
  sort(ALL(SN));
  for (int i = 0; i < 4; i++) {
    L *= 10;
    S *= 10;
    L += LN[3 - i];
    S += SN[i];
  }
  return (L - S == 6174) ? cnt + 1 : rec(to_string(L - S), cnt + 1);
}

signed main()
{
  string N;
  while (cin >> N, N != "0000") {
    if (N[0] == N[1] and N[2] == N[3] and N[0] == N[2]) print("NA");
    else if (N == "6174") print("0");
    else print(rec(N, 0));
  }

  return (0);
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int N;
	while(1){
		cin>>N;
		if(N==0000)break;
		if(N%1111==0){
			cout<<"NA"<<endl;
		}
		   int cnt = 0;
 
        while(N!=6174){
            int n[4];
            n[0] = N/1000;
            n[1] = N/100%10;
            n[2] = N/10%10;
            n[3] = N%10;
            sort(n, n+4);
            int L = n[3]*1000+n[2]*100+n[1]*10+n[0];
            int S = n[0]*1000+n[1]*100+n[2]*10+n[3];
            N = L-S;
            cnt++;
        }
        cout << cnt << endl;
	}
	return 0;
}

C++: # include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { -1,0,1,0 }, dy[4] = { 0,-1,0,1 };
const char dir[4] = { 'u','l','d','r' };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        (wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)
# pragma warning(disable:4996)


string s;
vector<int> v(4);

string calc(string s) {
	REP(i, 4)v[i] = s[i] - '0';
	sort(ALL(v));
	int hi = 0, lo = 0;
	REP(i, 4) {
		lo += v[i];
		lo *= 10;
	}
	lo /= 10;
	reverse(ALL(v));
	REP(i, 4) {
		hi += v[i];
		hi *= 10;
	}
	hi /= 10;
	string ret = to_string(hi - lo);
	while (ret.size() != 4)ret = "0" + ret;
	return ret;
}

int main() {
	while (cin >> s&&s != "0000") {
		for (int i = 1;; i++) {
			if (i == 1000) {
				cout << "NA" << endl;
				break;
			}
			if (s == "6174") {
				cout << i-1 << endl;
				break;
			}
			s = calc(s);
		}
	}
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int stoi(string s){
	int size=s.size(),res=0;
	for(int i=size-1;i>=0;i--){
		res+=(int)(s[i]-'0')*pow(10,size-1-i);
	}
	return res;
}

string itos(int n){
	int size=log(n)/log(10)+1;
	string s="";
	rep(i,size){
		s+=(char)(n/pow(10,size-1-i)+'0');
		n%=(int)pow(10,size-1-i);
	}
	return s;
}

int main(){
	string s,res[2];
	int ma,mi;
	while(cin>>s){
		if(s[0]=='0' && s[1]=='0' && s[2]=='0' && s[3]=='0')break;
		if(s[0]==s[1] && s[1]==s[2] && s[2]==s[3]){
			cout<<"NA"<<endl;
			continue;
		}
		int cnt=0;
		while(s!="6174"){
			cnt++;
			sort(s.begin(),s.end());
			mi=stoi(s);
			reverse(s.begin(),s.end());
			ma=stoi(s);
			s=itos(ma-mi);
		}
		cout<<cnt<<endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<string.h>
#include<map>
#include<queue>

using namespace std;

typedef pair<int,int> pii;

#define rep(i,j) for(int i=0;i<(j);i++)
#define reps(i,j,k) for(int i=j;i<=k;i++)
#define in(i,j,k) ((i)>=(j)&&(i)<=(k))

int main(){
	char n[5],l[5],h[5],*k;
	int cnt,t;
	k = "6174";
	while(~scanf("%s",n)){
		cnt=0;
		strcpy(l,n);
		strcpy(h,n);
		sort(l,l+4);
		sort(h,h+4,greater<char>());
		if(*h=='0')break;
		if(!strcmp(h,l)){
			puts("NA");
			continue;
		}
		while(strcmp(n,k)){
			strcpy(l,n);
			strcpy(h,n);
			sort(l,l+4);
			sort(h,h+4,greater<char>());
			rep(i,4){
				n[i]=h[i]-l[i]+'0';
				if(n[i]<'0'){
					n[i]+=10;
					n[i-1]--;
				}
			}
			cnt++;
		}
		printf("%d\n",cnt);
	}
	return 0;
}