Go: package main

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

func main() {
	var v int
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Print("NA")
			continue
		}
		count := 0
		for v != 6174 {
			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))
			v = l - s
			count++
		}
		fmt.Println(count)
	}
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
	while (1) {
		int cnt = 0;
		int n;
		cin >> n;
		if (n == 0000) {
			return 0;
		}
		bool b = true;
		if (n%1111==0) {
			cout << "NA" << endl;
			b = false;
		}
		while (b) {
			if (n == 6174) {
				break;
			}
			string s;
			s += n / 1000;
			n -= (n / 1000) * 1000;
			s += n / 100;
			n -= (n / 100) * 100;
			s += n / 10;
			s += n - 10 * (n / 10);
			sort(s.begin(), s.end());
			int L = 0, S = 0;
			L = (int)s[3] * 1000 + (int)s[2] * 100 + (int)s[1] * 10 + (int)s[0];
			S = (int)s[0] * 1000 + (int)s[1] * 100 + (int)s[2] * 10 + (int)s[3];
			n = L - S;
			cnt++;
		}
		if(b) cout << cnt << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<sstream>
#include<queue>
using namespace std;
int latte(string str){
    stringstream ss(str);
    int n;
    ss>>n;
    return n;
}
struct data{
    string str;
    int n;
    data(string a,int b):str(a),n(b){}
    data(){}
};
int main(){
    string str="";
    while(cin>>str,str!="0000"){
        int memo[10000];
        fill(memo,memo+10000,-1);
        queue<data>Q;
        Q.push(data(str,0));
        while(!Q.empty()){
            data ins=Q.front();Q.pop();
            str=ins.str;int n=ins.n;
            if(memo[latte(str)]!=-1)continue;
            memo[latte(str)]=n;
            string a,b;
            a=b=str;
            sort(a.rbegin(),a.rend());sort(b.begin(),b.end());
            int to=latte(a)-latte(b);
            stringstream ss;
            ss<<to;
            string to_;
            ss>>to_;
            Q.push(data(to_,n+1));
        }
        if(memo[6174]==-1)cout<<"NA"<<endl;
        else cout<<memo[6174]<<endl;
    }
}

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

int main(){
	int num[4],l,s,aa=0;
	string n;
	while(cin>>n,n!="0000"){
		/*if(n>999){
			num[0]=n/1000;
			num[1]=n/100%10;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		if(n>99&&n<1000){
			num[0]=0;
			num[1]=n/100%10;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		if(n>9&&n<100){
			num[0]=0;
			num[1]=0;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		if(n<10){
			num[0]=0;
			num[1]=0;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		*/
		for(int i=0;i<4;i++)num[i]=n[i]-'0';
		aa=0;
		if(num[0]==num[1]&&num[1]==num[2]&&num[2]==num[3]){
			cout<<"NA"<<endl;
			continue;
		}
		while(num[0]!=6&&num[1]!=1&&num[2]!=7&&num[3]!=4){
			l=0,s=0;
			sort(num,num+4);
			for(int i=0;i<4;i++)cout<<num[i];
			cout<<endl;
			s=num[0]*1000+num[1]*100+num[2]*10+num[3];
			l=num[3]*1000+num[2]*100+num[1]*10+num[0];
			num[0]=(l-s)/1000;
			num[1]=(l-s)/100%10;
			num[2]=(l-s)/10%10;
			num[3]=(l-s)%10;
			aa++;
		}
		cout<<aa<<endl;
	}
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <iterator>
#include <functional>

using namespace std;

template<class T, class U> T lexical_cast(const U &from) {
    stringstream ss;
    ss << from;
    T to;
    ss >> to;
    return to;
}

bool same(const string &s) {
    return s[0] == s[1] && s[0] == s[2] && s[0] == s[3];
}

int main() {
    for(string S; cin >> S, S != "0000"; ) {
        if(same(S)) {
            cout << "NA" << endl;
            continue;
        }
        int ans = 0;
        while(S != "6174") {
            sort(begin(S), end(S));
            string L = "    ";
            for(int i = 0; i < 4; i++) L[i] = S[3 - i];
            S = lexical_cast<string>(lexical_cast<int>(L) - lexical_cast<int>(S));
            ans++;
        }
        cout << ans << endl;
    }
}

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;
        }
}