Go: package main

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

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 {
		var s string
		fmt.Scan(&s)
		v, _ = strconv.Atoi(s)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <climits>
#include <algorithm>
#include <map>
#include <sstream>
using namespace std;

int toInt(string s) {
    stringstream ss(s);
    int ret;
    ss >> ret;
    return ret;
}

string toStr(int n) {
    stringstream ss;
    ss << n;
    return ss.str();
}

string next(string N) {
    sort(N.begin(), N.end());
    int S = toInt(N);
    reverse(N.begin(), N.end());
    int L = toInt(N);
    return toStr(L-S);
}

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;
            continue;
        }
        int cnt = 0;
        while (N != "6174") {
            N = next(N);
            ++ cnt;
        }
        cout << cnt << endl;
    }
    return 0;
}

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

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;
            continue;
        }
        int ans = 0;
        while(n != "6174"){
            string l = n, s = n;
            sort(l.begin(), l.end(), greater<char>()); sort(s.begin(), s.end());
            int t = atoi(l.c_str()) - atoi(s.c_str());
            for(int i = 3; i >= 0; --i){
                n[i] = '0' + t % 10;
                t /= 10;
            }
            ++ans;
        }
        cout << ans << endl;
    }
}

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

using namespace std;

int d[4];
string in;
int data[10000];

void solve(){
  bool f = true;
  for(int i=1;i<4;i++) if(in[0] != in[i]) f = false;
  if(f) cout << "NA" << endl;
  else{
    int ans = 0;
    for(int i=0;i<4;i++) d[i] = in[i] - '0';
    while(1){
      if(d[0] == 6 && d[1] == 1 && d[2] == 7 && d[3] == 4) break;
      int l = 0, s = 0;
      sort(d, d+4);
      for(int i=0;i<4;i++){
        s *= 10;
        s += d[i];
        l *= 10;
        l += d[3-i];
      }
      ans++;
      int tmp  = l - s;
      if(data[tmp] != -1){
        ans += data[tmp];
        break;
      }
      for(int i=0;i<4;i++){
        d[3-i] = tmp % 10;
        tmp /= 10;
      }
    }
    int tmp = 0;
    for(int i=0;i<4;i++){
      tmp *= 10;
      tmp += d[3-i];
    }
    data[tmp] = ans;
    cout << ans << endl;
  }
}

main(){
  fill(data, data+10000, -1);
  while(cin >> in){
    if(in == "0000") break;
    solve();
  }
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

signed main() {
	int A[4];
	for (string s; cin >> s&&s != "0000";) {
		int cnt = 0;
		if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
			cout << "NA" << endl;
			continue;
		}
		while (s != "6174") {
			cnt++;
			rep(i, 0, 4) {
				A[i] = s[i] - '0';
			}
			sort(A, A + 4);
			int L = 0, S = 0;
			rep(i, 0, 4) {
				L *= 10;
				S *= 10;
				L += A[3 - i];
				S += A[i];
			}
			s = to_string(L - S);
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include <stdio.h> // C++???????????????
#include <string.h> // c++???????????????
#include <math.h> // c++???????????????
#include <iostream>
#include <string>
#include <stack>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;

typedef long long int lli;
#define PI 3.1415926535897932
#define MAX(a,b) ((a>b)?(a):(b)) // ?????§????????????
#define MIN(a,b) ((a<b)?(a):(b)) // ????°?????????????
#define INRANGE(a,b,c) ((a<=b&&b<=c)?(1):(0)) // a<=b<=c??????????????????????????????
#define N 4

void swap(int &a,int &b){ // c??????*
	int w=a;
	a=b;
	b=w;
}

int getN(int num,int digit){ // ?????????????????°????????????
	int i=1,j;
	for(j=1;j<digit;i*=10,j++);
	return (num/i)%10;
}

int zeller(int y,int m,int d){ // ?????§??????
	if(m<3){y--; m+=12;}
	return (y+y/4-y/100+y/400+(13*m+8)/5+d)%7;
	// 0=sunday!
}

int gcd(int a,int b){	// ?????§??¬?´???°
	if(b==0) return a;
	return gcd(b,a%b);
}

int lcm(int a,int b){	// ????°???¬?????°
	return a*b/gcd(a,b);
}

int stoi(const string str){ // ?????????????????´??°
	int i=0,j;
	int len=str.length();
	int w=0;
	for(i=1,j=1;i<=len && INRANGE('0',str[len-i],'9');i++,j*=10){
		w+=(str[len-i]-'0')*j;
	}
	if(str[0]=='-')w*=-1; // ??????????????????
	return w;
}

string itos(int a){ // ??´??°???????????????
	int i;
	string str="";
	int keta;
	if(a<0){str+='-';a*=-1;} // ?????°??????????????????
	for(keta=10;a/keta!=0;keta*=10);
	for(i=keta/10;i!=0;i/=10){
		str+=((a/i)%10)+'0';
	}
	return str;
}

int main(void){
	string str;
	string str1,str2;
	int a,b; //a>b
	int c;
	while(cin>>str,str!="0000"){
		c=0;
		while(str!="6174"){
			for(int i=0;i<N;i++){
				for(int j=i+1;j<N;j++){
					if(str[i]<str[j])swap(str[i],str[j]);
				}
			}
			str1=str;
			for(int i=0;i<N/2;i++) swap(str[i],str[N-i-1]);
			str2=str;
			str=itos(stoi(str1)-stoi(str2));
			if(str=="0"){cout<<"NA"<<endl; goto LOOP;}
			c++;
		}
		cout<<c<<endl;
LOOP:;
	}
	return 0;
}

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