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.Println("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++: #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") {
		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));
			while (s.size() < 4)s = "0" + s;
			ans++;
		}
		cout << ans << endl;
	here:;

	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int n,i;

int solve(int num){

	if(num == 6174){
		return i;
	}

	i++;

	int k[5];
	k[0] = num/1000;
	k[1] = (num%1000)/100;
	k[2] = (num%100)/10;
	k[3] = num%10;

	sort(k,k+4);
	int s = k[0]*1000+k[1]*100+k[2]*10+k[3];

	reverse(k,k+4);
	int l = k[0]*1000+k[1]*100+k[2]*10+k[3];

	solve(l-s);

}



int main (){

	for(int d=0; d<10000; d++){
	
		i=0;

		cin >> n;
		if(n*1 == 0)break;

		if(n%1111 == 0){
			cout << "NA" << endl;
		}

		else{
			cout << solve(n) << endl;
	
		}
	}
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
template<typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& a){return is >> a.first >> a.second; }
template<typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1, T2>& a){ return os << a.first << " "<<a.second; }
template<typename T> istream& operator>>(istream& is, vector< T >& vc){ for(int i = 0; i < vc.size(); i++) is >> vc[i]; return is; }
template<typename T> ostream& operator<<(ostream& os, vector< T >& vc){ for(int i = 0; i < vc.size(); i++) os << vc[i] << endl; return os; }

main()
{
  string N;
  while(cin >> N, N != "0000") {
    if(count(N.begin(), N.end(), N[0]) == 4) {
      cout << "NA" << endl;
    } else {
      int ret = 0;
      for( ; N != "6174"; ret++) {
        string Sort1 = N, Sort2 = N;
        sort(Sort1.rbegin(), Sort1.rend());
        sort(Sort2.begin(), Sort2.end());
        int Value = atoi(Sort1.c_str()) - atoi(Sort2.c_str());
        stringstream sss; sss << Value;
        N = sss.str();
        while(N.size() < 4) N = "0" + N;
      }
      cout << ret << endl;
    }
  }
}

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);
			while(s.size() < 4)
			{
				s += '0';
			}
		}

		if(res == -1)
		{
			cout << "NA" << endl;
			continue;
		}
		cout << res << endl;
	}

}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int check(int n){
	int a[4];
	int s1=0,s2=0,s3;
	for(int i=0;i<4;i++){
		a[i]=n%10;
		n/=10;
	}
	for(int i=0;i<4;i++){
		for(int j=0;j<3;j++){
			if(a[j]<a[j+1])swap(a[j],a[j+1]);
		}
	}
	for(int i=0;i<4;i++){
		int j=3-i;
		s1*=10;   s2*=10;
		s1+=a[i]; s2+=a[j];
	}
	s3=s1-s2;
	return s3;
}
int s[10005];
int main(){
	int n;
	int i=0,j=0;
	while(cin>>n && n){
		for(int k=0;k<10000;k+=1111){
			if(n==k){
				s[i]=-1;
				break;
			}
		}
		if(s[i]==-1){
			i++;
			continue;
		}
		while(1){
			if(n==6174)break;
			j++;
			n=check(n);
		}
		s[i]=j;
		j=0;
		i++;
	}
	for(j=0;j<i;j++){
		if(s[j]==-1){
			cout<<"NA"<<endl;
		}else{
			cout<<s[j]<<endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;

int main(){
    int cnt,a,b;
    char num[5];
    while(1){
        cin >> num;
        cnt = 0;
        if(strcmp(num,"0000") == 0) break;
        if(num[0] == num[1] && num[1] == num[2] && num[2] == num[3]){
            cout << "NA" << endl;
            continue;
        }
        while(1){
            if(strcmp(num,"6174") == 0) break;
            cnt++;
            sort(num,num + 4);
            a = atoi(num);
            sort(num,num + 4,greater<char>());
            b = atoi(num);
            b -= a;
            sprintf(num,"%04d",b);
        }
        cout << cnt << endl;
    }
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str(); }
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 2;
const int MOD = 1000000007;

using namespace std;
typedef vector<ll> vl;
typedef vector<vl> vvl;

int main() {
	string s;
	while (cin >> s) {
		if (s == "0000") {
			break;
		}
		int ans = 0;
		if (toInt(s) % 1111 == 0) {
			cout << "NA" << endl;
		}
		else {
			while (s != "6174") {
				string a = s, b = s;
				stable_sort(ALL(a));
				stable_sort(RALL(b));
				s = toStr(toInt(b) - toInt(a));
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}