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 <algorithm>
#include <stdlib.h>
#include <string>

using namespace std;

int simulate(string d){
  if(d[0]==d[1] && d[1]==d[2] && d[2]==d[3]) return -1;
  string l, s;
  for(int cnt=0; ; cnt++){
    if(d=="6174") return cnt;
    l = s = d;
    sort(l.begin(), l.end(), greater<char>());
    sort(s.begin(), s.end());
    int x = atoi(l.c_str()) - atoi(s.c_str());
    for(int i=0, p=1000;i<4;i++, p/=10){
      d[i] = '0' + x/p;
      x %= p;
    }
  }
}

int main(){
  string str;
  while(cin>>str && str!="0000"){
    int cnt = simulate(str);
    if(cnt>=0){
      cout<<cnt<<endl;
    }else{
      cout<<"NA"<<endl;
    }
  }
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
string S; int a[4], b, cnt;
int main() {
	while (true) {
		cin >> S;
		if (S == "0000") { break; }
		if (S[0] == S[1] && S[2] == S[3] && S[1] == S[2]) { cout << "NA" << endl; }
		else {
			b = stoi(S);
			cnt = 0;
			while (b != 6174) {
				cnt++;
				for (int i = 0; i < 4; i++) {
					a[i] = (b / (int)pow(10, i)) % 10;
				}
				sort(a, a + 4);
				b = (a[3] - a[0]) * 1000 + (a[2] - a[1]) * 100 + (a[1] - a[2]) * 10 + (a[0] + a[3]);
			}
			cout << cnt << endl;
		}
	}
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
bool NA(string s) {
	if (s == "1111" || s == "2222" || s == "3333" || s == "4444" || s == "5555" || s == "6666" || s == "7777" || s == "8888" || s == "9999")return true;
	else return false;
}
signed main() {
	string s;
	while (cin >> s, s != "0000") {
		if (NA(s))cout << "NA" << endl;
		else {
			int ans(0);
			while (s != "6174") {
				if (NA(s)) {
					cout << "NA" << endl;
					goto here;
				}
				string big(s), small(s);
				sort(all(small));
				sort(all(big), greater<char>());
				s = to_string(stoi(big) - stoi(small));
				ans++;
			}
			cout << ans << endl;
		here:;
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;

int main(){
	char c[5];
	while(scanf("%s",c),!(c[0]=='0'&&c[1]=='0'&&c[2]=='0'&&c[3]=='0')){
		if(c[0]==c[1]&&c[0]==c[2]&&c[0]==c[3]){ printf("NA\n"); continue;}
		int res=0;
		while(!(c[0]=='6'&&c[1]=='1'&&c[2]=='7'&&c[3]=='4')){
			char l[5],m[5];
			for(int i=0;i<5;i++) l[i]=m[i]=c[i];
			sort(l,l+4,greater<char>()); sort(m,m+4);
			int a=(l[0]-48)*1000+(l[1]-48)*100+(l[2]-48)*10+(l[3]-48);
			int b=(m[0]-48)*1000+(m[1]-48)*100+(m[2]-48)*10+(m[3]-48);
			int x=a-b;
			for(int i=0,p=1000;i<4;i++){ c[i]=(char)(48+x/p); x%=p; p/=10;}
			res++;
		}
		printf("%d\n",res);
	}
}

C++: #include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
	int n,tmp[4];
	while(true){
		cin>>n;
		int ans=0;
		if(n==0000)break;
		while(true){
			tmp[0]=n/1000;
			tmp[1]=(n/100)-10*tmp[0];
			tmp[2]=(n/10)-100*tmp[0]-10*tmp[1];
			tmp[3]=n%10;
			sort(tmp,tmp+4);
			int l=tmp[3]*1000+tmp[2]*100+tmp[1]*10+tmp[0];
			int s=tmp[0]*1000+tmp[1]*100+tmp[2]*10+tmp[3];
			if(tmp[0]==tmp[3]){
				ans=-1;
				break;
			}else if(n==6174){
				break;
			}else{
				n=l-s;
				ans++;
			}
		}
		if(ans<0)printf("NA\n");
		else printf("%d\n",ans);
	}
	return 0;
}