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 <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <cstdio>

using namespace std;

int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

int main(void)
{
	string n;
	while (cin >> n){
		if (n == "0000"){
			return 0;
		}
		if (count(n.begin(), n.end(), n[0]) == 4){
			cout << "NA" << endl;
		}

		int cnt = 0;
		while (n != "6174"){
			string L = n; sort(L.rbegin(), L.rend());
			string S = n; sort(S.begin(), S.end());
			int tmp = toInt(L) - toInt(S);
			char str[5];
			sprintf(str, "%04d", tmp);
			n = string(str);
			cnt++;
		}
		cout << cnt << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <algorithm> 
using namespace std;
int main(){
	int N; 
	int n[4];
	while(true){
		int cnt=0;
		cin >> N;
		if(N/1000==0) break;
		if(N%1111==0) {
			cout << "NA\n";
			continue;
		}
		while(N!=6174){
			n[0]=N/1000;
			n[1]=(N%1000)/100;
			n[2]=(N%100)/10;
			n[3]=N%10;
			sort(n,n+4);
			int L = n[3]*1000+n[2]*100+n[1]*10+n[0];
			int S = n[0]*1000+n[1]*100+n[2]*10+n[3];
			N=L-S;
			cnt++;
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
 
const std::string answer="6174";
 
int main()
{
    std::string in_str;
        std::cin>>in_str;
        int L,S;
        int i;
        while(in_str[0]!='0'||in_str[1]!='0'||in_str[2]!='0'||in_str[3]!='0'){
                if(in_str[0]!=in_str[1]||in_str[1]!=in_str[2]||in_str[2]!=in_str[3]){
                        for(i=0;in_str!=answer;i++){
                {
                    std::stringstream sss;
                                std::sort(in_str.begin(),in_str.end());
                                sss<<in_str;
                                sss>>S;
                }
                {
                    std::stringstream sss;
                                std::sort(in_str.begin(),in_str.end(),std::greater<char>());
                                sss<<in_str;
                                sss>>L;
                }
                L-=S;
                {
                    std::stringstream sss;
                                sss<<L;
                                in_str=sss.str();
                    while(in_str.length()!=4){
                        in_str='0'+in_str;
                    }
                }
                        }
                        std::cout<<i<<std::endl;
                }else{
                        std::cout<<"NA"<<std::endl;
                }
                std::cin>>in_str;
        }
}

C++: #include <vector>
#include <algorithm>
#include <iostream>
#include <functional>

using namespace std;

int func2(int *temp) {
    int n = 0;
    
    n += (temp[0] * 1000);
    n += (temp[1] * 100);
    n += (temp[2] * 10);
    n += temp[3];
       
    return n;
}

int main() {
    while (true) {
        int N = 0, Nm[4] = {0};
        unsigned int count = 0;
        int judge = 0;
        int Lm[4] = {0}, Sm[4]={0};
        int S = 0, L = 0;
        
	cin >> N;
        
        Nm[0] = N / 1000;
        N %= 1000;
        Nm[1] = N / 100;
        N %= 100;
        Nm[2] = N / 10;
        N %= 10;
        Nm[3] = N;
        
        if((Nm[0] == 0 && Nm[1] == 0) && (Nm[2] == 0 && Nm[3] == 0))          break;
        
        for(int i = 1; i < 4; i++) {
            if (Nm[i] == Nm[0]) judge++;
        }
        
        if (judge != 4) {
            while (true) {
                if ((Nm[0] == 6 && Nm[1] == 1) && (Nm[2] == 7 && Nm[3] == 4)) break;
                
                //Sを作成
                sort(Nm, Nm + 4);
                for (int i = 0; i < 4; i++) {   
                    Sm[i] = Nm[i];
                    //cout << Sm[i] << endl;
                }   
                S = func2(Sm);

                //Lを作成
                sort(Nm, Nm + 4, greater<int>());
                for (int i = 0; i < 4; i++) {
                    Lm[i] = Nm[i];
                }
                L = func2(Lm);

                N = L - S;
                count++;

                Nm[0] = N / 1000;
                N %= 1000;
                Nm[1] = N / 100;
                N %= 100;
                Nm[2] = N / 10;
                N %= 10;
                Nm[3] = N;
            }
            cout << count << endl;
        }
        
        if(judge == 4) {
            cout << "NA" << endl;
        }
    }
    return 0;
}

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

char getc(int n){
    return '0' + n;
}

char geti(char c){
    return c - '0';
}

string getstr(int n){
    return string() + getc(n/1000) + getc(n/100%10) + getc(n/10%10) + getc(n%10);
}

int toint(string str){
    return geti(str[0])*1000+geti(str[1])*100+geti(str[2])*10+geti(str[3]);
}

int getl(int n){
    string str = getstr(n);
    sort(str.begin(),str.end());
    reverse(str.begin(),str.end());
    return toint(str);
}

int gets(int n){
    string str = getstr(n);
    sort(str.begin(),str.end());
    return toint(str);
}

int solve(int n){
    if(n == 6174) return 0;
    string str = getstr(n);
    if(str[0] == str[1] && str[1] == str[2] && str[2] == str[3])
        return -1;
    int i;
    for(i=0;n != 6174;i++){
        int l = getl(n);
        int s = gets(n);
        n = l-s;
        //cout << "--" << i << endl;
        //cout << "l:" << l << endl;
        //cout << "s:" << s << endl;
        //cout << "n:" << n << endl;
    }
    return i;
}

int main(){
    while(true){
        int n;
        cin >> n;
        if(n == 0)break;
        int ans = solve(n);
        if(ans != -1){
            cout << ans << endl;
        }else{
            cout << "NA" << endl;
        }
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
int main( ){
  while(1){
    int b[4],l,s,sum,count=0;
    string a;
    cin>>a;
    if(a[0]=='0'&&a[1]=='0'&&a[2]=='0'&&a[3]=='0')break;
    if(a[0]==a[1]&&a[2]==a[3]&&a[0]==a[3]){
      printf("NA\n");
      continue;
    }
    b[0]=a[0]-'0';
    b[1]=a[1]-'0';
    b[2]=a[2]-'0';
    b[3]=a[3]-'0';
    while(1){
      sum=b[0]*1000+b[1]*100+b[2]*10+b[3];
      if(sum==6174)break;
      count++;
      sort(b,b+4);
      s=b[0]*1000+b[1]*100+b[2]*10+b[3];
      l=b[3]*1000+b[2]*100+b[1]*10+b[0];
      sum=l-s;
      b[0]=sum/1000;
      b[1]=(sum%1000)/100;
      b[2]=(sum%100)/10;
      b[3]=sum%10;
    }
    printf("%d\n",count);
  }
  return 0;
}