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<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

int main(){
	for(char s[5];scanf("%s",s),strcmp(s,"0000")!=0;){
		if(count(s,s+4,s[0])==4){
			puts("NA");
			continue;
		}

		int ans=0;
		while(strcmp(s,"6174")!=0){
			char s1[5]; strcpy(s1,s); sort(s1,s1+4);
			char s2[5]; strcpy(s2,s); sort(s2,s2+4,greater<char>());
			int a1=atoi(s1);
			int a2=atoi(s2);
			sprintf(s,"%04d",a2-a1);
			ans++;
		}
		printf("%d\n",ans);
	}

	return 0;
}

C++: #include<iostream>
#include<string>
#include<map>
#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 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;
		
	}
	return 0;
}

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;
}