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

C++: #include <iostream>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;

void solve()
{
	string str;
	while(cin >> str)
	{
		if(str == "0000")
		{
			break;
		}
		else if(atoi(str.c_str()) % 1111 == 0)
		{
			cout << "NA" << endl;
			continue;
		}
		int count = 0;
		while(str != "6174")
		{
			sort(str.begin(), str.end());
			string small = str;
			string large;
			for(int i = str.size() - 1; i >= 0; --i)
			{
				large += str[i];
			}
			int num = atoi(large.c_str()) - atoi(small.c_str());
			stringstream ss;
			ss << num;
			str = ss.str();
			if(str.size() < 4)
			{
				string buffer = str;
				str.clear();
				for(int i = 0; i < 4 - buffer.size(); ++i)
				{
					str += "0";
				}
				str += buffer;
			}
			++count;
		}
		cout << count << endl;
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include<bits/stdc++.h>

using namespace std;

string N;


bool cmp(const char c1, const char c2) {
	return c1 > c2;
}

int solve() {
	int L, S, cnt;
	stringstream ss;

	cnt = 0;
	while (true) {
		if (N == "6174") {
			return cnt;
		}
		sort(N.begin(), N.end(), cmp);
		ss << N;
		ss >> L;
		//cout << L << " :output_L" << endl;
		ss.clear();
		sort(N.begin(), N.end());
		ss << N;
		ss >> S;
		//cout << S << " :output_S" << endl;
		ss.clear();
		ss << (L - S);
		ss >> N;
		ss.clear();
		while (N.size() < 4) {
			N = "0" + N;
		}
		//cout << "new:" << N << endl;
		++cnt;
	}
	
}

int main() {

	while (cin >> N, N != "0000") {
		if (N[0] == N[1] && N[1] == N[2] && N[2] == N[3]) {
			cout << "NA" << endl;
			continue;
		}
		cout << solve() << endl;
	}
	
	return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <complex>
#include <ctime>
#include <cstdlib>

using namespace std;

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

typedef long long ll;

int main()
{
	string s;
	while(cin >> s)
	{
		if(s == "0000")
		{
			break;
		}
		int res = 0;
		if(s == "6174")
		{
			cout << res << endl;
			continue;
		}
		for(int i = 1; ;i++)
		{
			string UP = s, LOW = s;
			sort(LOW.begin(), LOW.end());
			sort(UP.begin(), UP.end(), greater<char>());

			int up = to_int(UP), low = to_int(LOW);
			int mid = up - low;

			if(mid == 0)
			{
				res = -1;
				break;
			}
			if(mid == 6174)
			{
				res = i;
				break;
			}
			s = to_str(mid);
		}

		if(res == -1)
		{
			cout << "NA" << endl;
			continue;
		}
		cout << res << 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"){
		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;
	}
}