Go: package main

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

var memo []int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if memo[v] == 0 {
		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([]int, 10000)
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <functional>
#include <queue>
#include <set>
#include <map>
#include <numeric>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <sstream>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for(int i = a; i < b; ++i)
#define repll(i, a) REPLL(i, 0, a)
#define REPLL(i, a, b) for(ll i = a; i < b; ++i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;
const double PI = 3.14159265358979323846;
const double esp = 1e-9;
const int infi = (int)1e+9 + 10;
const ll infll = (ll)1e+17 + 10;

std::string n, l, s;
int cnt = 0;
bool used[10000];
bool flg = true;

int main(){
	while (std::cin >> n, n != "0000"){
		rep(i, 10000)used[i] = false;
		cnt = 0;
		flg = true;
		while (n != "6174"){
			l = n;
			s = n;
			std::sort(l.begin(), l.end(), std::greater<char>());
			std::sort(s.begin(), s.end());
			if (l.size() < 4)l += std::string("0", 4 - l.size());
			n = std::to_string(std::stoi(l) - std::stoi(s));
			if (used[std::stoi(n)]){
				flg = false;
				break;
			}
			used[std::stoi(n)] = true;
			++cnt;
		}
		if (flg)std::cout << cnt << std::endl;
		else std::cout << "NA" << std::endl;
	}
	return 0;
}

C++: #include<stdio.h>
int main (){
	int N[4],L,S,c,i,a;
	while(1){
		for(i=0;i!=4;i++){
			scanf("%1d",&N[i]);
		}
		if(N[0]==N[1]&&N[2]==N[3]&&N[1]==N[2]){
			if(N[1]==0)
				break;
			else
				puts("NA");
		}
		else{
			for(i=0;c!=6174;i++){
				for(S=0;S!=4;S++){
					for(a=S;a!=4;a++){
						if(N[S]<N[a]){
							c=N[a];
							N[a]=N[S];
							N[S]=c;
						}
					}
				}
				L=1000*N[0]+100*N[1]+10*N[2]+N[3];
				S=1000*N[3]+100*N[2]+10*N[1]+N[0];
				L-=S;
				c=L;
				N[0]=L/1000;
				L%=1000;
				N[1]=L/100;
				L%=100;
				N[2]=L/10;
				L%=10;
				N[3]=L;
			}
			printf("%d\n",i);
		}
	}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int solve(int num,int& cnt) {
	if (num == 6174)return cnt;
	cnt++;
	int s[5];
	for (int i = 0; i < 4; i++) {
		s[i] = num % 10;
		num -= s[i];
		num /= 10;
	}
	sort(s, s + 4);
	return solve((s[3] - s[0]) * 999 + (s[2] - s[1]) * 90), cnt);
}
int main() {
	int n;
	while (cin >> n) {
		if (!n)return 0;
		else if (n % 1111 == 0) {
			cout << "NA\n";
			continue;
		}
		int cnt;
		solve(n, cnt);
		cout << cnt << endl;
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main(void){
	string S;
	while(cin >> S){
		if(S == "0000")break;
		int cost = 0;
		int sum = StrToInt(S);
		if(1){
			int a = sum/1000;
			if(sum == a*1000+a*100+a*10+a){
				cout << "NA" << endl;
				continue;
			}
		}
		vector<int> v(4);
		vector<int> _v(4);
		while(1){
			if(abs(sum) == 6174){
				cout << cost << endl;
				break;
			}
			string s = IntToStr(sum);
			rep(i, 4){
				v[i] = s[i]-'0';
				_v[i] = -(s[i]-'0');
			}
			sort(v.begin(), v.end());
			sort(_v.begin(), _v.end());
			int _L = 1000*v[0] + 100 * v[1] + 10*v[2] + v[3];
			int _S = -(1000*_v[0] + 100 * _v[1] + 10*_v[2] + _v[3]);
			sum = _S - _L;
			cost++;
		}
	}

	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}

C++: #include<stdio.h>
int main (){
	int N[4],L,S,c,i,a;
	while(1){
		for(i=0;i!=4;i++){
			scanf("%1d",&N[i]);
		}
		if(N[0]==N[1]&&N[2]==N[3]&&N[1]==N[2]){
			if(N[1]==0)
				break;
			else
				puts("NA");
		}
		else{
			for(i=0;c!=6174;i++){
				for(S=0;S!=4;S++){
					for(a=S;a!=4;a++){
						if(N[S]<N[a]){
							c=N[a];
							N[a]=N[S];
							N[S]=c;
						}
					}
				}
				L=1000*N[0]+100*N[1]+10*N[2]+N[3];
				S=1000*N[3]+100*N[2]+10*N[1]+N[0];
				L-=S;
				c=L;
				N[0]=L/1000;
				L%=1000;
				N[1]=L/100;
				L%=100;
				N[2]=L/10;
				L%=10;
				N[3]=L;
			}
			printf("%d\n",i);
		}
	}
}

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

int main()
{
	while (true)
	{
		int n;
		cin >> n;
		stringstream ss;
		ss << setfill('0') << setw(4) << n;
		string N = ss.str();
		/*
		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<vector>
#include<algorithm>
#include<sstream>
#include<string>
using namespace std;
int cnv(string str,bool gFlag){
    sort(str.begin(),str.end());
    if(gFlag)reverse(str.begin(),str.end());
    stringstream ss(str);
    int n;
    ss>>n;
    return n;
}
int main(){
    int n;
    while(cin>>n,n){
        int cnt=0,left=0;
        bool flag=false;
        while(n!=6174){
            stringstream ss;
            ss<<n;
            string str;
            ss>>str;
            n=cnv(str,true)-cnv(str,false);
            cnt++;
            if(left==n){
                flag=true;
                break;
            }
            left=n;
        }
        if(flag)cout<<"NA"<<endl;
        else cout<<cnt<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstdlib>
#define rep(i,a) for( int i = 0; i != (a); ++i )

std::string intToStr( int n )
{
	char str[5];
	sprintf( str, "%04d", n );

	return str;
}

int main()
{
	std::string L, S;
	while( std::cin >> L, L != "0000" )
	{
		bool fl = true;

		rep( i, 3 )
			fl &= L[i+1] == L[i];
		if( fl )
		{
			std::cout << "NA" << std::endl;
			continue;
		}

		int ans = 0;
		while( L != "6174" )
		{
			S = L;
			std::sort( L.rbegin(), L.rend() );
			std::sort( S.begin(), S.end() );

			++ans;

			L = intToStr( atoi( L.c_str() ) - atoi( S.c_str() ) );
		}

		std::cout << ans << std::endl;
	}

	return 0;
}