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

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
#define FOR(i,a,n) for(int (i)=(a);(i)<(n);(i)++)
#define eFOR(i,a,n) for(int (i)=(a);(i)<=(n);(i)++)
#define rFOR(i,a,n) for(int (i)=(n)-1;(i)>=(a);(i)--)
#define erFOR(i,a,n) for(int (i)=(n);(i)>=(a);(i)--)
#define SORT(i) sort((i).begin(),(i).end())
#define rSORT(i,a) sort((i).begin(),(i).end(),(a))
#define all(i) (i).begin(),(i).end()
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; }return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; }return 0; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T> inline istream& operator>>(istream& input, vector<T>& v) { for (auto& elemnt : v)input >> elemnt; return input; }

int main() {
    init();

    string in;
    while (1) {
        cin >> in;
        if (in == "0000")break;
        for (int i = 0;; i++) {
            if (in == "6174") {
                cout << i << "\n";
                break;
            }
            bool same = true;
            FOR(i, 0, 3)if (in[i] != in[i + 1])same = false;
            if (same) {
                cout << "NA\n";
                break;
            }
            string s = in, t = in;
            SORT(s), rSORT(t, greater<char>());
            in = to_string(stoi(t) - stoi(s));
            while (in.length() < 4)in = '0' + in;
        }
    }
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int d[4];
int main(){
    int a;
    while(scanf("%d",&a),a){
        if(a%1111==0){
            printf("NA\n");
            continue;
        }
        int count=0;
        while(a!=6174){
            for(int i=0;i<4;i++){
                d[i]=a%10;
                a/=10;
            }
            std::sort(d,d+4);
            int b=0;
            for(int i=0;i<4;i++){
                b*=10;
                b+=d[i];
            }
            int c=0;
            for(int i=3;i>=0;i--){
                c*=10;
                c+=d[i];
            }
            a=c-b;
            count++;
        }
        printf("%d\n",count);
    }
}

C++: #include<cstdio>
#include<algorithm>
#include<string>
#include<sstream>
#include<iostream>

#define reps(i,f,n) for(int i = f; i < int(n); i++)
#define rep(i,n) reps(i,0,n)
using namespace std;

int to[10000];

void init(){
	string str, rstr;
	int L,S;
	stringstream ss;
//	int i = 1000;{
	rep(i,10000){
		ss.clear();
		ss<<i;
		ss>>rstr;
		sort(rstr.begin(),rstr.end());
		str = rstr;
		reverse(str.begin(), str.end());
		ss.clear();
		ss << (str + " " + rstr);
		ss >> L >> S;
		to[i] = L-S;
	}
}

int main(void){
	int n;
	init();
	
	while(scanf("%d", &n),n){
		int ans = 0;
		if(to[n] == 0){
			puts("NA");
			continue;
		}
		while(n != 6174){
			n = to[n];
			ans++;
		}
		printf("%d\n", ans);
	}
	
	return 0;
}

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


bool allsame(int n){
  int tmp=n%10;
  n/=10;
  while(n!=0){
    if(tmp!=n%10){
      return 0;
    }
    tmp=n%10;
    n/=10;
  }
  return 1;
}

string toSortString(int n){
  stringstream ss;
  ss<<n;
  string tmp;
  ss>>tmp;
  sort(tmp.begin(),tmp.end());
  return tmp;
}

int make_l(string s){
  int res=0;
  for(int i=0;i<s.size();i++){
    res+=(s[i]-'0')*pow(double(10),i);
  }
  return res;
}

int make_s(string s){
  int res=0;
  for(int i=0;i<s.size();i++){
    res+=(s[s.size()-1-i]-'0')*pow(double(10),i);
  }
  return res;
}

int main(){
  int n;
  int ans=0;
  while(cin>>n,n!=0){
    while(n!=6174){
      if(allsame(n)){
        cout<<"NA"<<endl;
        break;
      }
      ans++;
      int l,s;
      string tmp_n=toSortString(n);
      l=make_l(tmp_n);
      s=make_s(tmp_n);
      n=l-s;
      assert(ans<=7);
    }
    if(n==6174){
      cout<<ans<<endl;
    }
  }
}

C++: #include <stdio.h>
#include <algorithm>
#include <vector>

using namespace std;

bool input(int a[]){
	int i, inp;
	scanf("%d",&inp);
	if(inp == 0)
		return false;
	for(i = 3;i >= 0;i--){
		a[i] = inp % 10;
		inp /= 10;
	}
	return true;
}
int main(void){
	int a[4], b[4], i, count;
	const int suc[4] = {6,1,7,4};
	while(input(a)){
		count = 0;
		while(1){
			for(i = 0;i < 4;i++)
				if(suc[i] != a[i]) break;
			if(i == 4){
				printf("%d\n",count);
				break;
			}
			for(i = 1;i < 4;i++)
				if(a[0] != a[i]) break;
			if(i == 4){
				printf("NA\n");
				break;
			}
			for(i = 0;i < 4;i++)
				b[i] = a[i];
			sort(a,a + 4,greater<int>());
			sort(b,b + 4);
			for(i = 0;i < 4;i++)
				printf("%d",b[i]);
			printf("\n");
			for(i = 3;i >= 0;i--){
				a[i] -= b[i];
				if(a[i] < 0){
					a[i - 1]--;
					a[i] += 10;
				}
			}
			count++;
		}
	}
	return 0;
}