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 <queue>
#include <vector>
#include <algorithm>
#include <vector>

std::string sub(const std::string& a, const std::string& b) {
    int a_ = 0, b_ = 0;

    int k = 1;
    for(int i = 3; i >= 0; --i) {
        a_ += k * (a[i] - '0');
        b_ += k * (b[i] - '0');

        k *= 10;
    }

    return std::to_string(a_ - b_);
}

int func(int i, const std::string& s) {
    if(s == "6174") {
        return i;
    }

    auto a = s, b = s;

    std::sort(a.begin(), a.end());
    std::sort(b.begin(), b.end(), std::greater<char>());

    return func(i + 1, sub(b, a));
}

inline
bool same(const std::string& a) {
    bool same = true;

    for(int i = 1; i < a.size(); ++i) {
        if(a[i - 1] != a[i]) {
            same = false;
        }
    }

    return same;
}

int main() {
    std::string  n;

    while(std::cin>> n, n != "0000") {
        if(same(n)) {
            std::cout << "NA" << std::endl;

            continue;
        }

        std::cout << func(0, n) << std::endl;
    }
}

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

bool isSameNumber(vector<int> n){
    bool isSame = true;
    for(int i=0; i < n.size(); i++){
        for(int j=0; j < n.size(); j++){
            if(n[i] != n[j])isSame = false;
        }
    }
    return isSame;
}

int main(){
    vector<int> n;
    int input;
    int ans = 0;
    while(cin >> input){
        bool isok = true;
        if(log10(input) < 3) n.push_back(0);
        if(input == 0) break;
        while(input != 6174) {
            while(input != 0){
                n.push_back(input%10);
                input /= 10;
            }
            if(isSameNumber(n)){
                cout << "NA" << endl;
                n.clear();
                isok = false;
                break;
            }
            vector<int> minNum ,maxNum;

            sort(n.begin(), n.end());
            minNum = n;
            reverse(n.begin(), n.end());
            maxNum = n;

            int p=0, q=0;
            for(int i=0, m = 1000; i < 4; i++, m/=10){
                p += minNum[i]*m;
                q += maxNum[i]*m;
            }
            input = q-p;
            ans++;

            n.clear();
        }
        if(isok)cout << ans << endl;
    }
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <complex>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <iterator>

using namespace std;

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define iter(c) __typeof__((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INFTY=1<<29;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

int solve(int n,vi& memo)
{
	if(memo[n]!=-1) return memo[n];
	if(n==6174) return memo[n]=0;
	
	char s1[5]; sprintf(s1,"%04d",n); sort(s1,s1+4);
	char s2[5]; rep(i,4) s2[i]=s1[3-i];
	if(atoi(s1)==atoi(s2)) return -1;
	return memo[n]=1+solve(atoi(s2)-atoi(s1),memo);
}

int main()
{
	vi memo(10000,-1);
	for(int n;cin>>n,n;){
		int res=solve(n,memo);
		if(res==-1) cout<<"NA"<<endl;
		else cout<<solve(n,memo)<<endl;
	}
	
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int N;
int high;
int low;
int COU;
int KU[4]={0};
int JKK(int a,int b);
int main(){
	while (1){
		COU=0;
		cin>>N;
		if (N==0) break;
		if (N%1111==0){
			cout<<"NA"<<endl;
			continue;
		}
		while(N!=6174){
			COU++;
			KU[0]=JKK(10,N);
			KU[1]=JKK(100,N);
			KU[2]=JKK(1000,N);
			KU[3]=N/1000;
			sort(KU,KU+4);
			high=KU[3]*1000+KU[2]*100+KU[1]*10+KU[0];
			low=KU[3]+KU[2]*10+KU[1]*100+KU[0]*1000;
			N=high-low;
		}
		cout<<COU<<endl;
		N=0;
		COU=0;
		
	}
	return 0;
}
int JKK(int a,int b){
	int r;
	r=b%a;
	if(a==100) r/=10;
	else if(a==1000) r/=100;
	return r;
}

C++: #include<cstdio>
#include<algorithm>
#include<iostream>

#define reps(i,f,n) for(int i = f; i < int(n); i++)
#define rep(i,n) reps(i,0,n)
using namespace std;

int to[10000];
int memo[10000];

void init(){
	int L,S;
	char rstr[8];
	char str[8];
	rep(i,10000)memo[i] = -1;
	rep(i,10000){
		sprintf(rstr,"%04d", i);
		sprintf(str,"%04d", i);
		sort(rstr,rstr+4);
		sort(str,str+4);
		reverse(str, str+4);
		L = atoi(str);
		S = atoi(rstr);
		to[i] = L-S;
	}
}

int main(void){
	int n;
	init();
	
	while(scanf("%d", &n),n){
		int ans = 0;
		int n_ = n;
		if(to[n] == 0){
			puts("NA");
			continue;
		}
		if(memo[n] != -1){
			ans = memo[n];
		}else{
			while(n != 6174){
				n = to[n];
				ans++;
			}
			memo[n_] = ans;
		}
		printf("%d\n", ans);
	}
	
	return 0;
}

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

	using namespace std;
	int main()
{
	int a,n[4],L=0,S=0,i,c=0;
	scanf("%d",&a);
	
	while(a!=0000){
		c=0;
		while(a!=6174){
			if(a==1111||a==2222||a==3333||a==4444||a==5555||a==6666||a==7777||a==8888||a==9999){
				
				break;
			}
		L=0;S=0;
		
		
		
		c++;
	n[0]=a/1000;
	n[1]=a/100-n[0]*10;
	n[2]=a/10-n[0]*100-n[1]*10;
	n[3]=a-n[0]*1000-n[1]*100-n[2]*10;
	sort(n,n+4);
	L+=n[3]*1000;
	L+=n[2]*100;
	L+=n[1]*10;
	L+=n[0];
	S+=n[0]*1000;
	S+=n[1]*100;
	S+=n[2]*10;
	S+=n[3];
	a=L-S;
	}
		
		if(a==1111||a==2222||a==3333||a==4444||a==5555||a==6666||a==7777||a==8888||a==9999){
			printf("NA\n");
		}
		else{
			printf("%d\n",c);
		}
	
	
	scanf("%d",&a);
	}
	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"){
			//	cout<<n<<endl;
				//cin>>t;
				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,l,s;
				l=1000*b[0]+100*b[1]+10*b[2]+b[3];
				s=1000*a[0]+100*a[1]+10*a[2]+a[3];
			//	cout<<s<<" "<<l<<endl;
				tmp=l-s;
				n=itos(tmp);
				ans++;
			}
		}
		if(flag)cout<<ans<<endl;
		else cout<<"NA"<<endl;
	}
	return 0;
}