Go: package main

import (
	"fmt"
	"sort"
)

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]
	return solve(l-s) + 1
}

func main() {
	var v int
	memo = make([]int, 10000)
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

C++: #include <stdio.h>

int main(void)
{
	for(;;)
	{
		int n,a[4],b,c,d,f=0,l,s;
		scanf("%d",&n);
		if(n==0)
		{
			break;
		}
		else if(n%1111==0)
		{
			printf("NA\n");
		}
		else
		{
			for(;;)
			{
				if(n==6174)
				{
					break;
				}
				a[0]=n/1000;
				a[1]=n/100-a[0]*10;
				a[2]=n%100/10;
				a[3]=n%10;
				for(b=0;b<4;b++)
				{
					for(c=b+1;c<4;c++)
					{
						if(a[b]<a[c])
						{
							d=a[b];
							a[b]=a[c];
							a[c]=d;
						}
					}
				}
				l=(a[0]*1000+a[1]*100+a[2]*10+a[3]);
				s=(a[0]+a[1]*10+a[2]*100+a[3]*1000);
				n=l-s;
				f++;
			}
			printf("%d\n",f);
		}
	}
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
using namespace std;
int main() {
	while (true) {
		int n[4], cnt;
		char wat[4];
		scanf("%s", &wat);
		if (wat[0] == '0'&&wat[1] == '0'&&wat[2] == '0'&&wat[3] == '0') { goto endnow; }
		for (int i = 0;i < 4;++i) {
			n[i] = (int) wat[i] - 48;
		}
		for (int i = 1;i < 10;++i) {
			if (n[0] == i&&n[1] == i&&n[2] == i&&n[3] == i) {
				printf("NA\n");
				goto Ihatemath;
			}
		}
		cnt = 0;
		while (true) {
			int l[4];
			if (n[0] == 6 && n[1] == 1 && n[2] == 7 && n[3] == 4) {
				printf("%d\n", cnt);
				goto Ihatemath;
			}
			memcpy(l, n, sizeof(n));
			sort(l, l + 4,greater<int>());
			int lon = l[0] * 1000 + l[1] * 100 + l[2] * 10 + l[3];
			int shor = l[0] + l[1] * 10 + l[2] * 100 + l[3] * 1000;
			int wanna = lon - shor;
			n[0] = wanna / 1000;
			wanna = wanna % 1000;
			n[1] = wanna / 100;
			wanna = wanna % 100;
			n[2] = wanna / 10;
			wanna = wanna % 10;
			n[3] = wanna;
			cnt++;
		}
	Ihatemath:;
	}
endnow:;
	return 0;
}

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

#define pb push_back

int memo[10010];
int cnt(int n){
	vector<int> vec;
	vec.pb(n%10);
	vec.pb((n/10)%10);
	vec.pb((n/100)%10);
	vec.pb(n/1000);
	sort(vec.begin(),vec.end());
	int l = 0 , s = 0;
	for(int i = 0 ; i < 4 ; i ++){
		l *= 10;
		l += vec[vec.size()-1-i];
	}
	for(int i = 0 ; i < 4 ; i ++){
		s *= 10;
		s += vec[i];
	}
	return l-s;
}
int dfs(int n){
	if(memo[n] == -1)return memo[n] = dfs(cnt(n))+1;
	else return memo[n];
}

int main(){
	for(int i = 0 ; i < 10010 ; i ++){
		memo[i] = -1;
	}
	memo[6174] = 0;
	while(1){
		int n;
		scanf("%d",&n);
		if(n == 0)break;
		if(n%1111 == 0){
			puts("NA");
			continue;
		}
		printf("%d\n",dfs(n));
	}
}

C++: #include "bits/stdc++.h"
// Custom Header {{{
#define ALL(x) x.begin(), x.end()
#define endl '\n'
using namespace std;
using i64 = long long;
using pii = pair<i64, i64>;
template<class A, class B>inline bool chmax(A &a, const B &b){return b>a ? a=b,1 : 0;}
template<class A, class B>inline bool chmin(A &a, const B &b){return b<a ? a=b,1 : 0;}
constexpr int INF  = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;
// }}}


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

    string s;
    char buf[5];
    while(cin >> s, s != "0000") {
        int ans = 0;

        if (s[0] == s[1] && s[0] == s[2] && s[0] == s[3]) {
            cout << "NA" << endl;
            continue;
        }

        while(s != "6174") {
            string L = s;
            string S = s;
            sort(ALL(L), greater<>());
            sort(ALL(S));
            sprintf(buf, "%04d", stoi(L) - stoi(S));
            s = buf;
            ++ans;
        }

        cout << ans << endl;
    }
    return 0;
}

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

int stoi(string s){
  stringstream ss(s);
  int res; ss >> res;
  return res;
}

int main(){
  string s;
  while(cin >> s,s!="0000"){
    bool f = true;
    for(int i=0;i<3;i++)f &= (s[i]==s[i+1]);
    if(f){ cout << "NA\n"; continue; }

    int cnt = 0;
    while(s!="6174"){
      sort(s.begin(),s.end());
      int S = stoi(s);
      reverse(s.begin(),s.end());
      int L = stoi(s);
      char* N; sprintf(N,"%04d",L-S);
      for(int i=0;i<4;i++)s[i] = N[i];
      cnt++;
    }
    cout << cnt << endl;
  }
}

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

string cn;
int main()
{
  while(true){
    cin >> cn;
    if(cn == "0000"){
      break;
    }
    if(cn[0] == cn[1] && cn[0] == cn[2] && cn[0] == cn[3]){
      cout << "NA" << endl;
    }

    else{
      int ans = 0;
      while(cn != "6174"){
	int in, intss;
	stringstream ss1, ss2, ss3;
	string cncp;
	sort(cn.begin(), cn.end(), greater<char>());
	ss1 << cn;
	ss1 >> intss;
	in = intss;
	for(int j = 0; j < 4; j++){
	  cncp += cn[3 - j];
	}
	ss2 << cncp;
	ss2 >> intss;
	in -= intss;
	ss3 << in;
	ss3 >> cn;
	ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}

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

int N;
int Ns, Nl;
int num[4];
int S[4], L[4];
int result;

int main() {
    while (true) {
        result = 0;
        cin >> N;
        
        if (!N) break;            
        
        if (N != 6174) {
            num[0] = N / 1000;
            N %= 1000;
            num[1] = N / 100;
            N %= 100;
            num[2] = N / 10;
            N %= 10;
            num[3] = N;
            
            while (true) {
                Ns = 0; Nl = 0;
                int judge = 0;
                for (int i = 0; i < 4; i++) {
                    if (num[0] == num[i]) judge++;
                }

                if (judge == 4) {
                    cout << "NA" << endl;
                    break;
                }

                sort(num, num + 4);
                for (int i = 0; i < 4; i++) {
                    S[i] = num[i];
                }

                Ns += S[0] * 1000;
                Ns += S[1] * 100;
                Ns += S[2] * 10;
                Ns += S[3];

                sort(num, num + 4, greater<int>());
                for (int i = 0; i < 4; i++) {
                    L[i] = num[i];
                }

                Nl += L[0] * 1000;
                Nl += L[1] * 100;
                Nl += L[2] * 10;
                Nl += L[3];

                N = Nl - Ns;

                num[0] = N / 1000;
                N %= 1000;
                num[1] = N / 100;
                N %= 100;
                num[2] = N / 10;
                N %= 10;
                num[3] = N;

                result++;

                if(num[0] == 6 && num[1] == 1 && num[2] == 7 && num[3] == 4) {
                    cout << result << endl;
                    break;
                }
            }
        }
        else cout << 0 << endl;
        
    }
    
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
string s;
int main() {
	while(cin >> s, s != "0000") {
		if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
			cout << "NA" << endl;
			continue;
		}
		for(int i = 0; ; i++) {
			if(s == "6174") {
				cout << i << endl;
				break;
			}
			sort(s.begin(), s.end());
			string t = s;
			reverse(t.begin(), t.end());
			//cout << s << ' ' << t << endl;
			s = to_string(abs(stoi(s) - stoi(t)));
			while(s.size() < 4) s = "0" + s;
		}
	}
}