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]
	return solve(l-s) + 1
}

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<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#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 all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

signed main() {
	int A[4];
	for (string s; cin >> s&&s != "0000";) {
		int cnt = 0;
		if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
			cout << "NA" << endl;
			continue;
		}
		while (s != "6174") {
			cnt++;
			rep(i, 0, 4) {
				A[i] = s[i] - '0';
			}
			sort(A, A + 4);
			int L = 0, S = 0;
			rep(i, 0, 4) {
				L *= 10;
				S *= 10;
				L += A[3 - i];
				S += A[i];
			}
			s = to_string(L - S);
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include <stdio.h> // C++???????????????
#include <string.h> // c++???????????????
#include <math.h> // c++???????????????
#include <iostream>
#include <string>
#include <stack>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;

typedef long long int lli;
#define PI 3.1415926535897932
#define MAX(a,b) ((a>b)?(a):(b)) // ?????§????????????
#define MIN(a,b) ((a<b)?(a):(b)) // ????°?????????????
#define INRANGE(a,b,c) ((a<=b&&b<=c)?(1):(0)) // a<=b<=c??????????????????????????????
#define N 4

void swap(int &a,int &b){ // c??????*
	int w=a;
	a=b;
	b=w;
}

int getN(int num,int digit){ // ?????????????????°????????????
	int i=1,j;
	for(j=1;j<digit;i*=10,j++);
	return (num/i)%10;
}

int zeller(int y,int m,int d){ // ?????§??????
	if(m<3){y--; m+=12;}
	return (y+y/4-y/100+y/400+(13*m+8)/5+d)%7;
	// 0=sunday!
}

int gcd(int a,int b){	// ?????§??¬?´???°
	if(b==0) return a;
	return gcd(b,a%b);
}

int lcm(int a,int b){	// ????°???¬?????°
	return a*b/gcd(a,b);
}

int stoi(const string str){ // ?????????????????´??°
	int i=0,j;
	int len=str.length();
	int w=0;
	for(i=1,j=1;i<=len && INRANGE('0',str[len-i],'9');i++,j*=10){
		w+=(str[len-i]-'0')*j;
	}
	if(str[0]=='-')w*=-1; // ??????????????????
	return w;
}

string itos(int a){ // ??´??°???????????????
	int i;
	string str="";
	int keta;
	if(a<0){str+='-';a*=-1;} // ?????°??????????????????
	for(keta=10;a/keta!=0;keta*=10);
	for(i=keta/10;i!=0;i/=10){
		str+=((a/i)%10)+'0';
	}
	return str;
}

int main(void){
	string str;
	string str1,str2;
	int a,b; //a>b
	int c;
	while(cin>>str,str!="0000"){
		c=0;
		while(str!="6174"){
			for(int i=0;i<N;i++){
				for(int j=i+1;j<N;j++){
					if(str[i]<str[j])swap(str[i],str[j]);
				}
			}
			str1=str;
			for(int i=0;i<N/2;i++) swap(str[i],str[N-i-1]);
			str2=str;
			str=itos(stoi(str1)-stoi(str2));
			if(str=="0"){cout<<"NA"<<endl; goto LOOP;}
			c++;
		}
		cout<<c<<endl;
LOOP:;
	}
	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]) * 999 + (s[2] - s[1]) * 90, 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 <bits/stdc++.h>
using namespace std;
typedef long long ll;

pair<int, int> to_L_S(string s) {
    vector<int> a;
    for (int i = 0; i < s.length(); i++) {
        a.push_back((int)(s[i] - '0'));
    }
    while (a.size() <= 3) {
        a.push_back(0);
    }
    sort(a.begin(), a.end());
    int L = 0;
    int S = 0;
    int k[4] = {1, 10, 100, 1000};
    for (int i = 0; i < 4; i++) {
        L += (a[i] * k[i]);
        S += (a[i] * k[3 - i]);
    }
    return make_pair(L, S);
}

int main() {

    for (int i = 0; i < 1000000; i++) {
        string s;
        cin >> s;
        if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
            if (s[0] == '0') {
                return 0;
            }
            cout << "NA\n";
        } else if (s == "6174") {
            cout << "0\n";
        } else {
            pair<int, int> p = to_L_S(s);
            int cnt = 1;
            while (p.first - p.second != 6174) {
                cnt++;
                int N = p.first - p.second;
                // cout << p.first << " " << p.second << "\n";
                // cout << N << "\n";
                p = to_L_S(to_string(N));
            }
            cout << cnt << "\n";
        }

    }
    return 0;
}

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


bool allsame(string s){
  char c=s[0];
  for(int i=0;i<s.size();i++){
    if(s[i]!=c)return 0;
  }
  return 1;
}

string toSortString(int n){
  string res="0000";
  int tmp[4]={0,0,0,0};
  for(int i=0;n!=0;i++){
    tmp[i]=n%10;
    n/=10;
  }
  for(int i=0;i<4;i++){
    res[i]=tmp[i]+'0';
  }
  sort(res.begin(),res.end());
  return res;
}

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

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

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