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 <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){
      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 <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-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 2;
const int MOD = 1000000007;

using namespace std;
typedef vector<ll> vl;
typedef vector<vl> vvl;

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

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

int main () {
    string str;
    while (cin >> str) {
        bool f = false;
        for (auto i : str) if (i != '0') f = true;

        if (!f) break;

        f = false;
        for (auto i : str) if (i != str[0]) f = true;

        if (!f) {
            cout << "NA" << endl;
            continue;
        }

        int count = 0;
        int now;
        stringstream org;
        org << str;
        org >> now;
        while (1) {
            if (now == 6174) {
                cout << count << endl;
                break;
            }
            count++;
            sort(str.begin(), str.end());
            auto rstr = str;
            reverse(rstr.begin(), rstr.end());

            stringstream ss;
            ss << rstr << " " << str;
            int A, B;
            ss >> A >> B;
            now = A - B;
            stringstream next;
            next << now;
            next >> str;
            while (str.size() < 4) str = "0" + str;
        }
    }

    return 0;
}

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

int now[4],L[4],S[4];
int ans[10000] = {0},cor = 0,mode[10000];	//mode[i] : 1 = int,0 = char
char *ansc[10000];

void acpy(int *p,int *q,int size){
	for(int i = 0;i < size;i++)
		p[i] = q[i];
}
int main(){
	char str[6];
	int i;
	while(cin >> str){
		for(i = 0;i < 4;i++){
			if(str[i] != '0')
				break;
		}
		if(i == 4)
			break;
		//ここから開始
		for(i = 0;i < 3;i++){
			if(str[i] != str[i+1])
				break;
		}
		if(i == 3){
			ansc[cor] = "NA";
			cor++;
			continue;
		}
		for(i = 0;i < 4;i++){
			now[i] = str[i]-48;
		}
		while(now[0] != 6 || now[1] != 1 || now[2] != 7 || now[3] != 4){
			sort(now,now+4,greater<int>());
			acpy(L,now,4);
			sort(now,now+4);
			acpy(S,now,4);
			for(i = 3;i >= 0;i--){
				now[i] = L[i]-S[i];
				if(now[i] < 0){
					now[i] += 10;
					L[i-1]--;
				}
			}
			ans[cor]++;
		}
		mode[cor] = 1;
		cor++;
	}
	for(i = 0;i < cor;i++){
		if(mode[i])
			cout << ans[i] << endl;
		else
			cout << ansc[i] << endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<string.h>
#include<math.h>
int main(void)
{
	int a,s[5],d,f,g,h,i,j,z;
	while(1){
		scanf("%d",&a);
		if(a==0000) break;
		h=0;
		z=0;
		while(1){
			if(a==6174) break;
			s[0]=a/1000;
			s[1]=(a-s[0]*1000)/100;
			s[2]=(a-s[0]*1000-s[1]*100)/10;
			s[3]=(a-s[0]*1000-s[1]*100-s[2]*10);
		//	printf("%d %d %d %d\n",s[0],s[1],s[2],s[3]);
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(s[i]<s[j]){
						d=s[j];
						s[j]=s[i];
						s[i]=d;
					}
				}
			}
			a=s[0]*1000+s[1]*100+s[2]*10+s[3];
			f=s[3]*1000+s[2]*100+s[1]*10+s[0];
		//	printf("%d %d %d\n",a,f,a-f);
			if(a==f){
				z=1;
				break;
			}
			a-=f;
			h++;
		}
		if(z==1) printf("NA\n");
		else
			printf("%d\n",h);
	}
	return 0;
}

C++: #include<stdio.h>

int main(){

char ty;
char c1,c2,c3,c4,ch5;
int n1=0,n2=0,n3=0,n4=0,N=0,S=0,P=0,K=0;
int n[5]={};

while(1){
//n1=c1-'0';n2=c2-'0';n3=c3-'0';n4=c4-'0';
	
	bool f=false;
	do{
	scanf("%c%c%c%c%c",&c1,&c2,&c3,&c4,&ch5);
	ty=c1;
	
     if(ty==c1&&ty==c3&&ty==c4&&ty==c2)f=false;
	else f=true;

	if(ty=='0')f=true;
	if(f==false)printf("NA\n");

	}while(f!=true);
	if(c1=='0'&&c2=='0'&&c3=='0'&&c4=='0')break;
	
	
	for(int i=0;i<4;i++)
	n[i]=0;
int cnt=0;


n[0]=c1-'0';n[1]=c2-'0';n[2]=c3-'0';n[3]=c4-'0';


for(int i=0;i<3;i++)
for(int j=3;j>i;j--)
if(n[j]>n[j-1]){int t=n[j];n[j]=n[j-1];n[j-1]=t;}

N=1000*n[0]+100*n[1]+10*n[2]+n[3];
S=1000*n[3]+100*n[2]+10*n[1]+n[0];

P=N-S;
if(P!=6174)cnt++;

while(P!=6174){
N=P;
K=P;n[0]=N/1000;
K=N%1000;n[1]=K/100;
K=N%100;n[2]=K/10;
K=N%10;n[3]=K;

//printf(" %d\n",N);

for(int i=0;i<3;i++)
for(int j=3;j>i;j--)
if(n[j]>n[j-1]){int t=n[j];n[j]=n[j-1];n[j-1]=t;}


N=1000*n[0]+100*n[1]+10*n[2]+n[3];
S=1000*n[3]+100*n[2]+10*n[1]+n[0];

cnt++;
P=N-S;

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

C++: #include <bits/stdc++.h>
#define endl    '\n'
using namespace std;

typedef long long ll;

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

    int cnt;
    int n;
    int l[4], s[4];
    int l_num, s_num;

    cin >> n;

    while (n != 0) {
        cnt = 0;

        if (n % 1111 == 0) {
            cout << "NA";
        } else {
            while (n != 6174) {
                l_num = s_num = 0;

                for (int i = 3; i >= 0; --i) {
                    s[i] = n % 10;
                    n /= 10;
                }

                sort(s, s + 4);
                s_num += s[0] * 1000;
                s_num += s[1] * 100;
                s_num += s[2] * 10;
                s_num += s[3];

                l_num += s[3] * 1000;
                l_num += s[2] * 100;
                l_num += s[1] * 10;
                l_num += s[0];

                n = l_num - s_num;
                cnt++;
            }

            cout << cnt;
        }
        cout << endl;

        cin >> n;
    }

    return 0;
}