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]
	return solve(l-s) + 1
}

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<stdio.h>
int main(void)
{
	int n;
	int l,s;
	int i,j,temp;
	int keta[4];
	int cnt;
	
	scanf("%d",&n);
	
	while(n!=0000){
		if(n%1111==0){
			printf("NA\n");
			break;
		}
		while(n!=6174){
			for(i=0;i<4;i++){
				keta[i]=n%10;
				n=n/10;
			}
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(keta[i]<keta[j]){
						temp=keta[i];
						keta[i]=keta[j];
						keta[j]=temp;
					}
				}
			}
			l=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];

			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(keta[i]>keta[j]){
						temp=keta[i];
						keta[i]=keta[j];
						keta[j]=temp;
					}
				}
			}
			s=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];
			n=l-s;
			cnt++;
		}
	printf("%d\n",cnt);
	scanf("%d",&n);
	}

	return 0;
}

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

int main()
{
	int n[4];
	string str;

	while (cin >> str, str != "0000"){
		for (int i = 0; i < 4; i++){
			n[i] = str[i] - '0';
		}

		if (n[0] == n[1] && n[0] == n[2] && n[0] == n[3]){
			cout << "NA" << endl;
		}
		else {
			int c = 0;
			while (n[0] != 6 || n[1] != 1 || n[2] != 7 || n[3] != 4){
				
				char l[4], s[4];
				for (int i = 0; i < 4; i++){
					l[i] = s[i] = n[i];
				}

				sort(l, l + 4);
				reverse(l, l + 4);
				sort(s, s + 4);

				for (int i = 3; 0 <= i; i--){
					n[i] = l[i] - s[i];
					if (n[i] < 0){
						n[i] += 10;
						l[i - 1]--;
					}
				}
				c++;
			}
			cout << c << endl;
		}
	}
	return (0);
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
long n,d[4],l,s,k,i;
while(cin>>n,n){
for(k=0;n!=6174;k++){
for(i=0;i<4;i++){d[i]=n%10;n/=10;}
sort(d,d+4,greater<long>());
for(l=i=0;i<4;i++){l=l*10+d[i];}
sort(d,d+4);
for(s=i=0;i<4;i++){s=s*10+d[i];}
n=l-s;
if(n==0)break;
}
if(n==0)cout<<"NA"<<endl;else cout<<k<<endl;
}
return 0;
}

C++: // すべての数は6174に通ず.cpp : Defines the entry point for the console application.
//
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

void sort(int a[]) {
	for (int i = 1; i < 4; i++) {
		int v = a[i];
		int j = i - 1;
		while (j >= 0 && a[j] > v) {
			a[j + 1] = a[j];
			j--;
		}
		a[j + 1] = v;
	}
}

int main(void)
{
	int result[10000], suffix = 0;

	while (1) {
		int n, temp[4], save, big, small, count = 0;

		scanf("%d", &n);
		save = n;
		if (n == 0) {
			break;
		}
		bool success = false;
		for (int i = 0; i < 4; i++) {
			temp[i] = n % 10;
			n /= 10;
		}
		n = save;
		for (int i = 0; i < 3; i++) {
			if (temp[i] != temp[i + 1]) {
				success = true;
				break;
			}
		}

		while (n != 6174 && success == true) {
			for (int i = 0; i < 4; i++) {
				temp[i] = n % 10;
				n /= 10;
			}

			sort(temp);

			small = 0;
			big = 0;
			for (int i = 0; i < 4; i++) {
				small = small * 10 + temp[i];
				big = big * 10 + temp[3 - i];
			}
			n = big - small;
			count++;
		}

		if (success == true) {
			result[suffix] = count;
		}
		else {
			result[suffix] = -1;
		}
		suffix++;
	}

	for (int i = 0; i < suffix; i++) {
		if (result[i] == -1) {
			puts("NA");
		}
		else {
			printf("%d\n", result[i]);
		}
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

signed main()
{
  int N;
  while (cin >> N, N) {
    if (!(N % 1111)) cout << ("NA");
    else {
      int cnt = 0;
      while (N != 6174) {
        int n[4], L = 0, S = 0;
        for (int i = 0; i < 4; i++) {
          n[i] = N % 10;
          N /= 10;
        }
        sort(n, n + 4);
        for (int i = 0; i < 4; i++) {
          L = L * 10 + n[3 - i];
          S = S * 10 + n[i];
        }
        cnt += 1;
        N = L - S;
      }
      cout << cnt;
    }
    cout << "\n";
  }

  return (0);
}

C++: #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<Windows.h>
using namespace std;
int main(){
	char num[6]={0};
	
	while(1){
		int cnt = 0;
		scanf("%s",num);
		if(num[0] == num[1] && num[1] == num[2] && num[2] == num[3]){
			if(num[0] == 48)break;
			else puts("NA");
			continue;
		}
		if(num[0] == '6' && num[1] == '1' && num[2] == '7' && num[3] == '4'){
			puts("0");
			continue;
		}
		while(1){
			sort(num,num+4);
			int s = atoi(num);
			reverse(num,num+4);
			int l = atoi(num);
			int x = 0;
			if(l - s == 6174){
				printf("%d\n",cnt+1);
				break;
			}
			else{
				x = l - s;
				cnt++;
				for(int i = 0; i < 4; i++){
					num[i] = (x / pow(10,3-i))+48;
					x -= (num[i]-48) * pow(10,3-i);
				}
			}
		}
		
	}
	return 0;
}

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

int main() {
	std::string str;
	while (std::cin >> str) {
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == '0')
				break;
			else
				std::cout << "NA" << std::endl;
		}
		std::string l, s;
		int cnt = 0;
		while(str!="6174") {
			++cnt;
			l = str;
			std::sort(l.begin(), l.end(), std::greater<char>());
			s = str;
			std::sort(s.begin(), s.end());
			str = std::to_string(std::stoi(l) - std::stoi(s));
		}
		std::cout << cnt << std::endl;
	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
#include <math.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <deque>
#include <map>

#define ll long long
#define ull unsigned long long
#define mod 1000000007

using namespace std;

int main() {
	while(true){
		char x[4];
	for (int i = 0; i < 4; ++i) {
		cin >> x[i];
		x[i] -= '0';
	}

	if (x[0] == x[1] && x[1] == x[2] && x[2] == x[3]) {
		if(x[0] == 0) break;
		else cout << "NA" << "\n";
		continue;
	}

	int counter = 0;
	int t = x[0] * 1000 + x[1] * 100 + x[2] * 10 + x[3];
	while (t != 6174) {
		++counter;
		sort(x, x + 4);
		t = x[3] * 999 + x[2] * 90 - x[1] * 90 - x[0] * 999;
		x[0] = t / 1000;
		x[1] = (t / 100) - x[0] * 10;
		x[2] = (t / 10) - x[0] * 100 - x[1] * 10;
		x[3] = t - x[0] * 1000 - x[1] * 100 - x[2] * 10;
	}

	cout << counter << "\n";
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int dp[10000];
int solve(int num){
    if(num==6174){
        return 0;
    }else if(dp[num]){
        return dp[num];
    }else{
        int x[4];
        x[0]=num%10;
        x[1]=(num%100-num%10)/10;
        x[2]=(num%1000-num%100)/100;
        x[3]=(num%10000-num%1000)/1000;
        sort(x,x+4);
        dp[num]=solve(x[3]*999+x[2]*90-x[0]*999-x[1]*90)+1;
        return dp[num];
    }
    
}
int main(void){
   
 
   int N;
   while(1){
       cin>>N;
        
       if(N==0)break;
       
       if(N==1111||N==2222||N==3333||N==4444||N==5555||N==6666||N==7777||N==8888||N==9999){
           cout<<"NA"<<endl;
       }else{
           cout<<solve(N)<<endl;
       }
       
      
   }

}