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 <algorithm>
#include <deque>
using namespace std;
string L(string);
string S(string);
string sa(string,string);
int main(void)
{
	int count;
	string n;

	while(1){
		cin >> n;
		if(n=="0000") break;
		if(n=="1111"||n=="2222"||n=="3333"||n=="4444"||n=="5555"||n=="6666"||n=="7777"||n=="8888"||n=="9999") {
			cout << "NA" << endl;
			continue;
		}
		count=0;
		while(n!="6174"){
			n=sa(L(n),S(n));
			count+=1;
		}
		cout << count << endl;
	}
	return 0;
}
string S(string n)
{
	string s;
	deque<char> dn;
	for(int i=0;i<4;i++) dn.push_back(n[i]);
	sort(dn.begin(),dn.end());
	for(int i=3;0<=i;i--){
		s+=dn[i];
		dn.pop_back();
	}
	return s;
}
string L(string n)
{
	string l;
	deque<char> dn;
	for(int i=0;i<4;i++) dn.push_back(n[i]);
	sort(dn.begin(),dn.end());
	for(int i=0;i<4;i++){
		l+=dn[i];
	}
	return l;
}
string sa(string l,string s)
{
	string n;
	int lnum=0,snum=0,num,b=1;
	for(int i=3;0<=i;i--){
		lnum+=(b*(l[3-i]-'0'));
		snum+=(b*(s[3-i]-'0'));
		b*=10;
	}
	num=lnum-snum;
	for(int i=0;i<4;i++){
		b/=10;
		n+=((num/b)%10+'0');
	}
	//cout << n << endl;
	return n;
}

C++: #include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<algorithm>
typedef unsigned long long ull;
#define rep(i,a) for(int i=0;i<a;i++)
#define loop(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1);
const double inf = (int)1e8;

int main(void){
  string s;
  while(cin>>s){
    bool brek = true;
    rep(i,4)if(s[i] != s[0])brek = false;
    if(brek&&s[0]=='0')break;
    if(brek&&s[0]!='0'){cout<<"NA"<<endl;continue;}

    int cnt = 0;
    while(s!="6174"){
      cnt ++;
      vector<char> used;
      rep(i,4){
	used.push_back(s[i]);
      }
    
      sort(used.begin(),used.end());
      string diff;
      rep(i,used.size()){
	diff += used[used.size()-i-1] - used[i] + '0';
	if(diff[i]<'0'){diff[i-1]--; diff[i]+= 10;}
      }
      //cout<<diff<<endl;
      s=diff;
    }
    cout<<cnt<<endl;
  }
  return 0;
}

C++: #include<iostream>
#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 << 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 )
		{
			puts("NA");
			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() ) );
		}

		printf( "%d\n", ans );
	}

	return 0;
}

C++: #include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std;
char A[9],B[9];
#define I(s)strtol(s.c_str(),NULL,10)
string S(int i,int L){sprintf(A,"%%0%dd",L),sprintf(B,A,i);return B;}
int main(){
	int a,i;
	for(;scanf("%d",&a),a;printf(a?"%d\n":"NA\n",i)){
		for(i=0;a&&a!=6174;i++){
			string t=S(a,4),s;
			sort(t.begin(),t.end());
			s=t;
			reverse(s.begin(),s.end());
			a=I(s)-I(t);			
		}
	}
}

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

int main(){
  string str;
  while(cin >> str && str != "0000"){
    if(str == "1111" || 
       str == "2222" || 
       str == "3333" || 
       str == "4444" || 
       str == "5555" || 
       str == "6666" || 
       str == "7777" || 
       str == "8888" || 
       str == "9999") cout << "NA" << endl;
    else if(str == "6174") cout << 0 << endl;
    else{
      for(int n=1;;n++){
        int L=0,S=0,LS,a,b;
        bool f[4];
        fill(f,f+4,false);

        for(int i=1;i<=1000;i*=10){
          a = b = -1;
          for(int j=0;j<4;j++){
            if(f[j]) continue;
            else if(a < str[j] - '0'){
              a = str[j] - '0';
              b = j;
            }
          }
          f[b] = true;
          L += 1000 / i * a;
          S += i * a;
        }
        LS = L - S;

        if(LS == 6174){
          cout << n << endl;
          break;
        }

        for(int i=3;i>=0;i--){
          str[i] = LS % 10 + '0';
          LS /= 10;
        }
      }
    }
  }
}

C++: #include<iostream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<cstdlib>

int main() {
	char str[4];
	while (std::cin >> str) {
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == '0')
				break;
			else
				std::cout << "NA" << std::endl;
		}
		char l[4], s[4];
		int cnt = 0;
		while(str!="6174") {
			++cnt;
			std::memcpy(l, str, 4);
			std::memcpy(s, str, 4);
			std::sort(l, l + 4, std::greater<char>());
			std::sort(s, s + 4);
			int a = atoi(l) - atoi(s);
			itoa(a, str, 10);
		}
		std::cout << cnt << std::endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>

using namespace std;

int main(){
	int a[4];
	int b,flg,c,d,e,cunt,m,f;
	
	scanf("%d",&d);
	while(d!=0000){
		
		m=1000;
		cunt=0;
		flg=0;
		
		f=d;
		
		for(int i=0;i<3;i++){
			a[3-i]=d/m;
			d=d%m;
			m/=10;
		}
		a[0]=d;
		
		for(int i=0;i<3;i++){
			if(a[i]!=a[i+1]) flg=1;
		}
		
		
		
		if(flg==1){
			while(f!=6174){
				c=0;
				e=0;
				sort(a,a+4);
				m=1;
				for(int i=0;i<4;i++){
					c+=a[i]*m;
					m*=10;
				}
				sort(a,a+4,greater<int>());
				m=1;
				for(int i=0;i<4;i++){
					e+=a[i]*m;
					m*=10;
				}
				
				d=c-e;
				
				
				f=d;
				
				m=1000;
				
				for(int i=0;i<3;i++){
					a[3-i]=d/m;
					d=d%m;
					m/=10;
				}
				a[0]=d;
				cunt++;
			}
		}
		
		if(flg==0){
			printf("NA\n");
		}else{
			printf("%d\n",cunt);
		}
		scanf("%d",&d);
	}

}

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

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

int s(int n, bool f) {
  vector<int> a;
  while (n) {
    a.push_back(n % 10);
    n /= 10;
  }

  sort(a.begin(), a.end());
  if (f) reverse(a.begin(), a.end());

  int res = 0;
  for (int i = 0; i < a.size(); ++i) {
    res *= 10;
    res += a[i];
  }

  return res;
}

int main() {
  int n;

  while (cin >> n, n) {
    if (n % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }

    // cout << s(n, true) - s(n, false) << endl;

    int cnt = 0;
    while (n != 6174) {
      cnt++;
      n = s(n, true) - s(n, false);
    }

    cout << cnt << endl;
  }
}