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

C++: #include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <climits>
#include <algorithm>
#include <map>
#include <sstream>
using namespace std;

int toInt(string s) {
    stringstream ss(s);
    int ret;
    ss >> ret;
    return ret;
}

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

string next(string N) {
    sort(N.begin(), N.end());
    int S = toInt(N);
    reverse(N.begin(), N.end());
    int L = toInt(N);
    string ret = "0000" + toStr(L-S);
    return ret.substr((int)ret.size()-4);
}

int main() {
    string N;
    while (cin >> N, N != "0000") {
        if (N[0] == N[1] && N[1] == N[2] && N[2] == N[3]) {
            cout << "NA" << endl;
            continue;
        }
        int cnt = 0;
        while (N != "6174") {
            N = next(N);
            ++ cnt;
        }
        cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
string itos(int a){
	string s="";
	while(a){
		s=(char)(a%10+'0')+s;
		a/=10;
	}
	while(s.size()!=4)s="0"+s;
	return s;
}
int main(){
	string s;
	while(cin>>s,s!="0000"){
		int co=0;
		while(co!=1000){
			if(s=="6174")break;
			sort(all(s));
			string tmp=s;
			reverse(all(tmp));
			int sa=atoi(&tmp[0])-atoi(&s[0]);
			s=itos(sa);
			co++;
		}
		if(co!=1000)cout<<co<<endl;
		else cout<<"NA"<<endl;
	}
}

C++: #include <bits/stdc++.h>

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

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct
{
    int to, cost;
};

const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int next(int N)
{
    string S = to_string(N), L;

    while (S.size() < 4)
        S = "0" + S;

    sort(ALL(S));

    REP(i, 4)
    L += S[3 - i];

    return stoi(L) - stoi(S);
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N;

    while (cin >> N, N)
    {
        int ans = 0;

        for (;;)
        {
            if (N % 1111 == 0)
            {
                cout << "NA" << endl;
                break;
            }
            else if (N == 6174)
            {
                cout << ans << endl;
                break; 
            }

            N = next(N);

            ans++;
        }
    }

    return 0;
}