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

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<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define ll2(x, y) ll x, y; cin >> x >> y;
#define scn(n, a) rep(i, n)cin >> a[i]
#define sc2n(n, a, b) rep(i, n)cin >> a[i] >> b[i]
#define pri(x) cout << (x) << "\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define endl "\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << (x) << "\n"
#define debug2(x, y) cout << " --- " << (x) << " " << (y) << "\n"
#define debug3(x, y, z) cout << " --- " << (x) << " " << (y) << " " << (z) << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

int solve(int n, int cnt){
  if(n == 6174)return cnt;
  int a[4] = {n % 10, (n / 10) % 10, (n / 100) % 10, n / 1000};
  sort(a, a + 4);
  int mi = a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3];
  int ma = a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0];
  return solve(ma - mi, cnt + 1);
}

signed main(void){
  int i, j;
  for(int testcase = 0;testcase >= 0;testcase++){
    int(n);
    if(!n)break;
    if(n % 1111 == 0)puts("NA");
    else pri(solve(n, 0));

//*/

//*/ break;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <stdlib>

using namespace std;

int main(){

	while(1){
		int n=0,l,s;
		int cnt = 0;
		string tmp;

		cin >> tmp;
		n = atoi(tmp.c_str());

		if(n == 0)break;
		if(n % 1111 == 0){
			printf("NA\n");
			continue;
		}

		while(n != 6174){
			sort(tmp.begin(),tmp.end());
			s = atoi(tmp.c_str());

			sort(tmp.begin(),tmp.end(),greater<char>());
			l = atoi(tmp.c_str());
			n = l - s;

			for(int i=0,p=1000;i<4;i++,p/=10){
				tmp[i] = '0' + n/p;
				n %= p;
			}
			n = atoi(tmp.c_str());
			cnt++;
		}
		printf("%d\n", cnt);
	}
	return 0;
}

C++: #include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
int digs[5];
char c[5];
string str;

bool comp(const int &a, const int &b) {
    return a > b;
}

int sortDigit(int num) {
    int idx = 0;
    while (num != 0) {
        digs[idx++] = num % 10;
        num /= 10;
    }
    sort(digs, digs+idx, comp);
    int sdigit = 0, p = 1;
    for (int i = idx-1; i >= 0; i--) {
        sdigit += p * digs[i];
        p*=10;
    }
    while (sdigit < 1000) sdigit *= 10;
    return sdigit;
}

int revNum(int num) {
    int revnum = 0;
    while (num != 0) {
        revnum = revnum * 10 + (num % 10);
        num /= 10;
    }
    return revnum;
}

bool isSameDig(int num) {
    int dig = num % 10;
    num /= 10;
    while (num != 0) {
        if (dig != num % 10) return false;
        num /= 10;
    }
    return true;
}

int main(){
    int base = 6174;
    int prev, L, S, steps = 0;
    for (; ;) {
        scanf("%d", &prev);
        if (prev == 0) break;
        steps = 0;
        bool flag = false;
        while (prev != base) {
            L = sortDigit(prev); S = revNum(L);
            if (isSameDig(L)) { flag = true; break; }
            prev = L - S;
            steps++;
        }
        if (!flag) printf("%d\n", steps);
        else printf("NA\n");
    }
    return 0;
}

C++: #include <stdio.h>
int main(void){
	int i,s,o,n[4],q[4],toread,L,S,A=0,a,b,c;
	for(i=0;i<=3;i++){
		scanf("%1d",&n[i]);
		q[i]=n[i];
	}
	if(n[0]==n[1]&&n[1]==n[2]&&n[2]==n[3]){
		printf("NA\n");
	}
	if(n[0]==6&&n[1]==1&&n[2]==7&&n[3]==4){
		printf("1\n");
	}
	else{
		for(s=0;A!=6174;s++){
		for(o=0;o<=3;o++){
			for(i=0;i<=3;i++){
				if(n[i]<n[o]){
					toread=n[i];
					n[i]=n[o];
					n[o]=toread;
				}
			}
		}
		L=n[0]*1000+n[1]*100+n[2]*10+n[3];
		for(o=0;o<=3;o++){
			for(i=0;i<=3;i++){
				if(n[i]>n[o]){
					toread=n[i];
					n[i]=n[o];
					n[o]=toread;
				}
			}
		}
		S=n[0]*1000+n[1]*100+n[2]*10+n[3];
		A=L-S;
		a=A/1000;
		n[0]=a;
		n[1]=(A-(n[0]*1000))/100;
		n[2]=(A-((n[0]*1000)+(n[1]*100)))/10;
		n[3]=A-((n[0]*1000)+(n[1]*100)+(n[2]*10));
		}
	printf("%d\n",s);
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int main(){
	int i,n,L[5],N[5],S[5],num=0;
	for(i=0;i<=4;++i){
		N[i]=0;
		L[i]=0;
		S[i]=0;
	}
	while(cin>>n,n){
		while(1){
			if (n==6174){
				cout<<"0"<<endl;
				break;
			}
			L[4]=N[4]=n/1000;
			L[3]=N[3]=n/100-N[4]*10;
			L[2]=N[2]=n/10-N[4]*100-N[3]*10;
			L[1]=N[1]=n-(n/10)*10;
			if (L[1]==L[2] && L[2]==L[3] && L[3]==L[4]){
				cout<<"NA"<<endl;
				break;
			}
			for(int j=1;j<=4;++j){
				for(i=4;i>=2;--i){
					if (L[i]<L[i-1]){
						L[0]=L[i];
						L[i]=L[i-1];
						L[i-1]=L[0];
					}
				}
			}
			for(int j=1,i=4;j<=4,i>=1;++j,--i){
					S[j]=L[i];
			}
			L[0]=L[4]*1000+L[3]*100+L[2]*10+L[1];
			S[0]=S[4]*1000+S[3]*100+S[2]*10+S[1];
			n=L[0]-S[0];
			num+=1;
			if (n==6174){
				cout<<num<<endl;
				num=0;
				break;
			}
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>  // g++ -std=c++14 -o a a.cpp

using namespace std;

typedef pair<int, int> P;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define repto(i, n) for(int i = 0; i <= (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end()) // ユニークなvectorにする。連続した値を除去して切り出す。
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
#define DEBUG(x) cout << #x << ": " << x << endl;

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int toInt(vector<int> i) {
  return i[0] * 1000 + i[1] * 100 + i[2] * 10 + i[3];
}

vector<int> toVec(int n) {
  vector<int> result;
  result.pb(n / 1000);
  result.pb((n / 100) % 10);
  result.pb((n / 10) % 10);
  result.pb(n % 10);
  return result;
}

vector<int> calc(vector<int> v) {
  sort(v.rbegin(), v.rend());
  int L = toInt(v);

  sort(v.begin(), v.end());
  int S = toInt(v);

  int N = L - S;
  return toVec(N);
}

int main() {
  int N;
  while (cin >> N) {
  if (N == 0) break;
    vector<int> input = toVec(N);

    if(input[0] == input[1] &&
      input[1] == input[2] &&
      input[2] == input[3] ) {
        cout << "NA" << endl;
    } else {
      int count = 0;
      while (toInt(input) != 6174) {
        count++;
        input = calc(input);
      }
      cout << count << endl;
    }
  }
}