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>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
 
 
 
const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
 

 string calc(string s, string t){
 	string ret = s;

 	for(int i = 0; i < 4; i++){
 		ret[i] -= t[i] - '0';
 	}

 	for(int i = 3; i > 0; i--){
 		if(ret[i] < '0') {
 			ret[i] += 10;
 			ret[i - 1]--;
 		}
 	}

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

		bool f = false;
		for(int i = 0; i < 9; i++){
			if(s == string(4, '1' + i)){
				f = true;
				break;
			}
		}
		if(f){
			cout<<"NA"<<endl;
			continue;
		}

		int n = 0;
		while(1){
			if(s == "6174") break;
			n++;
			t = s;
			sort(all(s));
			sort(all(t));
			reverse(all(t));

			s = calc(t, s);
		}

		cout<<n<<endl;
	}
}

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

using namespace std;

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

int main(void)
{
	string n;
	while (cin >> n && n != "0000"){
		if (count(n.begin(), n.end(), n[0]) == 4){
			cout << "NA" << endl;
			continue;
		}
		// assert(n != "0000");

		int tmp = toInt(n);
		char str[128];
		sprintf(str, "%04d", tmp);
		n = string(str);
		int cnt = 0;
		while (n != "6174"){
			if (count(n.begin(), n.end(), n[0]) == 4){
				cout << "NA" << endl;
				goto END;
			}
			string L = n; sort(L.rbegin(), L.rend());
			string S = n; sort(S.begin(), S.end());
			// cout << n << " " << L << " " << S << endl;
			int tmp = toInt(L) - toInt(S);
			char str[128];
			sprintf(str, "%04d", tmp);
			n = string(str);
			cnt++;
		}
		cout << cnt << endl;
		END:;
	}

	return 0;
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int d[4];
int main(){
	int a;
	while(scanf("%d",&a),a){
		if(a%1111==0){
			printf("NA\n");
			continue;
		}
		int count=0;
		while(a!=6174){
			for(int i=0;i<4;i++){
				d[i]=a%10;
				a/=10;
			}
			std::sort(d,d+4);
			int b=0;
			for(int i=0;i<4;i++){
				b*=10;
				b+=d[i];
			}
			int c=0;
			for(int i=3;i>=0;i--){
				c*=10;
				c+=d[i];
			}
			a=c-b;
			count++;
		}
		printf("%d\n",count);
	}
}

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

int main() {
	std::string str;
	while (std::cin >> str) {
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == '0')
				break;
			else
				std::cout << "NA" << std::endl;
		}
		std::string l, s;
		int cnt = 0;
		while(str!="6174") {
			++cnt;
			l = str;
			std::sort(l.begin(), l.end(), std::greater<char>());
			s = str;
			std::sort(s.begin(), s.end());
			str = std::to_string(std::stoi(l) - std::stoi(s));
		}
		std::cout << cnt << std::endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define rep(i,x) for(int i=0;i<(x);++i)
#define rep1(i,x) for(int i=1;i<=(x);++i)
#define rrep(i,x) for(int i=(x)-1;i>=0;--i)
#define rrep1(i,x) for(int i=(x);i>=1;--i)
#define FOR(i,a,x) for(int i=(a);i<(x);++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

#define omajinai ios::sync_with_stdio(false);cin.tie(0)

template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>T get(){T a;cin>>a;return a;}
template<typename T>T rev(T a){reverse(all(a));return a;}
template<typename T>istream&operator>>(istream&is,vector<T>&vec){rep(i,vec.size())is>>vec[i];return is;}
template<typename T>vector<T>&sort(vector<T>&a){sort(all(a));return a;}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

string f(string a)
{
    while (a.size() < 4) a = '0' + a;

    return a;
}

int to_int(string s)
{
    int res = 0;

    int i = 0;
    while (s[i] == '0') i++;

    for (; i < s.size(); ++i) {
        res *= 10;
        res += s[i] - '0';
    }

    return res;
}

string to_str(int a)
{
    string res = "";

    do {
        res += (a % 10) + '0';
        a /= 10;
    } while (a);

    reverse(all(res));

    return f(res);
}

int solve(int now, string N)
{
    if (N == "6174") return now;

    string l = N, s = N;
    sort(rall(l));
    sort(all(s));

    return solve(now + 1, to_str(to_int(l) - to_int(s)));
}

bool check(string a)
{
    char c = a[0];
    bool f = true;

    rep(i, a.size()) {
        if (a[i] != c) return true;
    }

    return false;
}

signed main()
{
    string N;
    
    while (true) {
        cin >> N;

        if (check(N)) {
            cout << solve(0, N) << endl;
        } else {
            if (N[0] == '0') {
                break;
            }

            cout << "NA" << endl;
        }
    }
}

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

int main() {
	int n;
	int str[4];
	bool used[10000];
	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;
		}
		std::memset(used, false, sizeof(used));
		used[n] = true;
		int cnt = 0;
		while(n!=6174) {
			if (str[0] == str[1] == str[2] == str[3]) {
				std::cout << "NA" << std::endl;
				goto a;
			}
			++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;
			if (used[n])
				goto a;
			used[n] = true;
			str[0] = n / 1000;
			str[1] = (n / 100) % 10;
			str[2] = (n / 10) % 10;
			str[3] = n % 10;
		}
		std::cout << cnt << std::endl;
	a:;
	}
	return 0;
}