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<iostream>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<list>
#include<set>
#include<stack>
#include<cstdio>
using namespace std;
char n[5];
int k[5];

int jk(int sum); // L-Sを返す
int j;

int main(){
  int cnt;
  int sum;
  int fl;
  while(1){

    scanf("%s",n);

    if(n[0]=='0'&&n[1]=='0'&&n[2]=='0'&&n[3]=='0')break;
    fl = 0;
    for(int i=0;i<3;i++){
      if(n[i] != n[i+1]){
        fl = 1;
      }
    }

    //分解
    if(fl == 1){
      sum = 0;
      j = 1000;
      for(int i=0;i<4;i++){
        k[i] = n[i]-'0';
        sum+= k[i]*j;
        j/=10;
      }

      //操作
      cnt = 0;
      while(sum != 6174){
        cnt++;
        sum = jk(sum);
      }
      cout << cnt << endl;
    }else{
      cout << "NA" << endl;
    }
  }

  return 0;
}

int jk(int sum){
  int ls,l=0,s=0;
  int j;
  j = 1000;
  for(int i=0;i<4;i++){
    k[i] = sum/j;
    sum = sum%j;
    j/=10;
  }
  sort(k,k+4);
  j = 1000;
  for(int i=0;i<4;i++){
    s+=k[i] * j;
    j/=10;
  }

  reverse(k,k+4);
  j = 1000;
  for(int i=0;i<4;i++){
    l+=k[i] * j;
    j/=10;
  }

  ls = l - s;

  return ls;
}

C++: #include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <typeinfo>
#define PI 3.14159265359
#define INF 99999999;
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(n) rep(i, n)
#define EPS 1e-10
typedef long long ll;
using namespace std;



/*
class Target
{
public:
	vector <string> draw(int n)
	{

	}
};
*/



int main()
{
	for (char s[5]; scanf("%s", s), strcmp(s, "0000")!=0;)
	{
		if (count(s, s + 4, s[0]) == 4)
		{
			cout << "NA" << endl;
			continue;
		}
		
		int ans = 0;
		while (strcmp(s, "6174") != 0)
		{
			char s1[5];
			strcpy(s1, s);
			sort(s1, s1 + 4);
			char s2[5];
			strcpy(s2, s);
			sort(s2, s2 + 4, greater<char>());
			int a1 = atoi(s1);
			int a2 = atoi(s2);
			sprintf(s, "%04d", a2 - a1);
			ans++;
		}
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int d[4];
int main(){
    int a;
    while(scanf("%d",&a),a){
        if(a%1111==0){
            printf("NA\n");
            continue;
        }
        int count=0;
        while(a!=6174){
            for(int i=0;i<4;i++){
                d[i]=a%10;
                a/=10;
            }
            std::sort(d,d+4);
            int b=0;
            for(int i=0;i<4;i++){
                b*=10;
                b+=d[i];
            }
            int c=0;
            for(int i=3;i>=0;i--){
                c*=10;
                c+=d[i];
            }
            a=c-b;
            count++;
        }
        printf("%d\n",count);
    }
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

int main(){
  string s;

  while(cin >> s, s != "0000"){
    if(s[0] == s[1] && s[0] == s[2] && s[0] == s[3]){
      cout << "NA" << endl;
      continue;
    }

    int ans = 0;

    while(s != "6174"){
      sort(s.begin(), s.end());
      string S = s;
      sort(s.rbegin(), s.rend());
      string L = s;
      int N = atoi(L.c_str()) - atoi(S.c_str());

      char buff[10];
      sprintf(buff, "%04d", N);
      buff[4] = 0;
      s = string(buff);

      ans++;
    }

    cout << ans << endl;
  }
}

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

int main(){
    vector<int> n;
    int input;
    while(cin >> input){
        int ans = 0;
        if(input == 0) break;
        if(input % 1111 == 0){
            cout << "NA" << endl;
            continue;
        }
        while(input != 6174) {
            for(int i=0; i < 4; i++){
                n.push_back(input%10);
                input /= 10;
            }
            vector<int> minNum ,maxNum;

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

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

            n.clear();
        }
        cout << ans << 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;
    while(cin >> input){
        int ans = 0;
        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 <iostream>
#include <algorithm>
using namespace std;

string convert( string trg ){
    sort(trg.begin(), trg.end());
    int a = 0, b = 0, mul = 1;
    for(string::iterator it = trg.begin(); it != trg.end(); it++){
        int n = (*it)-'0';
        a += n * mul;
        b += n * 1000 / mul;
        mul *= 10;
    }
    a = a-b;
    char tmp[8] = {a/1000+'0', a/100%10+'0', a/10%10+'0', a%10+'0'};
    string res(tmp);
    return res;
}

int main(){
    string ss;
    string FIN("0000"), GOAL("6174");
    while( cin >> ss ){
        if( ss == FIN ) break;
        if( ss[0] == ss[1] && ss[1] == ss[2] && ss[2] == ss[3] ){
            cout << "NA" << endl;
            continue;
        }
        int c = 0;
        while( c < 10 && ss!=GOAL ){
            c++;
            ss = convert(ss);
        }
        cout << c << endl;

    }
    return 0;
}