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<algorithm>
#include<functional>
using namespace std;

int main()
{
	while (true){
		char num[4];
		int ans;
		for (int i = 0; i < 4; i++){
			cin >> num[i];
		}
		if (num[0] == num[1] && num[1] == num[2] && num[2] == num[3]){
			if (num[0] == '0') break;
			cout << "NA" << endl;
			continue;
		}
		for (ans = 0; num[0] != '6' || num[1] != '1' || num[2] != '7' || num[3] != '4'; ans++){
			char num2[4], num3[4];
			for (int i = 0; i < 4; i++){
				num2[i] = num[i];
			}
			sort(num, num + 4);
			sort(num2, num2 + 4, greater<char>());
			for (int i = 3; i >= 0; i--){
				if (num2[i] - num[i] < 0){
					num2[i] += 10;
					num2[i - 1]--;
				}
				num3[i] = num2[i] - num[i] + '0';
			}
			for (int i = 0; i < 4; i++){
				num[i] = num3[i];
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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



vector<int> i_v(int n){
  vector<int> res;
  while(n!=0){
    res.push_back(n%10);
    n/=10;
  }
  return res;
}

int v_i(vector<int> v){
  int res=0;
  for(int i=0;i<v.size();i++){
    res*=10;
    res+=v[v.size()-1-i];
  }
  return res;
}

int main(){
  int n;
  vector<int> l,s;
  while(cin>>n){
    l = i_v(n);
    bool f = true;
    for(int i=1;i<l.size();i++){
      if(l[i-1]!=l[i]){
        f=false;
      }
    }
    if(f){
      cout<<"NA"<<endl;
      continue;
    }
    int ans=0;
    while(n!=6174){
      l.clear(),s.clear();
      l = i_v(n);
      sort(l.begin(),l.end());
      for(int i=0;i<l.size();i++){
        s.push_back(l[l.size()-1-i]);
      }
      n = v_i(l)-v_i(s);
      ans++;
    }
    cout<<ans<<endl;
  }
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
 
 
 
const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
 

 string calc(string s, string t){
 	string ret = s;

 	for(int i = 0; i < 4; i++){
 		ret[i] -= t[i] - '0';
 	}

 	for(int i = 3; i > 0; i--){
 		if(ret[i] < '0') {
 			ret[i] += 10;
 			ret[i - 1]--;
 		}
 	}

 	return ret;
 }
 
int main(){
	string s, t;
	while(cin>>s){
		if(s == "0000") break;

		bool f = false;
		for(int i = 0; i < 9; i++){
			if(s == string(4, '1' + i)){
				f = true;
				break;
			}
		}
		if(f){
			cout<<"NA"<<endl;
			continue;
		}

		int n = 0;
		while(1){
			if(s == "6174") break;
			n++;
			t = s;
			sort(all(s));
			sort(all(t));
			reverse(all(t));

			s = calc(t, s);
		}

		cout<<n<<endl;
	}
}

C++: 
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <cstdio>
#include <cassert>

using namespace std;

int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

int main(void)
{
	string n;
	while (cin >> n && n != "0000"){
		if (count(n.begin(), n.end(), n[0]) == 4){
			cout << "NA" << endl;
			continue;
		}
		// assert(n != "0000");

		int tmp = toInt(n);
		char str[128];
		sprintf(str, "%04d", tmp);
		n = string(str);
		int cnt = 0;
		while (n != "6174"){
			if (count(n.begin(), n.end(), n[0]) == 4){
				cout << "NA" << endl;
				goto END;
			}
			string L = n; sort(L.rbegin(), L.rend());
			string S = n; sort(S.begin(), S.end());
			// cout << n << " " << L << " " << S << endl;
			int tmp = toInt(L) - toInt(S);
			char str[128];
			sprintf(str, "%04d", tmp);
			n = string(str);
			cnt++;
		}
		cout << cnt << endl;
		END:;
	}

	return 0;
}

C++: #include<stdio.h>
#include<algorithm>
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<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;
}