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 <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];
	    int dif = an - bn;
	    if(dif / 1000 >= 1)
		s = to_string(dif);
	    else if(dif / 100 >= 1)
		s = "0" + to_string(dif);
	    else if(dif / 10 >= 1)
		s = "00" + to_string(dif);
	    else s = "000" + to_string(dif);
	    ans++;
	}
	cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<cstdlib>

int main() {
	int n;
	int str[4];
	while (std::cin >> n) {
		int str[4] = { (n / 1000),(n / 100) % 10,(n / 10) % 10,n % 10 };
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == 0)
				break;
			else
				std::cout << "NA" << std::endl;
		}
		int cnt = 0;
		while(n!=6174) {
			if (str[0] == str[1] == str[2] == str[3])
				std::cout << "NA" << std::endl;
			++cnt;
			std::sort(str, str + 4, std::greater<int>());
			int l = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
			std::sort(str, str + 4);
			int	s = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
			n = l - s;
			str[0] = n / 1000;
			str[1] = (n / 100) % 10;
			str[2] = (n / 10) % 10;
			str[3] = n % 10;
		}
		std::cout << cnt << std::endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<sstream>
#include<string>
#include<iomanip>
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<<setw(4)<<setfill('0')<<n;
            string str;
            ss>>str;
            n=cnv(str,true)-cnv(str,false);
            cnt++;
            if(left==n){
                flag=true;
                break;
            }
            left=n;
            cout<<cnt<<":"<<n<<endl;
        }
        if(flag)cout<<"NA"<<endl;
        else cout<<cnt<<endl;
    }
    return 0;
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
bool NA(string s) {
	if (s == "1111" || s == "2222" || s == "3333" || s == "4444" || s == "5555" || s == "6666" || s == "7777" || s == "8888" || s == "9999")return true;
	else return false;
}
signed main() {
	string s;
	while (cin >> s, s != "0000") {
		int ans(0);
		while (s != "6174") {
			if (NA(s)) {
				cout << "NA" << endl;
				goto here;
			}
			string big(s), small(s);
			sort(all(small));
			sort(all(big), greater<char>());
			s = to_string(stoi(big) - stoi(small));
			while (s.size() < 4)s = "0" + s;
			ans++;
		}
		cout << ans << endl;
	here:;

	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int n,i;

int solve(int num){

	if(num == 6174){
		return i;
	}

	i++;

	int k[5];
	k[0] = num/1000;
	k[1] = (num%1000)/100;
	k[2] = (num%100)/10;
	k[3] = num%10;

	sort(k,k+4);
	int s = k[0]*1000+k[1]*100+k[2]*10+k[3];

	reverse(k,k+4);
	int l = k[0]*1000+k[1]*100+k[2]*10+k[3];

	solve(l-s);

}



int main (){

	for(int d=0; d<10000; d++){
	
		i=0;

		cin >> n;
		if(n*1 == 0)break;

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

		else{
			cout << solve(n) << endl;
	
		}
	}
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
template<typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& a){return is >> a.first >> a.second; }
template<typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1, T2>& a){ return os << a.first << " "<<a.second; }
template<typename T> istream& operator>>(istream& is, vector< T >& vc){ for(int i = 0; i < vc.size(); i++) is >> vc[i]; return is; }
template<typename T> ostream& operator<<(ostream& os, vector< T >& vc){ for(int i = 0; i < vc.size(); i++) os << vc[i] << endl; return os; }

main()
{
  string N;
  while(cin >> N, N != "0000") {
    if(count(N.begin(), N.end(), N[0]) == 4) {
      cout << "NA" << endl;
    } else {
      int ret = 0;
      for( ; N != "6174"; ret++) {
        string Sort1 = N, Sort2 = N;
        sort(Sort1.rbegin(), Sort1.rend());
        sort(Sort2.begin(), Sort2.end());
        int Value = atoi(Sort1.c_str()) - atoi(Sort2.c_str());
        stringstream sss; sss << Value;
        N = sss.str();
        while(N.size() < 4) N = "0" + N;
      }
      cout << ret << endl;
    }
  }
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <complex>
#include <ctime>
#include <cstdlib>

using namespace std;

inline int to_int(string s) {int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string to_str(T x) {ostringstream sout; sout << x; return sout.str();}

typedef long long ll;

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

		int res = 0;
		if(s == "6174")
		{
			cout << res << endl;
			continue;
		}
		for(int i = 1; ;i++)
		{
			string UP = s, LOW = s;
			sort(LOW.begin(), LOW.end());
			sort(UP.begin(), UP.end(), greater<char>());

			int up = to_int(UP), low = to_int(LOW);
			int mid = up - low;

			if(mid == 0)
			{
				res = -1;
				break;
			}
			if(mid == 6174)
			{
				res = i;
				break;
			}
			s = to_str(mid);
			while(s.size() < 4)
			{
				s += '0';
			}
		}

		if(res == -1)
		{
			cout << "NA" << endl;
			continue;
		}
		cout << res << endl;
	}

}