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 <string>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

int main()
{
	string S;

	while (true)
	{
		cin >> S;

		if (S == "0000") { break; }

		if (S[0] == S[1] == S[2] == S[3])
		{
			cout << "NA" << endl;
		}
		else
		{
			int count_ = 0;

			while (S != "6174")
			{
				string S1 = S, S2 = S;

				sort(S1.begin(), S1.end(), greater<char>());
				sort(S2.begin(), S2.end());

				S = to_string(stoi(S1) - stoi(S2));

				while (S.size() < 4)
				{
					S = '0' + S;
				}

				count_++;
			}

			cout << count_ << endl;
		}
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
	string N;
	
	while (cin >> N) {
		if (N == "0000") break;
		int ok = 1, ans = 0;
		unordered_set< string > strs;
		
		while (ok) {
			if (N == "6174") break;
			strs.insert(N);
			
			string L = N, S = N;
			sort(L.begin(), L.end(), greater< char >());
			sort(S.begin(), S.end());
			
			stringstream ss;
			ss << L << " " << S;
			int l, s;
			ss >> l >> s;
			
			int n = l - s;
			stringstream sss;
			sss << n;
			N = sss.str();
			
			while (N.size() < 4) N = "0" + N;
			
			++ans;
			
			if (strs.find(N) != strs.end()) ok = 0;
		}
		
		if (ok) cout << ans << endl;
		else cout << "NA" << endl;
	}
}

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

int main(){
    int cnt,number;
    int s,l;
    while(1){
        char num[5];
        cnt=0;
        cin >> num;
        if(num[0] != '0' && num[3] == '\0'){
            number = num[0]-'0';
            for(int i=0;i<3;i++){
                num[i] = 0;
            }
            num[3] = number;
        }
        if(strcmp(num,"0000") == 0) break;
        while(1){
            if(strcmp(num,"6174") == 0) break;
            if(num[0] == num[1] && num[1] == num[2] && num[2] == num[3]){
                cout << "NA" << endl;
                break;
            }
            sort(num,num + 4,greater<char>());
            l = (num[0]-'0')*1000 + (num[1]-'0')*100 + (num[2]-'0')*10 + num[3]-'0';
            sort(num,num + 4);
            s = (num[0]-'0')*1000 + (num[1]-'0')*100 + (num[2]-'0')*10 + num[3]-'0';
            l -= s;
            sprintf(num,"%d",l);
            cnt++;
        }
        if(num[0] != num[1]) cout << cnt << endl;
    }
}

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

#define DEBUG(x) cerr << #x << " = " << x << endl

template<class T, class U> T lexical_cast(const U &from) {
    stringstream ss;
    ss << from;
    T to;
    ss >> to;
    return to;
}

bool same(const string &s) {
    return s[0] == s[1] && s[0] == s[2] && s[0] == s[3];
}

int main() {
    for(string S; cin >> S, S != "0000"; ) {
        if(same(S)) {
            cout << "NA" << endl;
            continue;
        }
        int ans = 0;
        while(S != "6174") {
            sort(begin(S), end(S));
            string L = S;
            reverse(begin(L), end(L));
            S = lexical_cast<string>(lexical_cast<int>(L) - lexical_cast<int>(S));
            ans++;
        }
        cout << ans << endl;
    }
}

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

using namespace std;

int main()
{
	int m, n[4], mi[4], ma[4], na;
	int	max_int = 0, min_int = 0, n_int, ans;
	bool same;
				
	while (1){
		ans = 0;
		same = false;

		scanf("%d", &n_int);
		if (n_int == 0) break;
		if (n_int % 1111 == 0){
		 cout << "NA" << endl;
		 same = true;
		}

		for (int i = 3; i >= 0; i--){
			n[i] = n_int / (int)(pow(10, i)) % 10;
		}

		while (n_int != 6174){
			if (same) break;
			max_int = min_int = 0;

			for (int i = 3; i >= 0; i--){
				n[i] = n_int / (int)(pow(10, i)) % 10;
			}
			sort(n, n + 4);

			for (int i = 0; i < 4; i++){
				mi[3 - i] = n[i];
				ma[i] = n[i];
			}

			for (int i = 0; i < 4; i++){
				max_int += ma[i] * (int)(pow(10, i));
				min_int += mi[i] * (int)(pow(10, i));
			}
			n_int = max_int - min_int;
			
			ans++;
		}

		if (!same) cout << ans << endl;
	}
return (0);
}

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


int to_i(string &s){
	int a=0;
	for(int i=0;i<s.size();i++)a=a*10+s[i]-'0';
	return a;
}
string to_s(int n){
	string s;
	while(n){
		s+=n%10+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}



int cache[10000];

int solve(int n){
	if(n==6174)return 0;
	if(cache[n])return cache[n];
	string s=to_s(n);
	int a,b;
	sort(s.begin(),s.end());
	a=to_i(s);
	sort(s.begin(),s.end(),greater<char>());
	b=to_i(s);
	return cache[n] = 1+solve(b-a);
}


int main(){
	string s;
	while(cin>>s&&s!="0000"){
		if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3]){
			cout<<"NA\n";
			continue;
		}
		//cout<<solve(to_i(s))<<endl;
		cout<<1<<endl;
	}
	return 0;
}

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;
	int ans = 0;
	cin >> s;
	if(s == "0000") break;
	else if(s[0] == s[1] && s[1] == s[2] && s[2] == 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 u = 0;
	    vector<int> c(4);
	    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>

using namespace std;

int main() {
	string n;
	while (cin >> n, n != "0000") {
		if (n[0] == n[1] && n[1] == n[2] && n[2] == n[3]) cout << "NA" << endl;
		else {
			int i;
			for (i = 0; n != "6174"; ++i) {
				sort(n.begin(), n.end());
				int tmp = stoi(n);
				reverse(n.begin(), n.end());
				tmp = stoi(n) - tmp;
				n = to_string(tmp);
				while (n.size() != 4) n = '0' + n;
			}
			cout << i << endl;
		}
	}
	
	return 0;
}