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

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;
int stoi(string a){
		int b;
		stringstream ss;
		ss<<a;
		ss>>b;
		return b;
	}
	string itos(int a){
		stringstream ss;
		ss <<a;
		string b;
		b=ss.str();
		return b;
	}

int main(){
	string n;int t;
	while(cin>>n,n!="0000"){
		int ns=n.size();
		for(int i=0;i<4-ns;i++)n="0"+n;
		int ans=0;
		bool flag=true;
		if(n[0]==n[1]&&n[1]==n[2]&&n[2]==n[3])flag=false;
		
		if(flag){
			while(n!="6174"){
				ans++;
				//cout<<n<<endl;
				int a[4]={0},b[4]={0},ai,bi;
				for(int i=0;i<4;i++)a[i]=n[i]-'0',b[i]=n[i]-'0';
				sort(a,a+4);
				sort(b,b+4);
				reverse(b,b+4);
				int tmp=0;
				tmp=1000*b[0]+100*b[1]+10*b[2]+b[3];
				tmp-=1000*a[0]+100*a[1]+10*a[2]+a[3];
				n=itos(tmp);
			}
		}
		if(flag)cout<<ans<<endl;
		else cout<<"NA"<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <cmath>
#include <complex>
#include <cctype>
#include <cassert>
#include <sstream>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <deque>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;

const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS = 1e-10;

string to_str(int x) {
	stringstream ss;
	ss << x;
	return ss.str();
}

int main() {
	string N;
	
	while (cin >> N, N != "0000") {
		bool ng = true;
		REP(i, 3) if (N[i] != N[i + 1]) ng = false;
		
		if (ng) puts("NA");
		else {
			int ans = 0;
			
			while (N != "6174") {
				string s = N, l = N;
				
				sort(ALL(s));
				sort(ALL(l), greater<char>());
				
				string next = to_str(atoi(l.c_str()) - atoi(s.c_str()));
				
				reverse(ALL(next));
				while (next.size() < 4) next += '0';
				reverse(ALL(next));
				N = next;
				
				ans++;
			}
			
			cout << ans << endl;
		} 
	}
	
	return 0;
}