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 <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;
      continue;
    }
    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 <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

int main(void){
	int a,b,c,d;
	while(scanf("%1d%1d%1d%1d",&a,&b,&c,&d)!=EOF,!(a==0&&b==0&&c==0&&d==0)){
		if(a==b&&b==c&&c==d&&d==a){
			cout<<"NA"<<endl;
		}else{
			int count=0;
			while(!(a==6&&b==1&&c==7&&d==4)){
				vector<int> nums,numl;
				nums.push_back(a);
				nums.push_back(b);
				nums.push_back(c);
				nums.push_back(d);
				numl = nums;
				sort(nums.begin(),nums.end());
				sort(numl.begin(),numl.end(),greater<int>());
				int s = nums[0]*1000+nums[1]*100+nums[2]*10+nums[3];
				int l = numl[0]*1000+numl[1]*100+numl[2]*10+numl[3];
				int N = l-s;
				a=N/1000;
				b=(N%1000)/100;
				c=(N%100)/10;
				d=N%10;
				++count;
			}
			cout<<count<<endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
	
int main(){
	while(1){
		int N,cnt;
		cin >>N;

		if(N==0000)
			break;

		if(N%1111==0){
			cout << "NA" <<endl;
			continue;
		}

		while(N!=6174){
			int n[4]={N/1000,N%1000/100,N%100/10,N%10};
			sort(n,n+4);
			int L=n[3]*1000+n[2]*100+n[1]*10+n[0];
			int S=n[0]*1000+n[1]*100+n[2]*10+n[3];
	
			N=L-S;
			cnt++;
		}
	
		cout <<cnt;

	}
	return 0;
}

C++: #include<iostream>

using namespace std;

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

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

	cin >> N;
	
	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++: #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
	char num[6]={0};
	
	while(1){
		int cnt = 0;
		scanf("%s",num);
		if(num[0] == num[1] && num[1] == num[2] && num[2] == num[3]){
			if(num[0] == 48)break;
			else puts("NA");
			continue;
		}
		if(num[0] == '6' && num[1] == '1' && num[2] == '7' && num[3] == '4'){
			puts("0");
			continue;
		}
		while(1){
			sort(num,num+4);
			int s = atoi(num);
			reverse(num,num+4);
			int l = atoi(num);
			int x = 0;
			if(l - s == 6174){
				printf("%d\n",cnt+1);
				break;
			}
			else{
				x = l - s;
				cnt++;
				for(int i = 0; i < 4; i++){
					num[i] = (x / pow(10,3-i))+48;
					x -= (num[i]-48) * pow(10,3-i);
				}
			}
		}
		
	}
	return 0;
}

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

string operator-(const string &a, const string &b) {
	return to_string(stoi(a) - stoi(b));
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	const string goal = "6174";

	for(string n; cin >> n && n != "0000";) {
		if(all_of(n.begin(), n.end(), [&](char c) { return c == n.front(); })) {
			cout << "NA" << endl;
			continue;
		}


		int cnt = 0;
		while(n != goal) {
			string L(n), S(n);
			sort(L.rbegin(), L.rend());
			sort(S.begin(), S.end());
			n = L - S;
			++cnt;
		}

		cout << cnt << endl;
	}

	return EXIT_SUCCESS;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

int main() {
	string s;
	while(cin >> s, s!= "0000") {
		int tmp = toInt(s);
		int count = 1;
		if(tmp == 6174) {
			cout << 0 << endl;
			goto next;
		} else if(tmp%1111 == 0) {
			cout << "NA" << endl;
			goto next;
		}
		
		while(true) {	
			sort(ALL(s));
			int a = toInt(s);
			sort(RALL(s));
			int b = toInt(s);
			int c = b - a;
			if(c == 6174) {
				break;
			} else {
				s = toStr(c);
				count++;
			}
		}
		cout << count << endl;

		next:;
	}

	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;
	ll ans = 0;
	cin >> s;
	if(s == "0000") break;
	else if(s[0] == s[1] && s[0] == s[2] && s[0] == s[3]){
	    cout << "NA" << endl;
	    continue;
	}
	while(s.compare("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(RALL(a));
	    sort(ALL(b));
	    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];
	    int dif = an - bn;
	    if(dif / 1000 >= 1)
		s = to_string(dif);
	    else if(dif / 100 >= 1)
		s = "0" + to_string(dif);
	    else if(dif / 10 >= 1)
		s = "00" + to_string(dif);
	    else s = "000" + to_string(dif);
	    ans++;
	}
	cout << ans << endl;
    }
    return 0;
}