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 <iostream>
#include <algorithm>
using namespace std;

int main(){
  int n, c[4];
  while((cin >> n), n){
    if(n%1111==0){
      cout << "NA\n";
      continue;
    }
    int num = 0;
    while(n!=6174){
      for(int i=0; i<4; i++){
	c[i] = n%10;
	n/=10;
      }
      sort(c,c+4);
      int p=0, q=0;
      for(int i=0; i<4; i++){
	p*=10; q*=10;
	p+=c[i]; q+=c[3-i];
      }
      n = q-p;
      num++;
    }
    cout << num << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <cctype>
#include <utility>
#include <sstream>
#include <iomanip>
#include <string>
#include <array>
#include <algorithm>
using namespace std;

#define _gp(l) const auto gcu{getchar##l}; const auto pcu{putchar##l}
#ifdef __linux
_gp(_unlocked);
#else
_gp();
#endif
#define _DEF(r, n, ...) inline r n(__VA_ARGS__) noexcept
#define _T template <typename T>
#define _HT template <typename H,typename... T>
_T _DEF(T,in,int c){T n=0;int m=1;while(isspace(c)){c=gcu();}if(c=='-')m=-1,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;}
_DEF(int,in,){return in<int>(gcu());}
#define _SCAN(...) _DEF(bool,scan,__VA_ARGS__)
_T _SCAN(T &n){int c=gcu();return c==EOF?false:(n=in<T>(c),true);}
_DEF(bool, scan, int &c){c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_SCAN(string &s){int c;s="";
	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=(char)c;}}
#endif
_HT _SCAN(H &h,T&&... t){return scan(h)&&scan(t...);}
#define _OUT(...) _DEF(void,out,__VA_ARGS__)
#define _OUTL(...) _DEF(void,outl,__VA_ARGS__)
_OUT(bool b){pcu('0'+b);} _OUT(const char *s){while(*s)pcu(*s++);} _OUT(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_OUT(string s){for(char c:s)pcu(c);}
#endif
_T _OUT(T n){static char b[20];char *p=b;T m=n<0?pcu('-'),-1:1;
	if(!n)*p++='0';else while(n)*p++=(char)(n%10*m+'0'),n/=10;while(p!=b)pcu(*--p);}
_OUTL(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _OUT(vector<T> v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);}
#endif
_HT _OUT(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _OUTL(T&&... t){out(move(t)...);outl();}
struct range{
	int e,b=0,s=1; range(int _b,int _e,int _s):e(_e),b(_b),s(_s){} range(int _b,int _e): e(_e), b(_b){} range(int _e):e(_e){}
	struct it { int v, s; it (int _v, int _s) : v(_v), s(_s) {} operator int()const{return v;} operator int&(){return v;} int operator*()const{return v;}
		it& operator++(){v+=s;return *this;} }; it begin() {return {b, s};} it end() {return {e, s};}};

int loop(int n) {
	static array<int, 10000> m {};
	if (n == 6174)
		return 0;
	else if (m[n])
		return m[n];
	stringstream s;
	s << setw(4) << setfill('0') << n;
	string a = s.str();
	sort(begin(a), end(a));
	int S {stoi(a)};
	reverse(begin(a), end(a));
	int L {stoi(a)};
	return m[n] = loop(L - S) + 1;
}

int main() {
	for (;;) {
		string a;
		scan(a);
		if ([&] {
			for (int i: range(1, 4))
				if (a[0] != a[i])
					return false;
			return true;}()) {
			if (a[0] == '0')
				return 0;
			else {
				outl("NA");
				continue;
			}
		}
		outl(loop(stoi(a)));
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  string n;
  cin >>n;
  while(n!="0000"){
    int a[4];
    for(int i=0;i<4;i++){
      stringstream ss;
      ss <<n[i];
      ss >>a[i];
    }
    if((a[0]==a[1])&&(a[1]==a[2])&&(a[2]==a[3])){
      cout <<"NA"<<endl;
      cin >>n;
      continue;
    }
    for(int i=0;i<100;i++){
      if(n=="6174"){
        cout <<i <<endl;
        break;
      }
      sort(a,a+4);
      int l=a[0]+a[1]*10+a[2]*100+a[3]*1000;
      int s=a[3]+a[2]*10+a[1]*100+a[0]*1000;
      int n1=l-s;
      stringstream ss;
      ss << n1;
      ss >> n;
      if(n1<10){
        n.resize(4);
        n[3]=0;
        n[2]=0;
        n[1]=0;
      }else if(n1<100){
        n.resize(4);
        n[3]=0;
        n[2]=0;
      }else if(n1<1000){
        n.resize(4);
        n[3]=0;
      }
        for(int j=0;j<4;j++){
        stringstream ss1;
        ss1 <<n[j];
        ss1 >>a[j];
      }
    }
    cin >>n;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
main(){
  while(1){
    int n,l,s,cnt=0;
    bool check=true;
    int keta[4]={0};
    cin>>n;
    if(n==0){
      break;
    }
    keta[0]=(n)%10;
    keta[1]=(n/10)%10;
    keta[2]=(n/100)%10;
    keta[3]=(n/1000)%10;
    if(keta[0]==keta[1]&&keta[1]==keta[2]&&keta[2]==keta[3]){
      cout<<"NA"<<endl;
    }
    else{
      while(n!=6174){
	cout<<n<<endl;
	sort(keta,keta+4);
	s=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];
	l=keta[0]+keta[1]*10+keta[2]*100+keta[3]*1000;
	n=l-s;
	keta[0]=(n)%10;
	keta[1]=(n/10)%10;
	keta[2]=(n/100)%10;
	keta[3]=(n/1000)%10;
	cnt++;
      }
      cout<<cnt<<endl;
    }
  }
}