Go: package main

import (
	"fmt"
	"sort"
)

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]
	return solve(l-s) + 1
}

func main() {
	var v int
	memo = make([]int, 10000)
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

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

using namespace std;

// http://d.hatena.ne.jp/pogin/20121001/1349069143
inline 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() {
	while (true) {
		string s;
		cin >> s;
		if (s == "0000") {
			break;
		}
		if (toInt(s) % 1111 == 0) {
			cout << "NA" << endl;
			continue;
		}
		
		int ans = 0;
		while (s != "6174") {
			ans++;
			sort(s.begin(), s.end());
			string min = s;
			string max;
			max = min;
			reverse(max.begin(), max.end());
			//cout << "max: " << max << " min: " << min << endl;
			int next = toInt(max) - toInt(min);
			//cout << "next: " << next << endl;
			string next_string = toString<int>(next);
			int next_length = next_string.length();
			if (next_length <= 3) {
				for (int i = next_length; i < 4; i++) {
					next_string.append("0");
				}
			}
			s = next_string;
		}
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <sstream>
#include <numeric>
#include <cstdio>
#include <cassert>
using namespace std;

string to_str(int n) { stringstream ss; ss << n; return ss.str(); }
bool same_all(string s) { return count(s.begin(), s.end(), s[0]) == s.size(); }
int to_int(string s) { return atoi(s.data()); }

int main()
{
	for(string line; cin >> line && (line != "0000"); ) {
	  int times = 0;

		if(same_all(line)) {
			cout << "NA" << endl;
			continue;
		}
		
		for(; line != "6174"; times++) {
			sort(line.begin(), line.end());
			int small = to_int(line);
		
			reverse(line.begin(), line.end());
			int big = to_int(line);

			int next = big - small;

			assert(next >= 0);

			line = to_str(next);
		}

		cout << times << endl;
	}
}

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

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

		int tmp = toInt(n);
		char str[5];
		sprintf(str, "%04d", tmp);
		n = string(str);
		int cnt = 0;
		while (n != "6174"){
			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[5];
			sprintf(str, "%04d", tmp);
			n = string(str);
			cnt++;
		}
		cout << cnt << endl;
	}

	return 0;
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int make(vector<int> num)
{
	int s;
	s = 0;
	for (int i = 0; i < 4; i++){
		s = s * 10 + num[i];
	}
	
	return (s);
}

int main(void)
{
	vector<int> num(4);
	int n;
	int t;
	int ct;
	
	while (scanf("%d", &n) && n){
		
		if (n % 1111 == 0){
			printf("NA\n");
			continue;
		}
		
		ct = 0;
		while (n != 6174){
			ct++;
			t = n;
			for (int i = 0; i < 4; i++){
				num[i] = t % 10;
				t /= 10;
			}
			sort(num.rbegin(), num.rend());
			int L = make(num);
			sort(num.begin(), num.end());
			int S = make(num);
			n = L - S;
		}
		
		printf("%d\n", ct);
	}
	
	return (0);
}

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

int main(){
	int n;
	while(scanf("%d", &n), n){
		int cnt = 0;
		if(n == 6174){
			printf("0\n");
			continue;
		}
		if(n%1111 == 0){
			printf("NA\n");
			continue;
		}
		while(true){
			cnt++;
			int a[4];
			int l = 0, s = 0;
			for(int i = 0; i < 4; i++) a[i] = (n%(int)pow(10,4-i))/(int)pow(10,3-i);
			sort(a,a+4);
			for(int i = 0; i < 4; i++){
				s += a[3-i]*(int)pow(10,i);
				l += a[i]*(int)pow(10,i);
			}
			n = l-s;
			if(n == 6174){
				printf("%d\n", cnt);
				break;
			}
		}
	}
}

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

int main(){
	char input[4];
	char array[4];
	int n, l, s;
	int count;

	while(cin >> input, atoi(input)){
		count = 0;
		n = atoi(input);
		if(atoi(input) % 1111 == 0){
			cout << "NA" << endl;
		}
		else {
			while(n != 6174){
				sprintf(input, "%d", n);
				sort(input, input+4);
				s = atoi(input);
				sort(input, input+4, greater<int>());
				l = atoi(input);
				n = l-s;
				count++;
			}
			cout << count << endl;
		}
	}
}

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

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

	while (std::cin >> n) {
		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 <bits/stdc++.h>
using namespace std;

int main() {
	string N;
	
	while (cin >> N) {
		if (N == "0000") break;
		int ok = 1, ans = 0;
		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.count(N)) ok = 0;
		}
		
		if (ok) cout << ans << endl;
		else cout << "NA" << endl;
	}
}

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

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  string n;
  while (cin >> n, n != "0000") {
    int ans = 0;
    bool na = false;
    while (n != "6174") {
      sort(n.begin(), n.end());
      int s = stoi(n);
      sort(n.begin(), n.end(), greater<char>());
      int L = stoi(n);
      if (s == L) {
        cout << "NA\n";
        na = true;
        break;
      }
      ans++;
      ostringstream os;
      os << setfill('0') << setw(4) << L - s;
      n = os.str();
    }
    if (!na) cout << ans << "\n";
  }
}

C++: #include<stdio.h>
int main(void)
{
	int n;
	int l,s;
	int i,j,temp;
	int keta[4];
	int cnt;
	
	scanf("%d",&n);
	
	while(n!=0000){
		if(n%1111==0){
			printf("NA\n");
		}
		while(n!=6174){
			for(i=0;i<4;i++){
				keta[i]=n%10;
				n=n/10;
			}
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(keta[i]<keta[j]){
						temp=keta[i];
						keta[i]=keta[j];
						keta[j]=temp;
					}
				}
			}
			l=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];

			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(keta[i]>keta[j]){
						temp=keta[i];
						keta[i]=keta[j];
						keta[j]=temp;
					}
				}
			}
			s=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];
			n=l-s;
			cnt++;
		}
	printf("%d\n",cnt);
	scanf("%d",&n);
	}

	return 0;
}