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>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <sstream>
#include <string>
#include <map>
#include <set>
using namespace std;

#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)
#define all(c) (c).begin(), (c).end()

int main(){
    int a;
    while(cin >> a && a){
        if(a%1111==0){
            printf("NA\n");
            continue;
        }
        int ans=0;
        while(a!=6174){
            int d[4];
            rep(i,4){
                d[i]=a%10;
                a/=10;
            }
            sort(d,d+4);
            int b=0;
            rep(i,4){
                b*=10;
                b+=d[i];
            }
            int c=0;
            for(int i=3;i>=0;i--){
                c*=10;
                c+=d[i];
            }
            a=c-b;
            ans++;
        }
        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <sstream>
#include <vector>
#include <map>
#include <cassert>
using namespace std;

int main(){
	string s;
	while(cin >> s && s != "0000"){
		if( count(s.begin(),s.end(),s[0]) == 4 ){
			cout << "NA" << endl;
			continue;
		}
		int cnt = 0;
		while( s != "6174" ){
			string l,g;
			l=g=s;
			sort(l.begin(),l.end());
			sort(g.rbegin(),g.rend());
			int a = atoi(l.c_str());
			int b = atoi(g.c_str());
			int r = b - a;
			char c[10];
			sprintf(c,"%04d",r);
			s = string(c);
			cnt++;
		}
		cout << cnt << endl;
	}
}

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

int main(){
	int num[4],l,s,aa=0;
	string n;
	while(cin>>n,n!="0000"){
		for(int i=0;i<4;i++)num[i]=n[i]-'0';
		aa=0;
		if(num[0]==num[1]&&num[1]==num[2]&&num[2]==num[3]){
			cout<<"NA"<<endl;
			continue;
		}
		while(num[0]!=6&&num[1]!=1&&num[2]!=7&&num[3]!=4){
			l=0,s=0;
			sort(num,num+4);
			//for(int i=0;i<4;i++)cout<<num[i];
			//cout<<endl;
			s=num[0]*1000+num[1]*100+num[2]*10+num[3];
			l=num[3]*1000+num[2]*100+num[1]*10+num[0];
			num[0]=(l-s)/1000;
			num[1]=(l-s)/100%10;
			num[2]=(l-s)/10%10;
			num[3]=(l-s)%10;
			aa++;
		}
		cout<<aa<<endl;
	}
}

C++: // 0259
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int my_pow(int x, int y){
	int z = 1;
	for(int i=0;i<y;i++)
		z *= x;
	return z;
}

int main(){
	while(1){
		string str;
		int N = 0, count = 0;
		vector<int> num;

		cin>>str;
		if(str == "0000")
			break;

		num.resize(4);
		for(int i=0;i<4;i++){
			num[i] = str[i]-'0';
			N += num[i]*my_pow(10, 3-i);
		}

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

		while(N != 6174){
			for(int i=0;i<4;i++){
				num[i] = N%10;
				N /= 10;
			}

			sort(num.begin(), num.end());

			int L=0, S=0;
			for(int i=0;i<4;i++){
				S += num[i]*my_pow(10, 3-i);
				L += num[i]*my_pow(10, i);
			}

			N = L-S;
			count++;
		}

		cout<<count<<endl;
	}
	return 0;
}

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

using namespace std;

bool check(string &s)
{
    return (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]);
}

int main()
{
    string N;
    while (cin >> N, N != "0000") {
        int cnt = 0;
        bool fail = 0;
        while (N != "6174") {
            if (check(N)) {
                fail = 1;
                break;
            }                
            string L = N, S = N;
            sort(L.begin(), L.end(), greater<char>());
            sort(S.begin(), S.end());
            auto diff = stoi(L) - stoi(S);
            N = to_string(diff);
            cnt++;            
        }
        if (fail) {
            cout << "NA" << endl;
        } else {
            cout << cnt << endl;
        }
    }        
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
int main(){
	string a;
	while (cin >> a&&a != "0000") {
		int b[4], l = 0, s = 0, f = atoi(a.c_str()), sum = 0;
		if (a[0] != a[1] && a[1] != a[2] && a[2] != a[3]) {
			while (f != 6174) {
				a = to_string(f);
				for (int i = 0; i < 4; i++) {
					b[i] = (int)a[i] - '0';
				}
				sort(b, b + 4);
				for (int i = 3; i >= 0; i--) {
					l += (pow(10, i)*b[i]);
				}
				for (int i = 0; i < 4; i++){
					s += (pow(10,3 - i)*b[i]);
				}
				f = l - s;
				sum++;
				l = 0;
				s = 0;
			}
			cout << sum << endl;
		}
		else 
			cout << "NA" << endl;
	}
}

C++: #include "stdio.h"
#include "math.h"

#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

const int INF = 10000000;
using namespace std;
typedef long long  ll;

/** Problem0259 : All Numbers Lead to 6174 **/
int main()
{
	string s;
	int  min, max, num[4], n;
	
	while (cin>>s, s!="0000") {
		n=1;
		if (s=="6174") {
			cout << 0 << endl;
			continue;
		} else if (s[0]==s[1] && s[0]==s[2] && s[0]==s[3]) {
			cout << "NA" << endl;
			continue;
		}
		while (1) {
			min=0; max=0;
			fill(num, num+4, 0);
			rep(i, s.length()) {
				num[i] = s[i]-'0';
			}
			sort(num, num+4);
			rep(i, 4) {
				min*=10;
				min+=num[i];
			}
			reverse(num, num+4);
			rep(i, 4) {
				max*=10;
				max+=num[i];
			}
			int nn = max-min;
			if (nn == 6174) {
				cout << n << endl;
				break;
			} else {
				s="";
				rep(i, 4) {
					s+= nn%10;
					nn/=10;
				}
			}
			n++;
		}
	}
	
	return 0;
}

C++: #include<iostream>
using namespace std;
int main()
{
    int input = 0;
    int digit[4];
    int max, min;
    int base[4]={1000,100,10,1};
    int c=0,i,j,tmp;

    while(input != 6174)
    {
        cin >> input;
        if(input == 0) return 0;
        if(input % 1111 == 0){
	    cout << "NA" << endl;
	    return 0;
        }
        for(i=0; i<4; i++) // 各桁を取り出す
        {
            digit[i]= input / base[i];
            input = input % base[i]; // Highestを除く
        }
        for(i=0;i<3;i++) // sort
        {
            for(j=i+1;j<4;j++)
            {
                if(digit[i]<digit[j])
                {
                    tmp=digit[i];
                    digit[i]=digit[j];
                    digit[j]=tmp;
                }
            }
        }
	max = min = 0;
        for(i=0;i<4;i++)
        {
            max += (digit[i]*base[i]);
            min += (digit[i]*base[3-i]);
        }
        input = max-min;
        c++;
    }
    cout << c << endl;
    return 0;
}