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 <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){
		int cost = 0;
		string str = S;
		if(S == "6174"){
			cout << cost << endl;
			continue;
		}
		if(S == "0000")break;
		if(StrToInt(S) % 1111 == 0){
			cout << "NA" << endl;
			continue;
		}
		while(1){
			cost++;
			vector<int> v;
			rep(i,4){
				int a = str[i]-'0';
				v.push_back(a);
			}
			sort(v.begin(), v.end(), greater<int>());
			int L = v[0] * 1000 + v[1] * 100 + v[2] * 10 + v[3];
			sort(v.begin(), v.end());
			int S = v[0] * 1000 + v[1] * 100 + v[2] * 10 + v[3];
			int sum = L - S;
			//cout << "L:" << L << " S:" << S << endl;
			//cout << "cost:" << cost << " " << sum << endl;
			if(sum == 6174){
				cout << cost << endl;
				break;
			}
			str = IntToStr(sum);
			if(sum < 1000)str = '0'+str;
			if(sum < 100)str = '0'+str;

		}


	}
	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 <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.compare("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(RALL(a));
	    sort(ALL(b));
	    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;
}

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());
			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<iostream>
#include<algorithm>
#include<sstream>
#include<string>
#include<cstdlib>
#define rep(i,a) for( int i = 0; i != (a); ++i )

std::string intToStr( int n )
{
	std::ostringstream oss;
	oss << n;

	return oss.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;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int N;
int ans = 0;
int main()
{
	while (true){
		cin >> N;
		if (N == 0)break;
		if (N % 1111 == 0)printf("NA\n");
		else{
			ans = 0;
			while (N != 6174){
				int A[4];
				A[0] = N / 1000;
				A[1] = (N / 100) % 10;
				A[2] = (N / 10) % 10;
				A[3] = N % 10;
				sort(A, A + 4);
				reverse(A, A + 4);
				N = 999 * (A[0] - A[3]) + 90 * (A[1] - A[2]);
				ans++;
				
			}
			
			printf("%d\n", ans);
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int f(int p) {
	int a[10];
	a[0] = (p - p % 1000) / 1000;
	p -= a[0] * 1000;
	a[1] = (p - p % 100) / 100;
	p -= a[1] * 100;
	a[2] = (p - p % 10) / 10;
	p -= a[2] * 10;
	a[3] = p;
	sort(a, a + 4);
	return a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0] - a[0] * 1000 - a[1] * 100 - a[2] * 10 - a[3];
}
int main() {
	int n; while (cin >> n) {
		if (n == 0)return 0;
		else if (n % 1111 == 0) { cout << "NA\n"; continue; }
		else {
			for (int i = 0; i <= 6174; i++) {
				if (n == 6174) {
					cout << i << endl;
					i = 10000;
				}
				else n = f(n);
			}
		}
	}

}

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

int main() {
	int n;
	int a[4] = {};

	while (std::cin >> n) {
		if (n == 0000)
			break;

		if (n % 1111 == 0) {
			std::cout << "NA" << std::endl;
			continue;
		}

		int count = 0;
		while (n != 6174) {
			a[0] = n / 1000, n %= 1000;
			a[1] = n / 100, n %= 100;
			a[2] = n / 10, n %= 10;
			a[3] = n;

			std::sort(a, a + 4);

			n = 0;
			n += (a[3] - a[0]) * 1000;
			n += (a[2] - a[1]) * 100;
			n += (a[1] - a[2]) * 10;
			n += a[0] - a[3];

			++count;
		}

		std::cout << count << std::endl;
	}
	return 0;
}

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

int main(void) {
	int S, L;
	string N;
	int ans;
	bool flag = false;

	while(1) {
		cin >> N;
		if(N[0] == N[1] && N[1] == N[2] && N[2] == N[3]) {
			flag = true;
			if(N[0] == '0')
				break;
		}

		if(!flag) {
			for(ans = 0; atoi(N.c_str()) != 6174; ans++) {
				sort(N.begin(), N.end());
				S = atoi(N.c_str());
				sort(N.begin(), N.end(), greater<int>());
				L = atoi(N.c_str());
				int tmp = L - S;

				for(int i = 1000, j = 0; i; i /= 10, j++) {
					N[j] = tmp / i + '0';
					tmp %= i;
				}
			}
		}

		if(flag)
			cout << "NA" << endl;
		else
			cout << ans << endl;
	}

	return 0;
}