Go: package main

import (
	"fmt"
	"sort"
)

var memo []int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	//if memo[v] == 0 {
	a := make([]int, 4)
	for i := 0; i < 4; i++ {
		a[i] = v % 10
		v /= 10
	}
	sort.Slice(a, func(i, j int) bool {
		return a[i] > a[j]
	})
	l := int(0)
	for i := 0; i < 4; i++ {
		l *= 10
		l += a[i]
	}
	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})
	s := int(0)
	for i := 0; i < 4; i++ {
		s *= 10
		s += a[i]
	}
	memo[v] = solve(l-s) + 1
	//}
	//return memo[v]
	return solve(l-s) + 1
}

func main() {
	var v int
	memo = make([]int, 10000)
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

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

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

int main() {
	int n;
	while(cin >> n && n){
		if(n % 1111 == 0){
			cout << "NA" << endl;
			continue;
		}
		
		int ans = 0, num = n;
		while(num != 6174){
			int l = 0, s = 0, b[4];
			for(int i = 0; i < 4; ++i){
				b[i] = num % 10;
				num /= 10;
			}
			sort(b, b + 4);
			for(int i = 3; i >= 0; --i){
				l += b[i] * pow(10, i);
				s += b[3 - i] * pow(10, i);
			}
			num = l - s;
			ans++;
		}
		cout << ans << endl;
	}
	return 0;
}