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 <string.h>
#include <math.h>
#include <algorithm>
#include <functional>
using namespace std;

int main(){
	int n,nl,nh;
	char c[5],bl[5],bh[5];

	while(scanf("%s",c), (c[0] | c[1] | c[2] | c[3]) != '0'){
		if(c[0] == c[1] && c[0] == c[2] && c[0] == c[3]){
			printf("NA\n");
			continue;
		}
		int tm = 0;
		while(!(c[0] == '6' && c[1] == '1' && c[2] == '7' && c[3] == '4')){
			strcpy(bl,c);
			strcpy(bh,c);
			sort(bl,bl+4);
			sort(bh,bh+4, greater<char>());
			nl = nh = 0;
			for(int i = 0; i < 4; i++){
				nl += (bl[i] - '0') * pow(10,3-i);
				nh += (bh[i] - '0') * pow(10,3-i);
			}
			n = nh - nl;
			for(int i = 0; i < 4; i++){
				c[3-i] = '0' + (n % 10);
				n /= 10;
			}
			tm++;
		}
		printf("%d\n",tm);
	}

	return 0;
}

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

int main(){
    int cnt,a,b;
    char num[5];
    while(1){
        cin >> num;
        cnt = 0;
        if(strcmp(num,"0000") == 0) break;
        if(num[0] == num[1] && num[1] == num[2] && num[2] == num[3]){
            cout << "NA" << endl;
            continue;
        }
        while(1){
            if(strcmp(num,"6174") == 0) break;
            cnt++;
            sort(num,num + 4);
            a = atoi(num);
            sort(num,num + 4,greater<char>());
            b = atoi(num);
            b -= a;
            sprintf(num,"%d",b);
        }
        cout << cnt << endl;
    }
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
string S; int a[4], b, cnt;
int main() {
	while (true) {
		cin >> S;
		if (S == "0000") { break; }
		if (S[0] == S[1] && S[2] == S[3] && S[1] == S[2]) { cout << "NA" << endl; }
		else {
			b = stoi(S);
			cnt = 0;
			while (b != 6174) {
				cnt++;
				for (int i = 0; i < 4; i++) {
					a[i] = (b / (int)pow(10, i)) % 10;
				}
				sort(a, a + 4);
				b = (a[3] - a[0]) * 1000 + (a[2] - a[1]) * 100 + (a[1] - a[2]) * 10 + (a[0] + a[3]);
			}
			cout << cnt << endl;
		}
	}
}

C++: #include<iostream>

using namespace std;

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

bool 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" << endl; 
			flag = false;
		}
	}
	
	if(flag)
	{
		while( func_a(&N, &count) ){};

		cout << count << endl;
	}

	return false;
}

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

C++: #include <iostream>
#include <algorithm> 
using namespace std;
int main(){
	int N; 
	int n[4];
	while(true){
		int cnt=0;
		cin >> N;
		if(N==0) break;
		if(N%1111==0) {
			cout << "NA\n";
			continue;
		}
		while(N!=6174){
			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<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int main(){
    int n;
    while(scanf("%d",&n),n){
        int cnt = 0;
        bool flg = 0;
        while(1){
            if(n == 6174)break;
            if(n%1111 == 0){
                flg = 1;
                break;
            }
            int a[4]={0};
            a[0] = n/1000;
            a[1] = (n%1000)/100;
            a[2] = (n%100)/10;
            a[3] = n%10;
            int L[4],S[4];
            sort(a,a+4);
            for(int i = 0; i < 4; i++){
                L[3-i] = S[i] = a[i];
            }
            int l=0,s=0;
            for(int i = 0; i < 4; i++){
                l += L[3-i] * pow(10,i);
                s += S[3-i] * pow(10,i);
            }
            n = l - s;
            cnt++;
        }
        if(flg)puts("NA");
        else printf("%d\n",cnt);
    }
	return 0;
}

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

using namespace std;

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

int string_to_int(const string& s){
  int n;
  istringstream iss(s);
  iss >> n;
  return n;
}

bool is_equal(const string& s){
  for(int i=1; i<s.size(); ++i){
    if(s[0]!=s[i]) return false;
  }
  return true;
}

int main(){
  int cnt;
  string s, max_, min_;
  while(1){
    cin >> s;
    if(s=="0000") break;
    if(is_equal(s)){
      cout << "NA" << endl;
      continue;
    }
    for(cnt=0; s!="6174"; ++cnt){
      min_ = s;
      sort(min_.begin(), min_.end());
      max_ = min_;
      reverse(max_.begin(), max_.end());
      s = int_to_string(string_to_int(max_)-string_to_int(min_));
    }
    cout << cnt << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)),UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a+mod-b)%mod
#define MUL(a, b) a = (a * b) % mod
#define repi(i,m,n) for(int i = m;i < n;i++)
#define drep(i,n,m) for(int i = n;i >= m;i--)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) repi(i,1,n+1)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define fi first
#define se second
typedef pair<int,int> P;
typedef pair<int, P> PP;
typedef pair<P, int> Pi;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9+7;
const int INF = 1e18+7;

int to_num(string s){
	return atoll(s.c_str());
}
string to_str(int num){
	string str = "";
	rep(i,4){
		str.pb((num%10)+'0');
		num /= 10;
	}
	reverse(all(str));
	return str;
}

signed main(){
	while(1){
		string str;
		cin >> str;
		if(str == "0000")break;
		if(str[0] == str[1] and str[1] == str[2] and str[2] == str[3])
		{cout << "NA" << endl;continue;}
		
		int ans = 0;
		while(1){
			if(str == "6174")break;
			string s1 = str, s2 = str;
			sort(all(s1), greater<int>());
			sort(all(s2));
			int num = to_num(s1)-to_num(s2);
			str = to_str(num);
			ans++;
		}
		printf("%lld\n", ans);
		
	}
	return 0;
}