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<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int main(){
    int n;
    while(scanf("%d",&n),n){
        int cnt = 0;
        bool flg = 0;
        while(1){
            if(n == 6174)break;
            if(n%1111 == 0){
                flg = 1;
                break;
            }
            int a[4]={0};
            a[0] = n/1000;
            a[1] = (n%1000)/100;
            a[2] = (n%100)/10;
            a[3] = n%10;
            int L[4],S[4];
            sort(a,a+4);
            for(int i = 0; i < 4; i++){
                L[3-i] = S[i] = a[i];
            }
            int l=0,s=0;
            for(int i = 0; i < 4; i++){
                l += L[3-i] * pow(10,i);
                s += S[3-i] * pow(10,i);
            }
            n = l - s;
            cnt++;
        }
        if(flg)puts("NA");
        else printf("%d\n",cnt);
    }
	return 0;
}

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

using namespace std;

string int_to_string(int n){
  stringstream ss;
  ss << n;
  return ss.str();
}

int string_to_int(const string& s){
  int n;
  istringstream iss(s);
  iss >> n;
  return n;
}

bool is_equal(const string& s){
  for(int i=1; i<s.size(); ++i){
    if(s[0]!=s[i]) return false;
  }
  return true;
}

int main(){
  int cnt;
  string s, max_, min_;
  while(1){
    cin >> s;
    if(s=="0000") break;
    if(is_equal(s)){
      cout << "NA" << endl;
      continue;
    }
    for(cnt=0; s!="6174"; ++cnt){
      min_ = s;
      sort(min_.begin(), min_.end());
      max_ = min_;
      reverse(max_.begin(), max_.end());
      s = int_to_string(string_to_int(max_)-string_to_int(min_));
    }
    cout << cnt << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)),UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a+mod-b)%mod
#define MUL(a, b) a = (a * b) % mod
#define repi(i,m,n) for(int i = m;i < n;i++)
#define drep(i,n,m) for(int i = n;i >= m;i--)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) repi(i,1,n+1)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define fi first
#define se second
typedef pair<int,int> P;
typedef pair<int, P> PP;
typedef pair<P, int> Pi;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9+7;
const int INF = 1e18+7;

int to_num(string s){
	return atoll(s.c_str());
}
string to_str(int num){
	string str = "";
	rep(i,4){
		str.pb((num%10)+'0');
		num /= 10;
	}
	reverse(all(str));
	return str;
}

signed main(){
	while(1){
		string str;
		cin >> str;
		if(str == "0000")break;
		if(str[0] == str[1] and str[1] == str[2] and str[2] == str[3])
		{cout << "NA" << endl;continue;}
		
		int ans = 0;
		while(1){
			if(str == "6174")break;
			string s1 = str, s2 = str;
			sort(all(s1), greater<int>());
			sort(all(s2));
			int num = to_num(s1)-to_num(s2);
			str = to_str(num);
			ans++;
		}
		printf("%lld\n", ans);
		
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(v, a, b) for(int v = (a); v < (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define UNWORDS(vec, str) for(int i = 0; i < (vec).size(); ++i) cout << (i ? str : "") << (vec)[i]
#define UNLINES(vec) for(int i = 0; i < (vec).size(); ++i) cout << (vec)[i] << endl
#define RS resize
#define CLR clear
#define PB push_back
using namespace std;

int main(){
	int n, cnt;
	
	int rep[] = {1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999};
	vector< int > repdig(rep, rep + (sizeof(rep) / sizeof(int)));
	
	while(cin >> n, n){
		
		if(find(repdig.begin(), repdig.end(), n) != repdig.end()){
			cout << "NA" << endl;
			continue;
		}
		
		cnt = 0;
		
		while(n != 6174){
			stringstream ss;
			ss << n;
			string str = ss.str();
			
			sort(str.begin(), str.end());
			
			int s = atoi(str.c_str());
			
			reverse(str.begin(), str.end());
			
			int l = atoi(str.c_str());
			
			n = l - s;
			++cnt;
		}
		cout << cnt << endl;
	}
	
	return 0;
}

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

int main(){
	int n;
	while( cin >> n, n ){
		int cnt = 0;
		while( n != 6174 ){
			int a[4] = { (n/1000), (n/100)%10, (n/10)%10, n%10 };
			if( a[0] == a[1] && a[1] == a[2] && a[2] == a[3] ){
				cout << "NA" << endl;
				cnt = -1;
				break;
			}
			sort( a, a+4 );
			int s = a[0]*1000 + a[1]*100 + a[2]*10 + a[3];
			int l = a[3]*1000 + a[2]*100 + a[1]*10 + a[0];
			n = l-s;
			cnt++;
		}
		if( cnt >= 0 )
			cout << cnt << endl;
	}
	return 0;
}

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

int main(){
    int n, d[4];
    while(cin >> n){
        if(n % 1111 == 0){
            cout << "NA" << endl;
            return 0;
        }else{
            int tmpn = n;
            for(int i = 3; i >= 0; i--){
                d[i] = tmpn%10;
                tmpn /= 10;
            }
            
            int ans = 0;
            while(n != 6174){
                ans++;
                sort(d, d+4);
                n = (1000*d[3] + 100*d[2] + 10*d[1] + d[0]) -
                        (1000*d[0] + 100*d[1] + 10*d[2] + d[3]);
                tmpn = n;
                for(int i = 3; i >= 0; i--){
                    d[i] = tmpn%10;
                    tmpn /= 10;
                }
            }
            
            cout << ans << endl;
        }
    }
    return 0;
}

C++: #include <vector>
#include <algorithm>
#include <iostream>
#include <functional>

using namespace std;

int func2(int *temp) {
    int n = 0;
    
    n += (temp[0] * 1000);
    n += (temp[1] * 100);
    n += (temp[2] * 10);
    n += temp[3];
       
    return n;
}

int main() {
    while (true) {
        int N = 0, Nm[4] = {0};
        int count = 0;
        int Lm[4] = {0}, Sm[4]={0};
        int S = 0, L = 0;
        
	cin >> N;
        
        Nm[0] = N / 1000;
        N %= 1000;
        Nm[1] = N / 100;
        N %= 100;
        Nm[2] = N / 10;
        N %= 10;
        Nm[3] = N;
        
        if (!((Nm[0] == Nm[1]) && (Nm[2] == Nm[3]))) {
            while (true) {
                if (Nm[0] == 6 && Nm[1] == 1 && Nm[2] == 7 && Nm[3] == 4) {
                    break;
                }

                //Sを作成
                sort(Nm, Nm + 4);
                for (int i = 0; i < 4; i++) {   
                    Sm[i] = Nm[i];
                }   
                S = func2(Sm);

                //Lを作成
                sort(Nm, Nm + 4, greater<int>());
                for (int i = 0; i < 4; i++) {
                    Lm[i] = Nm[i];
                }
                L = func2(Lm);

                N = L - S;
                count++;

                Nm[0] = N / 1000;
                N %= 1000;
                Nm[1] = N / 100;
                N %= 100;
                Nm[2] = N / 10;
                N %= 10;
                Nm[3] = N;
            }
            cout << count << endl;
        }
        
        if(((Nm[0] == Nm[1]) && (Nm[2] == Nm[3]))) {
            cout << "NA" << endl;
        }
    }
    return 0;
}