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<cstdio>
#include<cmath>
#include<vector>
#include<sstream>
#include<algorithm>
#define all(n) n.begin(),n.end()
using namespace std;

string itos(int i)
{
  stringstream ss;
  ss << i;
  return ss.str();
}

void EXAM(string N)
{
  int cnt = 0;
  while(true)
    {
      vector<int> mex,men;
      for(int i=0;i<N.size();i++)
	mex.push_back(N[i]-'0');
      sort(all(mex),greater<int>());
      men = mex;
      sort(all(men));
      
      int imex,imen;
      imex = imen = 0;
      for(int i=3;i>=0;i--)
	{
	  imex += mex[i]*pow(10,3-i);
      imen += men[i]*pow(10,3-i);
	}

      string pre = itos(imex-imen);      
      N = string(4-pre.size(),'0') + pre;
      //cout << "L = " << imex << " S = " << imen  << " L-S = " << N << endl;
      cnt++;
      if(N == "6174")
	break;
    }
  cout << cnt << endl;
}

int main()
{
  string N;
  while(true)
    {
      cin >> N;
      if(N == "0000")
	break;
      if(N == "1111" || N == "2222" || N == "3333" || N == "4444" || N == "5555" || N == "6666" || N == "7777" || N == "8888" || N == "9999")
	{
	  cout<< "NA" << endl;
	  continue;
	}
      if(N == "6174")
	{
	  cout << 0 << endl;
	  continue;
	}
      EXAM(N);

    }
  return 0;
}

C++: // 2015/07/21 Tazoe

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	while(true){
		int N;
		cin >> N;

		if(N==0)
			break;

		if(N%1111==0){			// 全桁が同じ数字
			cout << "NA" << endl;
			continue;
		}

		int cnt = 0;

		while(N!=6174){
			int n[4];
			n[0] = N/1000;		// 千の位
			n[1] = N/100%10;	// 百の位
			n[2] = N/10%10;		// 十の位
			n[3] = N%10;		// 一の位

			sort(n, n+4);		// ソート

			int L = n[3]*1000+n[2]*100+n[1]*10+n[0];
			int S = n[0]*1000+n[1]*100+n[2]*10+n[3];

			N = L-S;
			cnt++;
		}

		cout << cnt << endl;
	}

	return 0;
}

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

typedef long int lint;
typedef long long int llint;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
typedef pair<long, long> pll;

#define EACH(i, c) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, e) for (int i = (s); i < (int)(e); ++i)
#define REP(i, n) FOR(i, 0, n)
#define endl '\n'
#define mod 1000000007
#define INF 100000000
#define fk first
#define sv second

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, 1, 0, -1, 1, -1, 1, -1};

template<typename T>
void print(const std::vector<T>& v, char sep=' ') {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i != 0) cout << sep;
    cout << v[i];
  }
  cout << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;

  while (cin >> n){
    int ct = 0;
    if (n % 1111 == 0){
      cout << "NA\n";
      continue;
    }
    while (n != 6174){
      int l, s, num[4];
      REP(i, 4){
        num[i] = n % 10;
        n /= 10;
      }
      sort(n, n + 4);
      l = s = 0;
      REP(i, 4){
        s = n * 10 + num[3 - i];
        l = n * 10 + num[i];
      }
      n = l - s;
      ct++;
    }
    cout << ct << endl;
  }
}

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

int main()
{
    
    while(true){
        int N;
        cin>>N;
        
        if(N==0){
            break;
        }
        if(N%1111==0){
            cout<<"NA"<<endl;
            continue;
        }
        
        int cnt = 0;
        
        while(N!=6174){
            int n[4];
            n[0] = N/1000;
            n[1] = N/100%10;
            n[2] = N/10%10;
            n[3] = N%10;
            
            sort(n, n+4);
            
            int L=n[3]*1000+n[2]*100+n[1]*10+n[0];
            int S=n[0]*1000+n[1]*100+n[2]*10+n[3];
            
            N=L-S;
            cnt++;
        }
        
        cout<<cnt<<endl;
            
    }
    
    return 0;
}

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));
			ans++;
		}
		cout << ans << endl;
	here:;

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

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 <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <cstdio>
#include <cassert>

using namespace std;

int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

int main(void)
{
	string n;
	while (cin >> n && n != "0000"){
		if (count(n.begin(), n.end(), n[0]) == 4){
			cout << "NA" << endl;
		}
		// assert(n != "0000");

		int tmp = toInt(n);
		char str[10];
		sprintf(str, "%04d", tmp);
		n = string(str);
		int cnt = 0;
		while (n != "6174"){
			if (count(n.begin(), n.end(), n[0]) == 4){
				cout << "NA" << endl;
				goto END;
			}
			string L = n; sort(L.rbegin(), L.rend());
			string S = n; sort(S.begin(), S.end());
			// cout << n << " " << L << " " << S << endl;
			int tmp = toInt(L) - toInt(S);
			char str[10];
			sprintf(str, "%04d", tmp);
			n = string(str);
			cnt++;
		}
		cout << cnt << endl;
		END:;
	}

	return 0;
}