Go: package main

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

var memo []int

func solve(v int) int {
	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([]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 <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define P pair<int, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
const string END = "6174";

bool isNG(string s){
  REP(i, s.length()) if(s[0] != s[i]) return false;
  return true;
}

int make_L(string N){
  SORT(N);
  REVERSE(N);
  int ret = 0;
  REP(i, N.length()){
    ret *= 10;
    ret += (N[i] - '0');
  }
  return ret;
}

int make_S(string N){
  SORT(N);
  int ret = 0;
  REP(i, N.length()){
    ret *= 10;
    ret += (N[i] - '0');
  }
  return ret;
}

int main() {
  string N;
  while(cin >>N && N != "0000"){
    int cnt = 0;
    if(isNG(N)){
      cout <<"NA" <<endl;
      continue;
    }
    while(N != END){
    //REP(i, 10){
      int L = make_L(N);
      int S = make_S(N);
      //cout <<L <<", " <<S <<endl;
      stringstream ss; ss << (L - S);
      string s = ss.str();
      while(s.length() < 4) s = "0" + s;
      //cout <<s <<endl;
      N = s;
      ++cnt;
    }
    cout <<cnt <<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 <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int,int> pii;
#define REP(i,n) for(ll i=0;i<n;++i)
#define REPR(i,n) for(ll i=1;i<n;++i)
#define FOR(i,a,b) for(ll i=a;i<b;++i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define MOD (ll)(1e9+7)
#define ADD(a,b) a=((a)+(b))%MOD
#define FIX(a) ((a)%MOD+MOD)%MOD

int toint(string a){
  return atoi(a.c_str());
}
string tostr(int a){
  string ret = "";
  REP(_,4){
    ret = (char)(a%10+'0') + ret;
    a /= 10;
  }
  return ret;
}

int main(){
  while(true){
    string n;
    cin>>n;
    if(n == "0000")break;
    if(n[0]==n[1] && n[1]==n[2] && n[2]==n[3]){
      cout<<"NA"<<endl;
      continue;
    }
    int step = 0;
    while(n != "6174"){
      string l,s;
      sort(ALL(n));
      s = n;
      reverse(ALL(n));
      l = n;
      n = tostr(toint(l)-toint(s));
      ++step;
    }
    cout<<step<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int solve(int num,int& cnt) {
	if (num == 6174)return cnt;
	cnt++;
	int s[5];
	for (int i = 0; i < 4; i++) {
		s[i] = num % 10;
		num -= s[i];
		num /= 10;
	}
	sort(s, s + 4);
	return solve((s[3] - s[0]) * 1000 + (s[2] - s[1]) * 100 + (s[1] - s[2]) * 10 + (s[0] - s[3]), cnt);
}
int main() {
	int n;
	while (cin >> n) {
		if (!n)return 0;
		else if (n % 1111 == 0) {
			cout << "NA\n";
			continue;
		}
		int cnt;
		solve(n, cnt);
		cout << cnt << endl;
	}
}

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

int solve(int);

int main(){
	int N,ans=0;
	while(true){
		cin >> N;
		if(N == 0)
			break;
		if(N/1000 == (N%1000)/100 && (N%1000)/100 == (N%100)/10 && (N%100)/10 == N%10)
			cout << "NA" << endl;
		else{
			while(N != 6174){
				N = solve(N);
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

int solve(int N){
	int a[4],L,S;
	a[0] = N/1000;
	a[1] = (N%1000)/100;
	a[2] = (N%100)/10;
	a[3] = N%10;
	sort(a, a+4);
	S = a[3] + a[2]*10 + a[1]*100 + a[0]*1000;
	L = a[0] + a[1]*10 + a[2]*100 + a[3]*1000;
	return L-S;
}

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

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
  char c[4];
  while(cin>>c[0]>>c[1]>>c[2]>>c[3]){
    if(c[0]=='0'&&c[1]=='0'&&c[2]=='0'&&c[3]=='0') break;
    int su[4];
    int mems[4];
    int count = 0;
    int d = 1000;
    int h=1;
    for(int i=0;i<4;i++) su[i] = c[i] - '0';
    while(su[0]!=6||su[1]!=1||su[2]!=7||su[3]!=4){
      if(su[0]==0&&su[1]==0&&su[2]==0&&su[3]==0){
	cout << "NA" << endl;
	h = 0;
	break;
      }
      sort(su,su+4);
      for(int i=0;i<4;i++) mems[i] = su[i];
      sort(su,su+4,greater<int>());
      for(int i=0;i<4;i++) su[i] -= mems[i];
	for(int i=4;i>=1;i--){
	  if(su[i]<0){
	    su[i-1] -= 1;
	    su[i] += 10;
	  }
	}
	count++;
      }
    if(h==1) cout << count << endl;
  }
  return 0;
}