Go: package main

import (
	"fmt"
	"sort"
	"strconv"
)

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

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

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <iterator>
using namespace std;

#define DEBUG(x) cerr << #x << " = " << x << endl

template<class T, class U> T lexical_cast(const U &from) {
    stringstream ss;
    ss << from;
    T to;
    ss >> to;
    return to;
}

bool same(const string &s) {
    return s[0] == s[1] && s[0] == s[2] && s[0] == s[3];
}

int main() {
    for(string S; cin >> S, S != "0000"; ) {
        if(same(S)) {
            cout << "NA" << endl;
            continue;
        }
        int ans = 0;
        while(S != "6174") {
            sort(begin(S), end(S));
            string L = "    ";
            for(int i = 0; i < 4; i++) L[i] = S[3 - i];
            S = lexical_cast<string>(lexical_cast<int>(L) - lexical_cast<int>(S));
            ans++;
        }
        cout << ans << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int a;
  int b=0;
  int c[8],d[8];
  int h,l;
  while(true){
    b=0;
    cin>>a;
    if(a==0000){
      break;
    }
    while(true){
      if(a==6174){
	cout<<b<<"\n";
	break;
      }
      if(a==0){
	cout<<"NA\n";
	break;
      }
      for(int i=0;i<4;i++){
	c[i]=a%10;
	a=a/10;
	d[i]=c[i];
      }
      sort(c,c+4);
      sort(d,d+4,greater<int>());
      for(int i=0;i<4;i++){
	if(i==0){
	h=c[i]*1000;
	l=d[i]*1000;
	}
	if(i==1){
	   h=c[i]*100+h;
	   l=d[i]*100+l;
	}
	if(i==2){
	  h=c[i]*10+h;
	  l=d[i]*10+l;
	}
	if(i==3){
	  h=c[i]+h;
	  l=d[i]+l;
	}
      }
      a=l-h;
      b++;
    }
  }
  return(0);
}

C++: #include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, n)  FOR(i, 0, n)

int main()
{
    int num;

    while (cin >> num, num) {
        if (!(num % 1111)) {
            cout << "NA" << endl;
        }
        else {
            int cnt = 0;
            while (num != 6174){
                int L = 0, S = 0, n[4];
                REP (i, 4) {
                    n[i] = num % 10;
                    num /= 10;
                }
                sort(n, n + 4);
                REP (i, 4) {
                    L = L * 10 + n[3 - i];
                    S = S * 10 + n[i];
                }
                cnt++;
                num = L - S;
            }
            cout << cnt << endl;
        }
    }
    return (0);
}

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

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

int rec(string N, int cnt) {
  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];
  }
  return (L - S == 6174) ? cnt + 1 : rec(to_string(L - S), cnt + 1);
}

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 if (N == "6174") cout << ("0");
    else cout << rec(N, 0);
    cout << "\n";
  }

  return (0);
}

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

C++: #include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    int N,S = 0;
   while(1)
   {
       cin >> N;
       int big[4] = {0},small[4],ans = 0;
       if(N == 0000)break;
       if(N % 1111 == 0)
       {
           puts("NA");
       }
       else
       {
           while(N != 6174)
           {


               big[0] = small[0] = N / 1000;
               big[1] = small[1] = (N - (big[0]*1000)) / 100;
               big[2] = small[2] = (N - (big[0]*1000 + big[1]*100)) / 10;
               big[3] = small[3] = N % 10;
               /*for(int i = 0; i < 4; i++)
               {
                   cout << big[i] << "??¨" << small[i] << endl;
               }*/
               for(int i = 0; i < 4; i++)
               {
                   for(int j = i+1; j < 4; j++)
                   {
                       if(big[i] > big[j])swap(big[i],big[j]);
                       if(small[i] < small[j])swap(small[i],small[j]);
                   }
               }
               N = 0; S = 0;
               int aaa = 1;
               for(int i = 0; i < 4; i++)
               {

                   S += small[i]*aaa;
                   N += big[i]*aaa;
                   aaa = aaa*10;
               }
               N -= S;
               ans++;
           }
           cout << ans << endl;
       }

   }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cstdlib>

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

using namespace std;

int main()
{
	string s;
	while(cin >> s)
	{
		if(s == "0000") break;
		
		bool flag = true;
		rep(i,s.size()) if(s[0] != s[i]) flag = false;

		if(flag)
		{
			cout << "NA" << endl;
		}
		else
		{
			int cnt = 0;
			while(true)
			{
				if(s == "6174")
				{
					cout << cnt << endl;
					break;
				}
			
				string temp = s,temp2 = s;
				sort(temp.begin(),temp.end());
				sort(temp2.begin(),temp2.end(),greater<char>());

				int t = atoi(temp.c_str());
				int t2 = atoi(temp2.c_str());

				stringstream ss;
				ss << t2 - t;

				s = "";
				REP(i,ss.str().size(),4) s += "0";

				s += ss.str();
				cnt++;
			}
		}

	}
}