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 <string>
using namespace std;

int main(){
	int num[4],l,s,aa=0;
	string n;
	while(cin>>n,n!="0000"){
		for(int i=0;i<4;i++)num[i]=n[i]-'0';
		aa=0;
		if(num[0]==num[1]&&num[1]==num[2]&&num[2]==num[3]){
			cout<<"NA"<<endl;
			continue;
		}
		while(num[0]!=6&&num[1]!=1&&num[2]!=7&&num[3]!=4){
			l=0,s=0;
			sort(num,num+4);
			//for(int i=0;i<4;i++)cout<<num[i];
			//cout<<endl;
			s=num[0]*1000+num[1]*100+num[2]*10+num[3];
			l=num[3]*1000+num[2]*100+num[1]*10+num[0];
			num[0]=(l-s)/1000;
			num[1]=(l-s)/100%10;
			num[2]=(l-s)/10%10;
			num[3]=(l-s)%10;
			aa++;
		}
		cout<<aa<<endl;
	}
}

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