Go: package main

import (
	"fmt"
	"sort"
)

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]
	return solve(l-s) + 1
}

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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <complex>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <iterator>

using namespace std;

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define iter(c) __typeof__((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INFTY=1<<29;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

int solve(int n,vi& memo)
{
	if(memo[n]!=-1) return memo[n];
	if(n==6174) return memo[n]=0;
	
	char s1[5]; sprintf(s1,"%04d",n); sort(s1,s1+4);
	char s2[5]; rep(i,4) s2[i]=s1[3-i];
	if(atoi(s1)==atoi(s2)) return -1;
	return memo[n]=1+solve(atoi(s2)-atoi(s1),memo);
}

int main()
{
	vi memo(10000,-1);
	for(int n;cin>>n,n;){
		int res=solve(n,memo);
		if(res==-1) cout<<"NA"<<endl;
		else cout<<solve(n,memo)<<endl;
	}
	
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int N;
int high;
int low;
int COU;
int KU[4]={0};
int JKK(int a,int b);
int main(){
	while (1){
		COU=0;
		cin>>N;
		if (N==0) break;
		if (N%1111==0){
			cout<<"NA"<<endl;
			continue;
		}
		while(N!=6174){
			COU++;
			KU[0]=JKK(10,N);
			KU[1]=JKK(100,N);
			KU[2]=JKK(1000,N);
			KU[3]=N/1000;
			sort(KU,KU+4);
			high=KU[3]*1000+KU[2]*100+KU[1]*10+KU[0];
			low=KU[3]+KU[2]*10+KU[1]*100+KU[0]*1000;
			N=high-low;
		}
		cout<<COU<<endl;
		N=0;
		COU=0;
		
	}
	return 0;
}
int JKK(int a,int b){
	int r;
	r=b%a;
	if(a==100) r/=10;
	else if(a==1000) r/=100;
	return r;
}

C++: #include<cstdio>
#include<algorithm>
#include<iostream>

#define reps(i,f,n) for(int i = f; i < int(n); i++)
#define rep(i,n) reps(i,0,n)
using namespace std;

int to[10000];
int memo[10000];

void init(){
	int L,S;
	char rstr[8];
	char str[8];
	rep(i,10000)memo[i] = -1;
	rep(i,10000){
		sprintf(rstr,"%04d", i);
		sprintf(str,"%04d", i);
		sort(rstr,rstr+4);
		sort(str,str+4);
		reverse(str, str+4);
		L = atoi(str);
		S = atoi(rstr);
		to[i] = L-S;
	}
}

int main(void){
	int n;
	init();
	
	while(scanf("%d", &n),n){
		int ans = 0;
		int n_ = n;
		if(to[n] == 0){
			puts("NA");
			continue;
		}
		if(memo[n] != -1){
			ans = memo[n];
		}else{
			while(n != 6174){
				n = to[n];
				ans++;
			}
			memo[n_] = ans;
		}
		printf("%d\n", ans);
	}
	
	return 0;
}

C++: #include<stdio.h>
#include<algorithm>

	using namespace std;
	int main()
{
	int a,n[4],L=0,S=0,i,c=0;
	scanf("%d",&a);
	
	while(a!=0000){
		c=0;
		while(a!=6174){
			if(a==1111||a==2222||a==3333||a==4444||a==5555||a==6666||a==7777||a==8888||a==9999){
				
				break;
			}
		L=0;S=0;
		
		
		
		c++;
	n[0]=a/1000;
	n[1]=a/100-n[0]*10;
	n[2]=a/10-n[0]*100-n[1]*10;
	n[3]=a-n[0]*1000-n[1]*100-n[2]*10;
	sort(n,n+4);
	L+=n[3]*1000;
	L+=n[2]*100;
	L+=n[1]*10;
	L+=n[0];
	S+=n[0]*1000;
	S+=n[1]*100;
	S+=n[2]*10;
	S+=n[3];
	a=L-S;
	}
		
		if(a==1111||a==2222||a==3333||a==4444||a==5555||a==6666||a==7777||a==8888||a==9999){
			printf("NA\n");
		}
		else{
			printf("%d\n",c);
		}
	
	
	scanf("%d",&a);
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;
int stoi(string a){
		int b;
		stringstream ss;
		ss<<a;
		ss>>b;
		return b;
	}
	string itos(int a){
		stringstream ss;
		ss <<a;
		string b;
		b=ss.str();
		return b;
	}

int main(){
	string n;int t;
	while(cin>>n,n!="0000"){
		int ns=n.size();
		for(int i=0;i<4-ns;i++)n="0"+n;
		int ans=0;
		bool flag=true;
		if(n[0]==n[1]&&n[1]==n[2]&&n[2]==n[3])flag=false;
		
		if(flag){
			while(n!="6174"){
			//	cout<<n<<endl;
				//cin>>t;
				int a[4]={0},b[4]={0},ai,bi;
				for(int i=0;i<4;i++)a[i]=n[i]-'0',b[i]=n[i]-'0';
				sort(a,a+4);
				sort(b,b+4);
				reverse(b,b+4);
				int tmp=0,l,s;
				l=1000*b[0]+100*b[1]+10*b[2]+b[3];
				s=1000*a[0]+100*a[1]+10*a[2]+a[3];
			//	cout<<s<<" "<<l<<endl;
				tmp=l-s;
				n=itos(tmp);
				ans++;
			}
		}
		if(flag)cout<<ans<<endl;
		else cout<<"NA"<<endl;
	}
	return 0;
}

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

int main() {
	int n;
	int str[4];
	while (std::cin >> n) {
		int str[4] = { (n / 1000),(n / 100) % 10,(n / 10) % 10,n % 10 };
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == 0)
				break;
			else
				std::cout << "NA" << std::endl;
		}
		int cnt = 0;
		while(n!=6174) {
			if (str[0] == str[1] == str[2] == str[3]) {
				std::cout << "NA" << std::endl;
				goto a;
			}
			++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;
			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<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;
}

C++: #include<iostream>
#include<algorithm>
#include<sstream>
#include<string>
#include<cstdlib>
#define rep(i,a) for( int i = 0; i != (a); ++i )

std::string intToStr( int n )
{
	std::ostringstream oss;
	oss << n;

	return oss.str();
}

int main()
{
	std::string L, S;
	while( std::cin >> L, L != "0000" )
	{
		int ans = 0;
		while( L != "6174" )
		{
			bool fl = true;

			rep( i, 3 )
				fl &= L[i+1] == L[i];
			if( fl )
			{
				std::cout << "NA" << std::endl;
				goto loop_end;
			}

			S = L;
			std::sort( L.rbegin(), L.rend() );
			std::sort( S.begin(), S.end() );

			++ans;

			L = intToStr( atoi( L.c_str() ) - atoi( S.c_str() ) );
		}

		std::cout << ans << std::endl;
loop_end:;
	}

	return 0;
}