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 <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

int main()
{
	int m, n[4], mi[4], ma[4], na;
	int	max_int = 0, min_int = 0, n_int, ans;
	bool same = false;
				
	while (1){
		ans = 0;
		
		scanf("%d", &n_int);

		for (int i = 3; i >= 0; i--){
			n[i] = n_int / (int)(pow(10, i)) % 10;
		}

		na = n[0];
		for (int i = 1; i < 4; i++){
			if (na != n[i]) same = true;
		}
		if (!same && na == 0) break;
		if (!same) cout << "NA" << endl;

		while (n_int != 6174){
			max_int = min_int = 0;
			if (!same) break;
			for (int i = 3; i >= 0; i--){
				n[i] = n_int / (int)(pow(10, i)) % 10;
			}
			sort(n, n + 4);

			for (int i = 0; i < 4; i++){
				mi[3 - i] = n[i];
				ma[i] = n[i];
			}

			for (int i = 0; i < 4; i++){
				max_int += ma[i] * (int)(pow(10, i));
				min_int += mi[i] * (int)(pow(10, i));
			}
			n_int = max_int - min_int;
			
			ans++;
		}

		if (same) cout << ans << endl;
	}
return (0);
}

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

using namespace std;

int main()
{
	string S;

	while (true)
	{
		cin >> S;

		if (S == "0000") { break; }

		if (S[0] == S[1] && S[1] == S[2] && S[2] == S[3])
		{
			cout << "NA" << endl;
		}
		else
		{
			int count_ = 0;

			while (S != "6174")
			{
				string S1 = S, S2 = S;

				sort(S1.begin(), S1.end(), greater<char>());
				sort(S2.begin(), S2.end());

				S = to_string(stoi(S1) - stoi(S2));

				while (S.size() < 4)
				{
					S = '0' + S;
				}

				count_++;
			}

			cout << count_ << endl;
		}
	}

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stdio.h>
#include <string>
#include <string.h>
#include <cstring>
#include <sstream>
#include <iomanip>


using namespace std;

int main (void)
{
	int y = 0;
	for (long u = 0; u < 10000; u++)
	{
		int co = 0;
		string a;
		int g[4];

		cin >> a;
		for (int k = 0; k < 100; k++)
		{
			if (a == "6174")
			{
				cout << co << endl;

				break;
			}
			if (a == "0000")
			{
				y = -1;
				break;
			}

			for (int i = 0; i < 4; i++)
			{
				g[i] = 0;
				g[i] = stoi (a.substr(i,1));
			}
			if (g[0] == g[1] && g[1] == g[2])
			{
				if (g[2] == g[3])
				{
					cout << "NA" << endl;
					break;
				}
			}
			long Max = 0,Min = 0;

			sort(g,g + 4);

			Min = (g[0] * 1000) + (g[1] * 100) + (g[2] * 10) + g[3];
			Max = (g[3] * 1000) + (g[2] * 100) + (g[1] * 10) + g[0];

			co += 1;
			if (Max - Min == 6174)
			{
				cout << co << endl;
				break;
			}
			ostringstream p;
			p << Max - Min;
			a = p.str();
		}
		if (y != 0)
		{
			break;
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

#define ALL(v) (v).begin(), (v).end()

signed main()
{
  string N;
  while (cin >> N, N != "0000") {
    if (N[0] == N[1] and N[2] == N[3] and N[0] == N[2]) cout << ("NA");
    else {
      int cnt = 0;
      while (N != "6174") {
        vector<char> LN(4), SN(4);
        int L = 0, S = 0;
        for (int i = 0; i < 4; i++) {
          LN[i] = SN[i] = N[i] - '0';
        }
        sort(ALL(LN));
        sort(ALL(SN));
        for (int i = 0; i < 4; i++) {
          L *= 10;
          S *= 10;
          L += LN[3 - i];
          S += SN[i];
        }
        cnt += 1;
        N = to_string(L - S);
      }
      cout << cnt;
    }
    cout << "\n";
  }

  return (0);
}

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

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int mem[10000];

int next(int n) {
  int a[4];
  rep (i, 4) {
    a[i] = n % 10;
    n /= 10;
  }
  sort(a, a + 4);
  int p = 0, q = 0;
  rep (i, 4) p = p * 10 + a[i];
  rep (i, 4) q = q * 10 + a[3 - i];
  return q - p;
}

int solve(int n) {
  if (n == 6174) return 0;
  if (mem[n] != 0) return mem[n];
  return mem[n] = solve(next(n)) + 1;
}

int main() {
  int n;
  rep (i, 10) mem[1111 * i] = -1;
  while (true) {
    cin >> n;
    if (n == 0) break;
    if (mem[n] == -1) {
      cout << "NA" << endl;
      continue;
    }
    if (mem[n] != 0) {
      cout << mem[n] << endl;
      continue;
    }
    cout << solve(n) << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int main(){
    while(true){
	string s;
	ll ans = 0;
	cin >> s;
	if(s == "0000") break;
	else if(s[0] == s[1] && s[0] == s[2] && s[0] == s[3]){
	    cout << "NA" << endl;
	    continue;
	}
	while(s != "6174"){
	    vector<int> a(4);
	    vector<int> b(4);
	    a[0] = (int)(s[0] - '0');
	    a[1] = (int)(s[1] - '0');
	    a[2] = (int)(s[2] - '0');
	    a[3] = (int)(s[3] - '0');
	    b[0] = (int)(s[0] - '0');
	    b[1] = (int)(s[1] - '0');
	    b[2] = (int)(s[2] - '0');
	    b[3] = (int)(s[3] - '0');
	    sort(ALL(b));
	    sort(RALL(a));
	    int an = 1000 * a[0] + 100 * a[1] + 10 * a[2] + a[3];
	    int bn = 1000 * b[0] + 100 * b[1] + 10 * b[2] + b[3];
	    s = to_string(an - bn);
	    ans++;
	}
	cout << ans << endl;
    }
    return 0;
}

C++: #include <stdio.h>
#include <algorithm>

using namespace std;

bool input(int a[]){
     int i, inp;
     scanf("%d",&inp);
     if(inp == 0) return false;
     for(i = 4;i >= 0;i--){
          a[i] = inp % 10;
          inp /= 10;
     }
     return true;
}

int main(void){
     int a[4], b[4], i, count;
     const int suc[4] = {6,1,7,4};
     while(input(a)){
          count = 0;
          while(1){
               for(i = 0;i < 4;i++)
                    if(suc[i] != a[i]) break;
               if(i == 4){
                    printf("%d\n",count);
                    break;
               }
               for(i = 1;i < 4;i++)
                    if(a[0] != a[i]) break;
               if(i == 4){
                    printf("NA\n");
                    break;
               }
               for(i = 0;i < 4;i++)
                    b[i] = a[i];
               sort(a,a + 4);
               sort(b,b + 4,greater<int>);
               for(i = 4;i >= 0;i--){
                    a[i] -= b[i];
                    if(a[i] < 0){
                         a[i - 1]--;
                         a[i] += 10;
                    }
               }
               count++;
          }
     }
     return 0;
}

C++: #include <iostream>
using namespace std;
 
int main() {
  int a,b,c,d,e,g,v,ans,i,j;
  char ci[4],x,z='0';
  while (true) {
	 cin >> ci;
	 if (ci[0]==z && ci[1]==z && ci[2]==z && ci[3]==z) break;
	 ans=0; g=0;
	 if (ci[0]==ci[1] && ci[1]==ci[2] && ci[2]==ci[3]) { cout << "NA" << endl; continue;}
	 if (ci[0]=='6' && ci[1]=='1' && ci[2]=='7' && ci[3]=='4') g=6174;
	 while(g!=6174) {
	 ans++;
	 for (i=2;i>=0;i--) for (j=0;j<=i;j++) if (ci[j]<ci[j+1]) { x=ci[j+1]; ci[j+1]=ci[j]; ci[j]=x;}
	 a=ci[0]-z; b=ci[1]-z; c=ci[2]-z; d=ci[3]-z;
	 e=1000*(a-d)+100*(b-c)+10*(c-b)+d-a; g=e;
	 for (i=0,v=1000;i<4;i++,v/=10) { ci[i]='0'+e/v; e-=(e/v)*v;} 
	 }
     cout << ans << endl;
  }	 
  return 0;
}