Go: package main

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

var memo []int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if memo[v] == 0 {
		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([]int, 10000)
	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 <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>
#include <sstream>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

const int INF = 1 << 24;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

typedef unsigned long long ull;
typedef std::pair<int,int> P;

int main(){
    std::string s;
    while(std::cin >> s, s != "0000"){
        if(s[0] == s[1] && s[0] == s[2] && s[0] == s[3]){
            std::cout << "NA" << std::endl;
        }else{
            int t = 0;
            while(s != "6174"){
                int L, S;
                std::sort(s.begin(), s.end(), std::greater<char>());
                L = std::stoi(s);
                std::sort(s.begin(), s.end());
                S = std::stoi(s);

                s = std::to_string(L-S);
                t++;
            }

            std::cout << t << std::endl;
        }
    }
}

C++: #include<stdio.h>
int main(void)
{
	int N,L,S,N1,N2,N3,N4,N5,N6,i,temp,j,cnt,flg,flg1;
	int a[4];

	scanf("%d",&N);
	while(!(N==0000)){
		cnt=0;
		flg=0;
		while(!(N==6174)){
			cnt++;
			N1=N/1000;/*6!*/
			N2=N%1000;/*174*/
			N3=N2/100;/*1!*/
			N4=N2%100;/*74*/
			N5=N4/10;/*7*/
			N6=N4%10;/*4!*/
			if(N1==N3 && N1==N5 && N1==N6){
				printf("NA\n");
				flg=1;
				break;
			}
			else{
				a[0]=N1;
				a[1]=N3;
				a[2]=N5;
				a[3]=N6;
				for(i=0;i<4;i++){
					for(j=i+1;j<4;j++){
						if(a[i]<a[j]){
							temp=a[j];
							a[j]=a[i];
							a[i]=temp;	
						}
					}
				}
				N1=a[0]*1000;
				N3=a[1]*100;
				N5=a[2]*10;
				N6=a[3];
				L=N1+N3+N5+N6;
				for(i=0;i<4;i++){
					for(j=i+1;j<4;j++){
						if(a[i]>a[j]){
							temp=a[i];
							a[i]=a[j];
							a[j]=temp;	
						}
						
					}
				}
				N1=a[0]*1000;
				N3=a[1]*100;
				N5=a[2]*10;
				N6=a[3];
				S=N1+N3+N5+N6;
				N=L-S;
			}
		}
		if(flg==0){
			printf("%d\n",cnt);
		}
		scanf("%d",&N);
	}
	return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<sstream>
#include<string>
#include<cstdlib>
#define rep(i,a) for( int i = 0; i != (a); ++i )

std::string intToStr( int n )
{
	std::ostringstream oss;
	oss << std::setprecision( 4 ) << std::setfill( '0' ) << n;

	return oss.str();
}

int main()
{
	std::string L, S;
	while( std::cin >> L, L != "0000" )
	{
		bool fl = true;

		rep( i, 3 )
			fl &= L[i+1] == L[i];
		if( fl )
		{
			std::cout << "NA" << std::endl;
			continue;
		}

		int ans = 0;
		while( L != "6174" )
		{
			S = L;
			std::sort( L.rbegin(), L.rend() );
			std::sort( S.begin(), S.end() );

			++ans;

			L = intToStr( atoi( L.c_str() ) - atoi( S.c_str() ) );
		}

		std::cout << ans << std::endl;
	}

	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]={};
int F=0;
while(1){
//n1=c1-'0';n2=c2-'0';n3=c3-'0';n4=c4-'0';
	
	bool f=false;
	bool FF=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;
	F++;
	if(F==10001)break;
	
	if(c1=='6'&&c2=='1'&&c3=='7'&&c4=='4')FF=true;



	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(FF!=true)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>
using namespace std;

typedef long int lint;
typedef long long int llint;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
typedef pair<long, long> pll;

#define EACH(i, c) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, e) for (int i = (s); i < (int)(e); ++i)
#define REP(i, n) FOR(i, 0, n)
#define endl '\n'
#define mod 1000000007
#define INF 100000000
#define fk first
#define sv second

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, 1, 0, -1, 1, -1, 1, -1};

template<typename T>
void print(const std::vector<T>& v, char sep=' ') {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i != 0) cout << sep;
    cout << v[i];
  }
  cout << endl;
}

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

  while (cin >> n, n){
    int ct = 0;
    if (n % 1111 == 0){
      cout << "NA\n";
      continue;
    }
    
    while (n != 6174){
      int l, s, num[4];
      REP(i, 4){
        num[i] = n % 10;
        n /= 10;
      }
      sort(num, num + 4);
      l = s = 0;
      REP(i, 4){
        l = l * 10 + num[3 - i];
        s = s * 10 + num[i];
      }
      n = l - s;
      ct++;
    }
    cout << ct << endl;
  }
}

C++: #include <iostream>
#include <stdio.h>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <set>
#include <math.h>
#include <utility>
#include <stack>
#include <string.h>
#include <complex>
using namespace std;
const int INF = 1<<29;
const double EPS = 1e-8;
typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int,int> P;
struct edge{int to,cost;};

const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};



int main(){
    string N;
    while(cin >> N, N != "0000"){
        if(N=="0000"||N=="1111"||N=="2222"||N=="3333"||N=="4444"||N=="5555"||N=="6666"||N=="7777"||N=="8888"||N=="9999"){
            cout << "NA" << endl;
            continue;
        }else if(N=="6174"){
            cout << 0 << endl;
            continue;
        }
        vec Nv(4);
        for(int i=0;i<4;i++){
            Nv[i] = N[i]-'0';
        }
        int t = 0;
        while(1){
            t++;
            sort(Nv.begin(), Nv.end());
            int l=0, s=0, p=1;
            for(int i=0;i<4;i++){
                l += Nv[i]*p;
                s += Nv[i]*1000/p;
                p *= 10;
            }
            int sub = l - s;
            if(sub == 6174) break;
            p = 1000;
            for(int i=0;i<4;i++){
                Nv[i] = (sub/p)%10;
                p /= 10;
            }
        }
        cout << t << endl;
    }
    return 0;
}