Go: package main

import (
	"fmt"
	"sort"
	"strconv"
)

var memo map[int]int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if _, ok := memo[v]; !ok {
		str := ([]byte)(strconv.Itoa(v))
		sort.Slice(str, func(i, j int) bool {
			return str[i] < str[j]
		})
		s, _ := strconv.Atoi(string(str))
		sort.Slice(str, func(i, j int) bool {
			return str[i] > str[j]
		})
		l, _ := strconv.Atoi(string(str))
		memo[v] = solve(l-s) + 1
	}
	return memo[v]
}

func main() {
	var v int
	memo = make(map[int]int)
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

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());


            int diff = stoi(L) - stoi(S);            
            N = to_string(diff);
            reverse(N.begin(), N.end());
            while (N.size() != 4) {
                N += "0";
            }
            reverse(N.begin(), N.end());                        
            cnt++;            
        }
        if (fail) {
            cout << "NA" << endl;
        } else {
            cout << cnt << endl;
        }
    }        
    return 0;
}

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

int s(int n, bool f) {
  vector<int> a;
  for (int i = 0; i < 4; ++i) {
    a.push_back(n % 10);
    n /= 10;
  }

  sort(a.begin(), a.end());
  if (f) reverse(a.begin(), a.end());

  int res = 0;
  for (int i = 0; i < 4; ++i) {
    res *= 10;
    res += a[i];
  }

  return res;
}

int main() {
  int n;

  while (cin >> n, n) {
    if (n % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }

    // cout << s(n, true) - s(n, false) << endl;

    int cnt = 0;
    while (n != 6174) {
      cnt++;
      n = s(n, true) - s(n, false);
    }

    cout << cnt << endl;
  }
}

C++: #include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    while(1){
        int num;
        cin>>num;
        if(num==0)break;
        int numnum[4];
        int ans=0;
        while(1){
            if(num==6174){
                cout<<ans<<endl;
                break;
            }
            numnum[0]=num%10000/1000;
            numnum[1]=num%1000/100;
            numnum[2]=num%100/10;
            numnum[3]=num%10/1;
            if(numnum[0]==numnum[1]&&numnum[0]==numnum[2]&&numnum[0]==numnum[3])puts("NA");
            int nummax[4];
            int nummin[4];
            for(int i=0;i<4;i++){
                nummax[i]=numnum[i];
                nummin[i]=numnum[i];
            }
            sort(nummax,nummax+4);
            sort(nummin,nummin+4);
            swap(nummax[0],nummax[3]);swap(nummax[1],nummax[2]);
            int mx=nummax[0]*1000+nummax[1]*100+nummax[2]*10+nummax[3]*1;
            int mn=nummin[0]*1000+nummin[1]*100+nummin[2]*10+nummin[3]*1;
            //printf("[%d][%d]\n",mx,mn);
            num=mx-mn;
            ans++;
        }
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
int main(){
	string a;
	while (getline(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);
				if (a.length() == 1)
					a = "000" + a;
				else if (a.length() == 2)
					a = "00" + a;
				else if (a.length() == 3)
					a = "0" + a;
				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<bits/stdc++.h>

#define endl '\n'

using namespace std;

int main(){

  cin.tie(0);     // cout と cin の同期を切る
  ios::sync_with_stdio(false);  /* cの stdioストリーム (printfとか)と*/

  string str;

  while(1){

    
    cin>>str;
    
    int a[4];
    
    if(str=="0000"){
      break;
    }else if(str[0]==str[1]&&str[1]==str[2]&&str[2]==str[3]){
      cout<<"NA"<<endl;
      continue;
    }

    for(int i=0;i<4;i++){
      a[i]=str[i]-'0';
    }
    int cnt=0;

    while(1){

      int c[4];

      c[0]=a[0];
      c[1]=a[1];
      c[2]=a[2];
      c[3]=a[3];

      
      if(c[0]==6&&c[1]==1&&c[2]==7&&c[3]==4){
	break;
      }
      
      sort(a,a+4);
      
      int cnt1=a[0]*1000+a[1]*100+a[2]*10+a[3];
      int cnt2=a[3]*1000+a[2]*100+a[1]*10+a[0];
      
      cnt2=cnt2-cnt1;
      
      c[3]=cnt2%10;
      cnt2/=10;
      c[2]=cnt2%10;
      cnt2/=10;
      c[1]=cnt2%10;
      cnt2/=10;
      c[0]=cnt2%10;


      cnt++; 

      a[0]=c[0];
      a[1]=c[1];
      a[2]=c[2];
      a[3]=c[3];

    }

    cout<<cnt<<endl;
  }

  return 0;

}

C++: #include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <sstream>

#define rep(i, j) FOR(i, 0, j)
#define FOR(i, j, k) for(int i = j; i < k; ++i)

using namespace std;

int main(){
  char n[5];
  while(scanf("%s", n) && strcmp(n, "0000") != 0){
    if(n[0] == n[1] && n[1] == n[2] && n[2] == n[3]){ puts("NA"); continue;}
    int c = 0;
    while(strcmp(n, "6174")){
      rep(i, 4) FOR(j, i + 1, 4){
	if(n[i] > n[j]){ char tmp = n[i]; n[i] = n[j]; n[j] = tmp;}
      }
      int a = atoi(n);
      char tmp = n[0]; n[0] = n[3]; n[3] = tmp;
      tmp = n[1]; n[1] = n[2]; n[2] = tmp;
      int b = atoi(n);
      b -=  a;
      sprintf(n, "%04d", b);
      c++;
    }
    printf("%d\n", c);
  }
  return 0;
}