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 <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string s;
    while (cin >> s) {
        if (s == "0000") return 0;
        vector<int> nums(4);
        for (int i = 0; i < 4; ++i) {
            nums[i] = s[i] - '0';
        }
        bool ok = false;
        for (int i = 0; i < 3; ++i) {
            if (nums[i] != nums[i + 1]) {
                ok = true;
                break;
            }
        }
        if (!ok) {
            cout << "NA" << '\n';
        } else {
            int res = 0;
            int nnow = 1000;
            for (int i = 0; i < 4; ++i) {
                res += nums[i] * nnow;
                nnow /= 10;
            }
            int cnt = 0;
            while (true) {
                if (res == 6174) break;
                vector<int> num(4);
                for (int i = 0; i < 4; ++i) {
                    num[i] = res % 10;
                    res /= 10;
                }
                sort(num.begin(), num.end());
                int large = 0, small = 0;
                int now = 1;
                for (int i = 0; i < 4; ++i) {
                    large += num[i] * now;
                    now *= 10;
                }
                now = 1;
                for (int i = 3; i >= 0; --i) {
                    small += num[i] * now;
                    now *= 10;
                }
                res = large - small;
                ++cnt;
            }
            cout << cnt << '\n';
        }
    }
    return 0;
}

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

typedef long long ll;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, n) for(int i = 0; i < (n); i++)
#define dep(i, a, n) for(int i = a; i >= (n); i--)
#define FOR(i, a, n) for(int i = a; i < (n); i++)
#define mod (ll)(1e9+7)
#define int ll
#define INF 100000000

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int ch(int n) {
  vector<int> v;
  rep(i, 4) {
    v.pb(n % 10);
    n /= 10;
  }
  sort(v.begin(), v.end());
  int la = 0, sm = 0;
  rep(i, 4) {
    la += v[i] * pow(10, i);
    sm += v[3 - i] * pow(10, i);
  }
  return la - sm;
}

signed main() {
  ll n;
  while(cin >> n) {
    if(n == 0000) break;
    if(n % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }
    ll cnt = 0;
    while(1) {
      if(n == 6174) {
        cout << cnt << endl;
        break;
      }
      n = ch(n);
      cnt++;
    }
  }
	return 0;
}

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

int main(){
    int n, d[4];
    while(cin >> n){
        if(n == 0)  break;
        
        if(n % 1111 == 0){
            cout << "NA" << endl;
        }else{
            int tmpn = n;
            for(int i = 3; i >= 0; i--){
                d[i] = tmpn%10;
                tmpn /= 10;
            }
            
            int ans = 0;
            while(n != 6174){
                ans++;
                sort(d, d+4);
                n = (1000*d[3] + 100*d[2] + 10*d[1] + d[0]) -
                        (1000*d[0] + 100*d[1] + 10*d[2] + d[3]);
                tmpn = n;
                for(int i = 3; i >= 0; i--){
                    d[i] = tmpn%10;
                    tmpn /= 10;
                }
            }
            
            cout << ans << endl;
        }
    }
    return 0;
}

C++: #include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    while(1){
        int num;
        cin>>num;
        if(num==0)break;
        int numnum[4];
        int ans=0;
        while(1){
            if(num==6174){
                cout<<ans<<endl;
                break;
            }
            numnum[0]=num%10000/1000;
            numnum[1]=num%1000/100;
            numnum[2]=num%100/10;
            numnum[3]=num%10/1;
            if(numnum[0]==numnum[1]&&numnum[0]==numnum[2]&&numnum[0]==numnum[3]){
                puts("NA");
                break;
            }
            int nummax[4];
            int nummin[4];
            for(int i=0;i<4;i++){
                nummax[i]=numnum[i];
                nummin[i]=numnum[i];
            }
            sort(nummax,nummax+4);
            sort(nummin,nummin+4);
            swap(nummax[0],nummax[3]);swap(nummax[1],nummax[2]);
            int mx=nummax[0]*1000+nummax[1]*100+nummax[2]*10+nummax[3]*1;
            int mn=nummin[0]*1000+nummin[1]*100+nummin[2]*10+nummin[3]*1;
            //printf("[%d][%d]\n",mx,mn);
            num=mx-mn;
            ans++;
        }
    }
    return 0;
}

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

// int型からstring型に変換する(非負整数)
string to_s(int n){
	string s;
	do{
		s.push_back( (n%10)+'0' );
		n /= 10;
	}while( n != 0 );
	reverse( s.begin() , s.end() );
	return s;
}

// string型からint型に変換する(非負整数)
int to_i(string s){
	int n = 0;
	for(int i=0 ; i < s.size() ; i++ ){
		n = (n * 10) + s[i] - '0';
	}
	return n;
}

int main(){
	string N;
	while( cin >> N, N != "0000" ){
		if( N[0] == N[1] && N[1] == N[2] && N[2] == N[3] ){
			cout << "NA" << endl;
		}else{
			int ans = 0;
			while( N != "6174" ){
				string L = N, S = N;
				sort(L.rbegin(), L.rend());
				sort(S.begin(), S.end());
				N = to_s(to_i(L) - to_i(S));
				while( N.size() < 4 ) N = "0" + N;
				ans++;
			}
			cout << ans << endl;
		}
	}
}

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

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)
{
	int a;
	while (cin >> a, a){
		string n = toString(a);
		if (n.length() < 4) {
			for (int i = 0; i <= 4-n.length(); i++) {
				n += '0';
			}
		}
		if (n[0] == n[1] && n[0] == n[2] && n[0] == n[3]){
			cout << "NA" << endl;
			continue;
		}

		int cnt = 0;
		int tmp;
		string N = n;
		while (1){
			if (N == "6174"){
				cout << cnt << endl;
				break;
			}
			//cout << N << endl;
			//cin >> tmp;
			string L = N; sort(L.rbegin(), L.rend());
			string S = N; sort(S.begin(), S.end());
			int d = toInt(L) - toInt(S);
			N = toString(d);
			if (N.length() < 4) {
			for (int i = 0; i <= 4-N.length(); i++) {
				N += '0';
			}
		}
			cnt++;
		}
	}

	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>

using namespace std;

void st(char *hoge){
	for(int i=0;i<4;i++){
		for(int j=i+1;j<4;j++){
			if(hoge[i] < hoge[j]){
				char t = hoge[i];
				hoge[i] = hoge[j];
				hoge[j] = t;
			}
		}
	}
}

void rst(char *hoge){
	for(int i=0;i<4;i++){
		for(int j=i+1;j<4;j++){
			if(hoge[i] > hoge[j]){
				char t = hoge[i];
				hoge[i] = hoge[j];
				hoge[j] = t;
			}
		}
	}
}

int main(){

	while(1){
		int n=0,l,s;
		int cnt = 0;
		char tmp[5] = {0};

		scanf("%s",tmp);
		n = atoi(tmp);

		if(n == 0)break;
		if(n % 1111 == 0){
			printf("NA\n");
			continue;
		}

		while(n != 6174){
			sprintf(tmp, "%d", n);
			rst(tmp);
			s = atoi(tmp);

			st(tmp);
			l = atoi(tmp);
			n = l - s;

			cnt++;
		}
		printf("%d\n", cnt);
	}
	return 0;
}