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 <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <typeinfo>
#define PI 3.14159265359
#define INF 99999999;
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(n) rep(i, n)
#define EPS 1e-10
typedef long long ll;
using namespace std;



/*
class Target
{
public:
	vector <string> draw(int n)
	{

	}
};
*/



int main()
{
	for (char s[5]; scanf("%s", s), strcmp(s, "0000")!=0;)
	{
		if (count(s, s + 4, s[0]) == 4)
		{
			cout << "NA" << endl;
			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++;
		}
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int d[4];
int main(){
    int a;
    while(scanf("%d",&a),a){
        if(a%1111==0){
            printf("NA\n");
            continue;
        }
        int count=0;
        while(a!=6174){
            for(int i=0;i<4;i++){
                d[i]=a%10;
                a/=10;
            }
            std::sort(d,d+4);
            int b=0;
            for(int i=0;i<4;i++){
                b*=10;
                b+=d[i];
            }
            int c=0;
            for(int i=3;i>=0;i--){
                c*=10;
                c+=d[i];
            }
            a=c-b;
            count++;
        }
        printf("%d\n",count);
    }
}

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

int main(){
  string s;

  while(cin >> s, s != "0000"){
    if(s[0] == s[1] && s[0] == s[2] && s[0] == s[3]){
      cout << "NA" << endl;
      continue;
    }

    int ans = 0;

    while(s != "6174"){
      sort(s.begin(), s.end());
      string S = s;
      sort(s.rbegin(), s.rend());
      string L = s;
      int N = atoi(L.c_str()) - atoi(S.c_str());

      char buff[10];
      sprintf(buff, "%04d", N);
      buff[4] = 0;
      s = string(buff);

      ans++;
    }

    cout << ans << endl;
  }
}

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

int main(){
    vector<int> n;
    int input;
    while(cin >> input){
        int ans = 0;
        if(input == 0) break;
        if(input % 1111 == 0){
            cout << "NA" << endl;
            continue;
        }
        while(input != 6174) {
            for(int i=0; i < 4; i++){
                n.push_back(input%10);
                input /= 10;
            }
            vector<int> minNum ,maxNum;

            sort(n.begin(), n.end());
            minNum = n;
            reverse(n.begin(), n.end());
            maxNum = n;

            int L=0, S=0;
            for(int i=0, m = 1000; i < 4; i++, m/=10){
                L += maxNum[i]*m;
                S += minNum[i]*m;
            }
            input = L-S;
            ans++;

            n.clear();
        }
        cout << ans << endl;
    }
}

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

bool isSameNumber(vector<int> n){
    bool isSame = true;
    for(int i=0; i < n.size(); i++){
        for(int j=0; j < n.size(); j++){
            if(n[i] != n[j])isSame = false;
        }
    }
    return isSame;
}

int main(){
    vector<int> n;
    int input;
    while(cin >> input){
        int ans = 0;
        bool isok = true;
        if(log10(input) < 3) n.push_back(0);
        if(input == 0) break;
        while(input != 6174) {
            while(input != 0){
                n.push_back(input%10);
                input /= 10;
            }
            if(isSameNumber(n)){
                cout << "NA" << endl;
                n.clear();
                isok = false;
                break;
            }
            vector<int> minNum ,maxNum;

            sort(n.begin(), n.end());
            minNum = n;
            reverse(n.begin(), n.end());
            maxNum = n;

            int p=0, q=0;
            for(int i=0, m = 1000; i < 4; i++, m/=10){
                p += minNum[i]*m;
                q += maxNum[i]*m;
            }
            input = q-p;
            ans++;

            n.clear();
        }
        if(isok)cout << ans << endl;
    }
}

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

string convert( string trg ){
    sort(trg.begin(), trg.end());
    int a = 0, b = 0, mul = 1;
    for(string::iterator it = trg.begin(); it != trg.end(); it++){
        int n = (*it)-'0';
        a += n * mul;
        b += n * 1000 / mul;
        mul *= 10;
    }
    a = a-b;
    char tmp[8] = {a/1000+'0', a/100%10+'0', a/10%10+'0', a%10+'0'};
    string res(tmp);
    return res;
}

int main(){
    string ss;
    string FIN("0000"), GOAL("6174");
    while( cin >> ss ){
        if( ss == FIN ) break;
        if( ss[0] == ss[1] && ss[1] == ss[2] && ss[2] == ss[3] ){
            cout << "NA" << endl;
            continue;
        }
        int c = 0;
        while( c < 10 && ss!=GOAL ){
            c++;
            ss = convert(ss);
        }
        cout << c << endl;

    }
    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <sstream>

#define rep(i, j) FOR(i, 0, j)
#define FOR(i, j, k) for(int i = j; i < k; ++i)

using namespace std;

int main(){
  char n[5];
  while(scanf("%s", n) && strcmp(n, "0000") != 0){
    if(n[0] == n[1] && n[1] == n[2] && n[2] == n[3]){ puts("NA"); continue;}
    int c = 0;
    while(strcmp(n, "6174") != 0 && c < 10){
      rep(i, 4) FOR(j, i + 1, 4){
	if(n[i] > n[j]){ char tmp = n[i]; n[i] = n[j]; n[j] = tmp;}
      }
      int a = atoi(n);
      char tmp = n[0]; n[0] = n[3]; n[3] = tmp;
      tmp = n[1]; n[1] = n[2]; n[2] = tmp;
      int b = atoi(n);
      b -=  a;
      sprintf(n, "%d", b);
      c++;
    }
    printf("%d\n", c);
  }
  return 0;
}

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

using namespace std;
typedef long long ll;
typedef pair<ll, ll> pl;

int main()
{
	while (1) {
		string s;
		cin >> s;
		if (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0')
			break;
		if (s == "6174") {
			cout << 0 << endl;
		}
		else if (s[0] == s[1] &&  s[2] == s[3] && s[1] == s[2])
			cout << "NA" << endl;
		else {
			ll ans = 0;
			while (s != "6174") {
				int a[4];
				for (int i = 0; i < 4; i++)
					a[i] = s[i] - '0';

				sort(a, a + 4);

				ll small=0,large=0;
				for (int i = 0; i < 4; i++)
					large += a[i] * (ll)pow(10, i);
				
				reverse(a,a+4);
				for (int i = 0; i < 4; i++) 
					small += a[i] * (ll)pow(10, i);
				
				s = to_string(large - small);
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}