Go: package main

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

var memo []int8

func solve(v int) int8 {
	if v == 6174 {
		return 0
	}
	if memo[v] == 0 {
		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))
		memo[v] = solve(l-s) + 1
	}
	return memo[v]
}

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

C++: #include<stdio.h>
int main(void)
{
	int n,h[10],s[10],k[10],i,j,temp,flg,cnt;
	while(1){
		flg=0;
		cnt=0;
		scanf("%d",&n);
		if(n==0000)break;
		while(1){
			if(n==6174)break;
			if(n<1000){
				h[0]=0;
			}
			else{
				h[0]=n/1000;
			}
			if(n<100){
				h[1]=0;
			}
			else{
				h[1]=(n%1000)/100;
			}
			if(n<10){
				h[2]=0;
			}
			else{
				h[2]=((n%1000)%100)/10;
			}
			h[3]=(((n%1000)%100)%10)/1;
			if(h[0]==h[1]&&h[0]==h[2]&&h[0]==h[3]){
			flg=1;
			printf("NA\n");
			break;
			}
			cnt++;
//			printf("%d\n",cnt);
			for(i=0;i<4;i++){
				s[i]=h[i];
				k[i]=h[i];
			}
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(s[i]>s[j]){
						temp=s[i];
						s[i]=s[j];
						s[j]=temp;
					}
				}
			}
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(k[i]<k[j]){
						temp=k[i];
						k[i]=k[j];
						k[j]=temp;
					}
				}
			}
			n=(k[0]*1000+k[1]*100+k[2]*10+k[3])-(s[0]*1000+s[1]*100+s[2]*10+s[3]);
		}
		if(flg!=1){
			printf("%d\n",cnt);
		}
	}
	return 0;
}

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

void ssort(string& s,bool _greater){
    for(int i=0;i<4;i++){
        for(int j=i+1;j<4;j++){
            if(_greater){
                if(s[i]>s[j]){
                    swap(s[i],s[j]);
                }
            }
            else{
                if(s[i]<s[j]){
                    swap(s[i],s[j]);
                }
            }
        }
    }
}

int main() {

    string s;

    while (1) {

        cin >> s;

        if(s[0]==s[1]&&s[1]&&s[2]&&s[2]==s[3]&&s[3]==s[0]){
            if(s[0]=='0'){
                break;
            }
            cout << "NA" << endl;
            continue;
        }

        int ans = 0;

        while(1) {


            if (s == "6174") {
                cout << ans << endl;
                break;
            }

            ans++;

            string s1, s2;

            s1 = s2 = s;

            //sort(s1.begin(),s1.end());
            //sort(s2.begin(),s2.end(),greater<string>());

            ssort(s1, false);
            ssort(s2, true);

            int a1, a2;

            stringstream ss;

            ss << s1;
            ss >> a1;
            ss.clear();
            ss << s2;
            ss >> a2;
            a1 -= a2;

            ss.clear();

            ss << a1;

            ss >> s;

            if(s.size() != 4){
                while(s.size()!=4){
                    s = '0' + s;
                }
            }
        }

    }

    return 0;
}

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