Go: package main

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

var memo []int8

func solve(v int) int8 {
	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([]int8, 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 <string>
#include <algorithm>
#include <deque>
using namespace std;
string L(string);
string S(string);
string sa(string,string);
int main(void)
{
	int count;
	string n;

	while(1){
		cin >> n;
		if(n=="0000") break;
		if(n=="1111"||n=="2222"||n=="3333"||n=="4444"||n=="5555"||n=="6666"||n=="7777"||n=="8888"||n=="9999") {
			cout << "NA" << endl;
			continue;
		}
		count=0;
		while(n!="6174"){
			n=sa(L(n),S(n));
			count+=1;
		}
		cout << count << endl;
	}
	return 0;
}
string S(string n)
{
	string s;
	deque<char> dn;
	for(int i=0;i<4;i++) dn.push_back(n[i]);
	sort(dn.begin(),dn.end());
	for(int i=3;0<=i;i--){
		s+=dn[i];
		dn.pop_back();
	}
	return s;
}
string L(string n)
{
	string l;
	deque<char> dn;
	for(int i=0;i<4;i++) dn.push_back(n[i]);
	sort(dn.begin(),dn.end());
	for(int i=0;i<4;i++){
		l+=dn[i];
	}
	return l;
}
string sa(string l,string s)
{
	string n;
	int lnum=0,snum=0,num,b=1;
	for(int i=3;0<=i;i--){
		lnum+=(b*(l[3-i]-'0'));
		snum+=(b*(s[3-i]-'0'));
		b*=10;
	}
	num=lnum-snum;
	for(int i=0;i<4;i++){
		b/=10;
		n+=((num/b)%10+'0');
	}
	cout << n << endl;
	return n;
}

C++: #include<iostream>
#include<algorithm>
#include<functional>
#include<cstring>

int main() {
	int n;
	bool used[10000];
	while (std::cin >> n) {
		int str[4] = { (n / 1000),(n / 100) % 10,(n / 10) % 10,n % 10 };
		if (n % 1111 == 0) {
			if (str[0] == 0)
				break;
			else
				std::cout << "NA" << std::endl;

		}
		std::memset(used, false, sizeof(used));
		used[n] = true;
		int cnt = 0;
		while(n!=6174) {
			++cnt;
			std::sort(str, str + 4, std::greater<int>());
			int l = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
			std::sort(str, str + 4);
			int	s = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
			n = l - s;
			if (used[n])
				goto a;
			used[n] = true;
			str[0] = n / 1000;
			str[1] = (n / 100) % 10;
			str[2] = (n / 10) % 10;
			str[3] = n % 10;
		}
		std::cout << cnt << std::endl;
	a:;
	}
	return 0;
}

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef set<int> si;
typedef set<ll, ll> sll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef map<int, int> mii;
typedef map<int, ll> mil;
typedef map<ll, int> mli;
typedef map<ll, ll> mll;

#define FOR(i, a, b) for(int i = (a), loop_end_##i=(b); i < (loop_end_##i); i++)
#define REP(i, n) FOR(i, 0, n)
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define SORT(c) sort((c).begin(),(c).end())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define PB push_back

#define dump(x)  cout << #x << " = " << (x) << endl
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl

#define rep(v,n) for(int v=0;v<n;v++)
#define input(t,v) t v;cin>>v;
#define input2(t,v1,v2) t v1,v2;cin>>v1>>v2;
#define input3(t,v1,v2,v3) t v1,v2,v3;cin>>v1>>v2>>v3;

template<class T> T read() {
	T t;
	cin >> t;
	return t;
}

int main(){
	while(true){
		input(int,n);
		if(n==0)return 0;
		if(n%1111==0){cout<<"NA"<<endl;continue;}
		int ret=0;
		while(n!=6174){
			int ds[4],tmp;
			tmp=n;
			rep(i,4){
				ds[i]=tmp%10;
				tmp/=10;
			}
			sort(begin(ds),end(ds));
			
			int S=ds[0]*1000+ds[1]*100+ds[2]*10+ds[3];
			int L=ds[3]*1000+ds[2]*100+ds[1]*10+ds[0];
			n=L-S;
			ret++;
		}
		cout<<ret<<endl;
	}
	
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
int main() {
	char S[5];
	while(scanf("%s",S)) {
		if(strcmp(S,"0000")==0) break;
		int ans=0;
		bool NG=0;
		while(strcmp(S,"6174")) {
			sort(S,S+4);
			NG=1;
			for(int i=0;i<3;i++) {
				if(S[i]!=S[i+1]) NG=0;
			}
			if(NG) break;
			int a=0,b=0,c=1;
			for(int i=0;i<4;i++) {
				a+=(S[i]-'0')*c;
				b+=(S[3-i]-'0')*c;
				c*=10;
			}
			int k=a-b;
			for(int i=3;i>=0;i--) {
				S[i]=(k%10+'0');
				k/=10;
			}
			ans++;
		}
		if(NG) printf("NA\n"); else printf("%d\n",ans);
	}
}