Go: package main

import (
	"fmt"
	"sort"
	"strconv"
)

func main() {
	var v int
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Print("NA")
			continue
		}
		count := 0
		for v != 6174 {
			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))
			v = l - s
			count++
		}
		fmt.Println(count)
	}
}

C++: #include <stdio.h>

void sort4(int *a){
	int min, minID;
	
	for (int i=0; i<4; i++){
		min = a[i], minID = i;
		for (int j=i+1; j<4; j++){
			if (a[j] < min) min = a[minID=j];
		}
		a[minID] = a[i]; a[i] = min;
	}
}

int main(void){
	int n,l,s,cnt,a[4];
	
	while (scanf("%d",&n), n){
		if (n % 1111 == 0){
			puts("NA");
		} else {
			cnt = 0;
			while (n != 6174){
				for (int j=0; j<4; j++){
					a[j] = n%10; n /= 10;
				}
				sort4(a);
				l = a[0]+10*(a[1]+10*(a[2]+10*a[3]));
				s = a[3]+10*(a[2]+10*(a[1]+10*a[0]));
//				printf("l=%d, s=%d, n=%d\n", l, s, l-s);
				n = l-s;
				cnt++;
			}
			printf("%d\n", cnt);
		}
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(v, a, b) for(int v = (a); v < (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define UNWORDS(vec, str) for(int i = 0; i < (vec).size(); ++i) cout << (i ? str : "") << (vec)[i]
#define UNLINES(vec) for(int i = 0; i < (vec).size(); ++i) cout << (vec)[i] << endl
#define RS resize
#define CLR clear
#define PB push_back
using namespace std;

int main(){
	int n, cnt;
	
	int rep[] = {1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999};
	vector< int > repdig(rep, rep + (sizeof(rep) / sizeof(int)));
	
	while(cin >> n, n){
		
		if(find(repdig.begin(), repdig.end(), n) != repdig.end()){
			cout << "NA" << endl;
			continue;
		}
		
		cnt = 0;
		
		while(n != 6174){
			stringstream ss;
			ss << setfill('0') << setw(4) << n;
			string str = ss.str();
			
			sort(str.begin(), str.end());
			
			int s = atoi(str.c_str());
			
			reverse(str.begin(), str.end());
			
			int l = atoi(str.c_str());
			
			n = l - s;
			++cnt;
		}
		cout << cnt << endl;
	}
	
	return 0;
}

C++: #include<iostream>

using namespace std;

bool func_a(int *N, int *count);

int main()
{
	while(true)
	{
		int N = 0;
		int count = 0;
		bool flag = true;


		cin >> N;

		if(N == 0)
		{
			break;
		}

		if(N == 6174)
		{
			cout << 0 << endl;
			flag = false;
		}

		for(int i = 0; i <= 9; i++)
		{
			if(N == i + i * 10 + i * 100 + i * 1000)
			{
				cout << "NA" << endl; 
				flag = false;
			}
		}
	
		if(flag)
		{
			while( func_a(&N, &count) ){};

			cout << count << endl;
		}
	}

	return false;
}

bool func_a(int *N, int *count)
{	
	int num = *N;
	int Nmax[4];
	int Nmin[4];
	int L = 0, S = 0;

	*count += 1;

	for(int i = 0; i < 4; i++)
	{
		Nmax[i] = Nmin[i] = num % 10;
		num /= 10;
	}

	for(int i = 0; i < 4; i++)
	{
		for(int j = 0; j < 4; j++)
		{
			if(Nmax[i] < Nmax[j])
			{
				int tmp = Nmax[i];
				Nmax[i] = Nmax[j];
				Nmax[j] = tmp;
			}
			if(Nmin[i] > Nmin[j])
			{
				int tmp = Nmin[i];
				Nmin[i] = Nmin[j];
				Nmin[j] = tmp;
			}
		}
	}

	int dz[4] = {1, 10, 100, 1000};

	for(int i= 0; i < 4; i++)
	{
		L += Nmax[i] * dz[i];
		S += Nmin[i] * dz[i];
	}

	*N = L - S;

	if(*N == 6174)
	{
		return false;
	}
	else
	{
		return true;
	}
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <array>
#include <queue>
#include <deque>
#include <set>
#include <list>
#include <map>
#include <stack>
#include <utility>
#include <bitset>
#include <complex>
#include <algorithm>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <climits>
#include <random>
#include <functional>
#include <sstream>
#include <iomanip>

using namespace std;

#define rep(i, n) for(int i=0; i<(n); i++)
#define FOR(i, m, n) for(int i=(m);i<(n);i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define SORT(x) sort((x).begin(),(x).end())
#define REVE(x) reverse((x).begin(),(x).end())
#define mp make_pair
#define pb push_back
#define dump(x)  cout << #x << " = " << (x) << endl;
#define forauto(i, a) for(auto i:a)cout << i << " "; puts("");

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<vector<int>> VVI;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef complex<double> CD;

string aaa(string t) {
	string l = t; SORT(l); REVE(l);
	string s = t; SORT(s);
	int ans = stoi(l) - stoi(s);
	string sans = to_string(ans);
	string ret; ret.assign(4 - sz(sans), '0');
	ret += to_string(ans);
	return ret;
}

int main() {
	while (true) {
		string s; cin >> s;
		if (s == "0000")break;

		bool flag = (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]);
		int ans = 0;
		while (s != "6174" && !flag) {
			s = aaa(s);
			ans++;
		}
		if (flag)puts("NA");
		else cout << ans << endl;
	}
}

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

int main(){
  int t[4];
  int tt[4];
  int a,b,bb,c;
  while(true){
    cin>>a;
    c=0;
    if(a==0)break;
    for(int i=1111;i<10000;i+=1111){
      if(i==a){
	cout<<"NA"<<endl;
	c=-1;
      }
    }
    if(c==-1)continue;
    while(a!=6174){
      b=a;
      c++;
      for(int i=3;i>=0;i--){
	t[i]=b%10;
	b/=10;
      }
      sort(t,t+4);
      reverse(t,t+4);
      b=t[0]*1000+t[1]*100+t[2]*10+t[3];

      bb=a;
      for(int i=3;i>=0;i--){
	t[i]=bb%10;
	bb/=10;
      }
      sort(t,t+4);
      bb=t[0]*1000+t[1]*100+t[2]*10+t[3];
      
      a=b-bb;
      cout<<a<<endl;
    } 
    cout<<c<<endl;
    
  }
  return 0;
}

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

int main()
{
	while (true)
	{
		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;
}

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


using namespace std;

int main (void)
{
	int y = 0;
	for (long u = 0; u < 10000; u++)
	{
		int co = 0;
		string a;
		int g[4];

		cin >> a;
		for (int k = 0; k < 100; k++)
		{
			if (a == "6174")
			{
				cout << co << endl;

				break;
			}
			if (a == "0000")
			{
				y = -1;
				break;
			}

			for (int i = 0; i < 4; i++)
			{
				g[i] = 0;
				g[i] = stoi (a.substr(i,1));
			}
			if (g[0] == g[1] && g[1] == g[2])
			{
				if (g[2] == g[3])
				{
					cout << "NA" << endl;
					break;
				}
			}
			long Max = 0,Min = 0;

			sort(g,g + 4);

			Min = (g[0] * 1000) + (g[1] * 100) + (g[2] * 10) + g[3];
			Max = (g[3] * 1000) + (g[2] * 100) + (g[1] * 10) + g[0];

			co += 1;
			if (Max - Min == 6174)
			{
				cout << co << endl;
				break;
			}
			ostringstream p;
			p << Max - Min;
			a = p.str();
		}
		if (y != 0)
		{
			break;
		}
	}
}