Go: package main

import (
	"fmt"
	"sort"
)

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

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") {
				while (s.size() != 4)
					s = '0' + s;
				
				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;
}

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

string cn;
int main()
{
  while(true){
    cin >> cn;
    if(cn == "0000"){
      break;
    }
    if(cn[0] == cn[1] == cn[2] == cn[3]){
      cout << "NA" << endl;
    }

    else{
      int ans = 0;
      while(cn != "6174"){
	int in, intss;
	stringstream ss1, ss2, ss3;
	string cncp;
	sort(cn.begin(), cn.end(), greater<char>());
	ss1 << cn;
	ss1 >> intss;
	in = intss;
	for(int j = 0; j < 4; j++){
	  cncp += cn[3 - j];
	}
	ss2 << cncp;
	ss2 >> intss;
	in -= intss;
	ss3 << in;
	ss3 >> cn;
	ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int main()
{
	int N,T,C;
	int L, S;
	int data[4];
	int su[4] = { 1000,100,10,1 };
	while (1) {
		C = 0;
		cin >> N;
		if (N == 0) break;
		if (N % 1111 == 0) {
			cout << "NA" << endl;
			continue;
		}

		while (N != 6174) {
			L = S = 0;
			C++;
			data[0] = N / 1000;
			N %= 1000;
			data[1] = N / 100;
			N %= 100;
			data[2] = N / 10;
			N %= 10;
			data[3] = N;

			for (int i = 0; i < 3; i++) {
				for (int j = i + 1; j < 4; j++) {
					if (data[i] < data[j]) {
						T = data[i];
						data[i] = data[j];
						data[j] = T;
					}
				}
			}
			
			for (int i = 0; i < 4; i++) {
				L += data[i] * su[i];
				S += data[3 - i] * su[i];
			}
	
			N = L - S;
		}
		cout << C << endl;
	}
}

C++: #include <iostream>
//#include <cstdio>
#include <stdio.h>
#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;
#define all(vec) vec.begin(),vec.end()
typedef long long int ll;
typedef pair<ll,ll> P;
const ll MOD=1000000007;
const ll INF=1000000010;
const ll LINF=4000000000000000010LL;
const int MAX=310;
const double EPS=1e-9;
int main(){
    int n;
    while(cin>>n){
        if((n%10000)/1000==(n%1000)/100&&(n%1000)/100==(n%100)/10&&(n%100)/10==n%10){
            if(n%10==0){
                break;
            }
            cout<<"NA"<<endl;
            continue;
        }
    int c=0;
    while(n!=6174){
        int a[4];
        a[0]=(n%10000)/1000;
        a[1]=(n%1000)/100;
        a[2]=(n%100)/10;
        a[3]=n%10;
        int p;
        sort(a,a+4);
        p=1;
        int l=0,s=0;
        for(int i=0;i<4;i++){
            s+=a[i]*p;
            p*=10;
        }
        p=1;
        for(int i=3;i>=0;i--){
            l+=a[i]*p;
            p*=10;
        }
        n=s-l;
        c++;
    }
    cout<<c<<endl;
    }
}

C++: #include <cstdio>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <functional>
using namespace std;

int main(){
	int n,nl,nh;
	char c[5],bl[5],bh[5];

	while(scanf("%s",c), (c[0] | c[1] | c[2] | c[3]) != '0'){
		if(c[0] == c[1] && c[0] == c[2] && c[0] == c[3]){
			printf("NA\n");
			continue;
		}
		int tm = 0;
		while(!(c[0] == '6' && c[1] == '1' && c[2] == '7' && c[3] == '4')){
			strcpy(bl,c);
			strcpy(bh,c);
			sort(bl,bl+4);
			sort(bh,bh+4, greater<char>());
			nl = nh = 0;
			for(int i = 0; i < 4; i++){
				nl += (bl[i] - '0') * pow(10,3-i);
				nh += (bh[i] - '0') * pow(10,3-i);
			}
			n = nh - nl;
			for(int i = 0; i < 4; i++){
				c[3-i] = '0' + (n % 10);
				n /= 10;
			}
			tm++;
		}
		printf("%d\n",tm);
	}

	return 0;
}

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

int main(){
    int cnt,a,b;
    char num[5];
    while(1){
        cin >> num;
        cnt = 0;
        if(strcmp(num,"0000") == 0) break;
        if(num[0] == num[1] && num[1] == num[2] && num[2] == num[3]){
            cout << "NA" << endl;
            continue;
        }
        while(1){
            if(strcmp(num,"6174") == 0) break;
            cnt++;
            sort(num,num + 4);
            a = atoi(num);
            sort(num,num + 4,greater<char>());
            b = atoi(num);
            b -= a;
            sprintf(num,"%d",b);
        }
        cout << cnt << endl;
    }
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
string S; int a[4], b, cnt;
int main() {
	while (true) {
		cin >> S;
		if (S == "0000") { break; }
		if (S[0] == S[1] && S[2] == S[3] && S[1] == S[2]) { cout << "NA" << endl; }
		else {
			b = stoi(S);
			cnt = 0;
			while (b != 6174) {
				cnt++;
				for (int i = 0; i < 4; i++) {
					a[i] = (b / (int)pow(10, i)) % 10;
				}
				sort(a, a + 4);
				b = (a[3] - a[0]) * 1000 + (a[2] - a[1]) * 100 + (a[1] - a[2]) * 10 + (a[0] + a[3]);
			}
			cout << cnt << endl;
		}
	}
}