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

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<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;
				return 0;
			}
			++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 <cstring>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;

string toString(int n){
  ostringstream oss;
  oss << n;
  return oss.str();
}

int toInt(string s){
  istringstream iss(s);
  int ret;
  iss >> ret;
  return ret;
}


int main(){
  string s;
  while(cin >> s){
    if(s == "0000") break;
    if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) cout << "NA" << endl;
    
    int cnt = 0;
    
    while(s != "6174"){
      cnt++;
      string L, S;
      L = s;
      sort(L.begin(), L.end());
      S = L;
      reverse(L.begin(), L.end());
      int a, b;
      a = toInt(L);
      b = toInt(S);
      s = toString(a-b);
    }
    cout << cnt << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <functional>
#include <queue>
#include <set>
#include <map>
#include <numeric>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <sstream>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for(int i = a; i < b; ++i)
#define repll(i, a) REPLL(i, 0, a)
#define REPLL(i, a, b) for(ll i = a; i < b; ++i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;
const double PI = 3.14159265358979323846;
const double esp = 1e-9;
const int infi = (int)1e+9 + 10;
const ll infll = (ll)1e+17 + 10;

std::string n, l, s;
int cnt = 0;
bool used[10000];
bool flg = true;

int main(){
	while (std::cin >> n, n != "0000"){
		rep(i, 10000)used[i] = false;
		cnt = 0;
		flg = true;
		while (n != "6174"){
			l = n;
			s = n;
			std::sort(l.begin(), l.end(), std::greater<char>());
			std::sort(s.begin(), s.end());
			if (l.size() < 4)l += std::string("0", 4 - l.size());
			n = std::to_string(std::stoi(l) - std::stoi(s));
			if (used[std::stoi(n)]){
				flg = false;
				break;
			}
			used[std::stoi(n)] = true;
			++cnt;
		}
		if (flg)std::cout << cnt << std::endl;
		else std::cout << "NA" << std::endl;
	}
	return 0;
}

C++: #include<stdio.h>
int main (){
	int N[4],L,S,c,i,a;
	while(1){
		for(i=0;i!=4;i++){
			scanf("%1d",&N[i]);
		}
		if(N[0]==N[1]&&N[2]==N[3]&&N[1]==N[2]){
			if(N[1]==0)
				break;
			else
				puts("NA");
		}
		else{
			for(i=0;c!=6174;i++){
				for(S=0;S!=4;S++){
					for(a=S;a!=4;a++){
						if(N[S]<N[a]){
							c=N[a];
							N[a]=N[S];
							N[S]=c;
						}
					}
				}
				L=1000*N[0]+100*N[1]+10*N[2]+N[3];
				S=1000*N[3]+100*N[2]+10*N[1]+N[0];
				L-=S;
				c=L;
				N[0]=L/1000;
				L%=1000;
				N[1]=L/100;
				L%=100;
				N[2]=L/10;
				L%=10;
				N[3]=L;
			}
			printf("%d\n",i);
		}
	}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int solve(int num,int& cnt) {
	if (num == 6174)return cnt;
	cnt++;
	int s[5];
	for (int i = 0; i < 4; i++) {
		s[i] = num % 10;
		num -= s[i];
		num /= 10;
	}
	sort(s, s + 4);
	return solve((s[3] - s[0]) * 999 + (s[2] - s[1]) * 90), cnt);
}
int main() {
	int n;
	while (cin >> n) {
		if (!n)return 0;
		else if (n % 1111 == 0) {
			cout << "NA\n";
			continue;
		}
		int cnt;
		solve(n, cnt);
		cout << cnt << endl;
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main(void){
	string S;
	while(cin >> S){
		if(S == "0000")break;
		int cost = 0;
		int sum = StrToInt(S);
		if(1){
			int a = sum/1000;
			if(sum == a*1000+a*100+a*10+a){
				cout << "NA" << endl;
				continue;
			}
		}
		vector<int> v(4);
		vector<int> _v(4);
		while(1){
			if(abs(sum) == 6174){
				cout << cost << endl;
				break;
			}
			string s = IntToStr(sum);
			rep(i, 4){
				v[i] = s[i]-'0';
				_v[i] = -(s[i]-'0');
			}
			sort(v.begin(), v.end());
			sort(_v.begin(), _v.end());
			int _L = 1000*v[0] + 100 * v[1] + 10*v[2] + v[3];
			int _S = -(1000*_v[0] + 100 * _v[1] + 10*_v[2] + _v[3]);
			sum = _S - _L;
			cost++;
		}
	}

	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}

C++: #include<stdio.h>
int main (){
	int N[4],L,S,c,i,a;
	while(1){
		for(i=0;i!=4;i++){
			scanf("%1d",&N[i]);
		}
		if(N[0]==N[1]&&N[2]==N[3]&&N[1]==N[2]){
			if(N[1]==0)
				break;
			else
				puts("NA");
		}
		else{
			for(i=0;c!=6174;i++){
				for(S=0;S!=4;S++){
					for(a=S;a!=4;a++){
						if(N[S]<N[a]){
							c=N[a];
							N[a]=N[S];
							N[S]=c;
						}
					}
				}
				L=1000*N[0]+100*N[1]+10*N[2]+N[3];
				S=1000*N[3]+100*N[2]+10*N[1]+N[0];
				L-=S;
				c=L;
				N[0]=L/1000;
				L%=1000;
				N[1]=L/100;
				L%=100;
				N[2]=L/10;
				L%=10;
				N[3]=L;
			}
			printf("%d\n",i);
		}
	}
}

C++: //ソートを使いたくなかっただけなんだ
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	while (true)
	{
		int n;
		cin >> n;
		stringstream ss;
		ss << setfill('0') << setw(4) << n;
		string N = ss.str();
		/*
		string N;
		cin >> N;
		*/
		if (N == "0000")
		{
			break;
		}
		else if (N[0]==N[1] && N[1]==N[2] && N[2]==N[3])
		{
			cout << "NA" << endl;
			continue;
		}

		int cnt1 = 0;
		while (N != "6174")
		{
			int cnts[10];
			for (int i=0; i<10; i++)
			{
				cnts[i] = 0;
				unsigned int j = 0;
				while ((j=N.find('0'+i, j)) != string::npos)
				{
					++j;
					++cnts[i];
				}
			}
			string L="0000", S="0000";
			int cnt2 = 0;
			for (int i=0; i<10; i++)
			{
				for (int j=0; j<cnts[i]; j++)
				{
					L[3-cnt2] = S[cnt2] = '0'+i;
					++cnt2;
				}
			}

			stringstream ss_L(L), ss_S(S);
			int i_L, i_S;
			ss_L >> i_L;
			ss_S >> i_S;
			stringstream ss_N;
			ss_N << setfill('0') << setw(4) << i_L-i_S;
			N = ss_N.str();
			++cnt1;
		}
		cout << cnt1 << endl;
	}

	return 0;
}