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<iostream>
#include<sstream>
#include<algorithm>

using namespace std;


bool invalid(string s){
  char c = s[0];

  for(int i = 0; i < s.length(); i++)
    if(s[i] != c) return false;

  return true;
}

string tos(int n){
  string out = "";

  for(int i = 0; i < 4; i++){
    out += n%10+'0';
    n/=10;
  }
  reverse(out.begin(),out.end());
  return out;
}

string solve(string s){
  string A,B;

  sort(s.begin(),s.end());
  A = s;
  reverse(s.begin(),s.end());
  B = s;

  int a,b;
  stringstream s1(A),s2(B);

  s1 >> a;
  s2 >> b;
  return tos(b-a);
}

int main(){
  string s;
  while(cin >> s && s != "0000"){
    int ans = 0;

    if(invalid(s)){
      cout << "NA" << endl;
      continue;
    }

    while(s!="6174"){
      s = solve(s);
      ans++;
    }

    cout << ans << endl;
  }
  return 0;
}

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

int main(){
  int a[4];
  int n;
  
  while(1){
    cin>>n;
    if(n==0) break;
    int nmax,nmin;
    int co=0;
    while(1){
      if(n==0 || n==6174) break;
      co++;
      for(int i=0;i<4;i++){
	a[i] = n%10;
	n/=10;
      }
      nmax=0,nmin=0;
      sort(a,a+4);
      for(int i=0;i<4;i++){
	nmin = nmin*10+a[i];
	nmax = nmax*10+a[3-i];
      }
      n=nmax-nmin;
    }
    if(n==0) cout<<"NA"<<endl;
    else cout <<co<<endl;
  }


  return 0;
}

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


bool allsame(int n){
  int tmp=n%10;
  n/=10;
  while(n!=0){
    if(tmp!=n%10){
      return 0;
    }
    tmp=n%10;
    n/=10;
  }
  return 1;
}

string toSortString(int n){
  stringstream ss;
  ss<<n;
  string tmp;
  ss>>tmp;
  sort(tmp.begin(),tmp.end());
  return tmp;
}

int make_l(string s){
  int res=0;
  for(int i=0;i<s.size();i++){
    res+=(s[i]-'0')*pow(double(10),i);
  }
  return res;
}

int make_s(string s){
  int res=0;
  for(int i=0;i<s.size();i++){
    res+=(s[s.size()-1-i]-'0')*pow(double(10),i);
  }
  return res;
}

int main(){
  int n;
  int ans=0;
  while(cin>>n,n!=0){
    if(allsame(n)){
      cout<<"NA"<<endl;
      continue;
    }
    while(n!=6174){
      ans++;
      int l,s;
      string tmp_n=toSortString(n);
      l=make_l(tmp_n);
      s=make_s(tmp_n);
      n=l-s;
    }
    if(n==6174){
      cout<<ans<<endl;
    }
  }
}

C++: #include<stdio.h>
#include<algorithm>
#include<functional>
using namespace std;
int main(void){
	int n,i,a,j;
	int de,syo,tem[4];
	while(1){
		scanf("%d",&n);
		if(n==0000)
			break;
		int cnt=0,flg=0;
		while(n!=6174){
			de=syo=0;
			for(i=0;i<4;i++){
				a=1;
				for(j=0;j<3-i;j++){
					a*=10;
				}
				tem[i]=n/a;
				n-=tem[i]*a;
			}
			if(tem[0]==tem[1]==tem[2]==tem[3]){
				flg=1;
				break;
			}
			sort(tem,tem+4);
			a=1000;
			for(i=0;i<4;i++){
				syo+=tem[i]*a;
				a/=10;
			}
			sort(tem,tem+4,greater<int>());
			a=1000;
			for(i=0;i<4;i++){
				de+=tem[i]*a;
				a/=10;
			}
			cnt++;
			n=de-syo;
		}
		if(flg==0)
			printf("%d\n",cnt);
		else 
			printf("NA\n");
	}
	return 0;
}

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

int s(int n, bool f) {
  vector<int> a;
  while (n) {
    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 < a.size(); ++i) {
    res *= 10;
    res += a[i];
  }

  return res;
}

int main() {
  int n;

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

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

    cout << cnt << endl;
  }
}