Go: package main

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

var memo map[int]int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if _, ok := memo[v]; !ok {
		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))
		memo[v] = solve(l-s) + 1
	}
	return memo[v]
}

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

C++: #include<iostream>

using namespace std;

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

int main()
{
	while(true)
	{
		int N = 0;
		int count = 0;
		bool flag = true;


		cin >> N;

		if(N == 0)
		{
			break;
		}

		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++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <array>
#include <queue>
#include <deque>
#include <set>
#include <list>
#include <map>
#include <stack>
#include <utility>
#include <bitset>
#include <complex>
#include <algorithm>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <climits>
#include <random>
#include <functional>
#include <sstream>
#include <iomanip>

using namespace std;

#define rep(i, n) for(int i=0; i<(n); i++)
#define FOR(i, m, n) for(int i=(m);i<(n);i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define SORT(x) sort((x).begin(),(x).end())
#define REVE(x) reverse((x).begin(),(x).end())
#define mp make_pair
#define pb push_back
#define dump(x)  cout << #x << " = " << (x) << endl;
#define forauto(i, a) for(auto i:a)cout << i << " "; puts("");

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<vector<int>> VVI;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef complex<double> CD;

string aaa(string t) {
	string l = t; SORT(l); REVE(l);
	string s = t; SORT(s);
	int ans = stoi(l) - stoi(s);
	string sans = to_string(ans);
	string ret; ret.assign(4 - sz(sans), '0');
	ret += to_string(ans);
	return ret;
}

int main() {
	while (true) {
		string s; cin >> s;
		if (s == "0000")break;

		bool flag = (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]);
		int ans = 0;
		while (s != "6174" && !flag) {
			s = aaa(s);
			ans++;
		}
		if (flag)puts("NA");
		else cout << ans << endl;
	}
}

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

int main(){
  int t[4];
  int tt[4];
  int a,b,bb,c;
  while(true){
    cin>>a;
    c=0;
    if(a==0)break;
    for(int i=1111;i<10000;i+=1111){
      if(i==a){
	cout<<"NA"<<endl;
	c=-1;
      }
    }
    if(c==-1)continue;
    while(a!=6174){
      b=a;
      c++;
      for(int i=3;i>=0;i--){
	t[i]=b%10;
	b/=10;
      }
      sort(t,t+4);
      reverse(t,t+4);
      b=t[0]*1000+t[1]*100+t[2]*10+t[3];

      bb=a;
      for(int i=3;i>=0;i--){
	t[i]=bb%10;
	bb/=10;
      }
      sort(t,t+4);
      bb=t[0]*1000+t[1]*100+t[2]*10+t[3];
      
      a=b-bb;
      cout<<a<<endl;
    } 
    cout<<c<<endl;
    
  }
  return 0;
}

C++: //ソートを使いたくなかっただけなんだ
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;

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

		int cnt1 = 0;
		while (N != "6174")
		{
			int cnts[10];
			for (int i=0; i<10; i++)
			{
				cnts[i] = 0;
				unsigned int j = 0;
				while ((j=N.find('0'+i, j)) != string::npos)
				{
					++j;
					++cnts[i];
				}
			}
			string L="0000", S="0000";
			int cnt2 = 0;
			for (int i=0; i<10; i++)
			{
				for (int j=0; j<cnts[i]; j++)
				{
					L[3-cnt2] = S[cnt2] = '0'+i;
					++cnt2;
				}
			}

			stringstream ss_L(L), ss_S(S);
			int i_L, i_S;
			ss_L >> i_L;
			ss_S >> i_S;
			stringstream ss_N;
			ss_N << setfill('0') << setw(4) << i_L-i_S;
			N = ss_N.str();
			++cnt1;
		}
		cout << cnt1 << endl;
	}

	return 0;
}

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


using namespace std;

int main (void)
{
	int y = 0;
	for (long u = 0; u < 10000; u++)
	{
		int co = 0;
		string a;
		int g[4];

		cin >> a;
		for (int k = 0; k < 100; k++)
		{
			if (a == "6174")
			{
				cout << co << endl;

				break;
			}
			if (a == "0000")
			{
				y = -1;
				break;
			}

			for (int i = 0; i < 4; i++)
			{
				g[i] = 0;
				g[i] = stoi (a.substr(i,1));
			}
			if (g[0] == g[1] && g[1] == g[2])
			{
				if (g[2] == g[3])
				{
					cout << "NA" << endl;
					break;
				}
			}
			long Max = 0,Min = 0;

			sort(g,g + 4);

			Min = (g[0] * 1000) + (g[1] * 100) + (g[2] * 10) + g[3];
			Max = (g[3] * 1000) + (g[2] * 100) + (g[1] * 10) + g[0];

			co += 1;
			if (Max - Min == 6174)
			{
				cout << co << endl;
				break;
			}
			ostringstream p;
			p << Max - Min;
			a = p.str();
		}
		if (y != 0)
		{
			break;
		}
	}
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int main(){
    while(true){
	string s;
	ll ans = 0;
	cin >> s;
	if(s == "0000") break;
	else if(s[0] == s[1] && s[0] == s[2] && s[0] == s[3]){
	    cout << "NA" << endl;
	    continue;
	}
	while(s != "6174"){
	    vector<int> a(4);
	    vector<int> b(4);
	    a[0] = (int)(s[0] - '0');
	    a[1] = (int)(s[1] - '0');
	    a[2] = (int)(s[2] - '0');
	    a[3] = (int)(s[3] - '0');
	    b[0] = (int)(s[0] - '0');
	    b[1] = (int)(s[1] - '0');
	    b[2] = (int)(s[2] - '0');
	    b[3] = (int)(s[3] - '0');
	    sort(ALL(b));
	    sort(RALL(a));
	    int an = 1000 * a[0] + 100 * a[1] + 10 * a[2] + a[3];
	    int bn = 1000 * b[0] + 100 * b[1] + 10 * b[2] + b[3];
	    s = to_string(an - bn);
	    ans++;
	}
	cout << ans << endl;
    }
    return 0;
}