Go: package main

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

var memo []int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if memo[v] == 0 {
		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([]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(){

char ty;
char c1,c2,c3,c4,ch5;
int n1=0,n2=0,n3=0,n4=0,N=0,S=0,P=0,K=0;
int n[5]={};
int F=0;
while(1){
//n1=c1-'0';n2=c2-'0';n3=c3-'0';n4=c4-'0';
	
	bool f=false;
	do{
	scanf("%c%c%c%c%c",&c1,&c2,&c3,&c4,&ch5);
	ty=c1;
	
     if(ty==c1&&ty==c3&&ty==c4&&ty==c2)f=false;
	else f=true;

	if(ty=='0')f=true;
	if(f==false)printf("NA\n");

	}while(f!=true);
	if(c1=='0'&&c2=='0'&&c3=='0'&&c4=='0')break;
	F++;
	if(F==10001)break;
	
	for(int i=0;i<4;i++)
	n[i]=0;
int cnt=0;


n[0]=c1-'0';n[1]=c2-'0';n[2]=c3-'0';n[3]=c4-'0';


for(int i=0;i<3;i++)
for(int j=3;j>i;j--)
if(n[j]>n[j-1]){int t=n[j];n[j]=n[j-1];n[j-1]=t;}

N=1000*n[0]+100*n[1]+10*n[2]+n[3];
S=1000*n[3]+100*n[2]+10*n[1]+n[0];

P=N-S;
if(P!=6174)cnt++;

while(P!=6174){
N=P;
K=P;n[0]=N/1000;
K=N%1000;n[1]=K/100;
K=N%100;n[2]=K/10;
K=N%10;n[3]=K;

//printf(" %d\n",N);

for(int i=0;i<3;i++)
for(int j=3;j>i;j--)
if(n[j]>n[j-1]){int t=n[j];n[j]=n[j-1];n[j-1]=t;}


N=1000*n[0]+100*n[1]+10*n[2]+n[3];
S=1000*n[3]+100*n[2]+10*n[1]+n[0];

cnt++;
P=N-S;

}
printf("%d\n",cnt);
}
return 0;
}

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;
}