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<cstdio>
#include<algorithm>
#include<string>
#include<sstream>
#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(){
	string str, rstr;
	int L,S;
	stringstream ss;
	rep(i,10000)memo[i] = -1;
	rep(i,10000){
		ss.clear();
		ss<<i;
		ss>>rstr;
		sort(rstr.begin(),rstr.end());
		str = rstr;
		reverse(str.begin(), str.end());
		ss.clear();
		ss << (str + " " + rstr);
		ss >> L >> S;
		to[i] = L-S;
	}
}

int main(void){
	int n;
	init();
	
	while(scanf("%d", &n),n){
		int ans = 0;
		int n_ = n;
		if(n == 0)break;
		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<iostream>
#include<algorithm>

using namespace std;
int main()
{
	int su, n[5];
	int i;
	int d, s;
	int ke,cnt;
	int f;
	while (cin >> su, su != 0)
	{
		cnt = 0;
		f = 1;
		while (su != 6174)
		{
			//cout << cnt << endl;
			cnt++;
			ke = 1000;
			for (i = 0; i < 4; i++)
			{
				n[i] = su / ke;
				su -= su / ke * ke;
				ke = ke / 10;
			}
			if (n[0] == n[1] && n[2] == n[3] && n[1] == n[2])
			{
				cout << "NA" << endl;
				f = 0;
				break;
			}
			sort(n, n + 4);
			d = s = 0;
			ke = 1000;
			for (i = 0; i < 4; i++)
			{
				s += n[i] * ke;
				ke = ke / 10;
			}
			ke = 1000;
			for (i = 3; i >= 0; i--)
			{
				d += n[i] * ke;
				ke = ke / 10;
			}
			su = d - s;
		}
		if(f) cout << cnt << endl;
	}
	return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int n,r[4],l,s,x,y,z,a,i,j,flg;
	while(1){
		scanf("%d",&n);
		if(n==0000){
			break;
		}
		flg=1;
		a=0;
		while(n!=6174){
			y=0;
			x=1000;
			for(i=0;i<4;i++){
				r[i]=n/x;
				n=n%x;
				x=x/10;
				if(r[0]==r[i]) y++;
			}
			if(y==4) {
				flg=0;
				break;
			}
			l=0; s=0;
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(r[i]>r[j]){
						z=r[i];
						r[i]=r[j];
						r[j]=z;
					}
				}
			}
			x=1000;
			for(i=0;i<4;i++){
				s=r[i]*x+s;
				x=x/10;
			}
			printf("x=%d ",x);
			x=1;
			for(i=0;i<4;i++){
				l=r[i]*x+l;
				x=x*10;
			}
			n=l-s;
			a++;
		}
		if(flg==1) printf("%d\n",a);
		if(flg==0) printf("NA\n");
	}
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> pl;

int main()
{
	while (1) {
		string s;
		cin >> s;
		if (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0')
			break;
		if (s == "6174") {
			cout << 0 << endl;
		}
		else if (s[0] == s[1] &&  s[2] == s[3] && s[1] == s[2])
			cout << "NA" << endl;
		else {
			ll ans = 0;
			while (s != "6174") {
				while (s.size() != 4)
					s = '0' + s;
				
				int a[4];
				for (int i = 0; i < 4; i++)
					a[i] = s[i] - '0';

				sort(a, a + 4);

				ll small=0,large=0;
				for (int i = 0; i < 4; i++)
					large += a[i] * (ll)pow(10, i);
				
				reverse(a,a+4);
				for (int i = 0; i < 4; i++) 
					small += a[i] * (ll)pow(10, i);
				
				s = to_string(large - small);
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

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

string cn;
int main()
{
  while(true){
    cin >> cn;
    if(cn == "0000"){
      break;
    }
    if(cn[0] == cn[1] == cn[2] == cn[3]){
      cout << "NA" << endl;
    }

    else{
      int ans = 0;
      while(cn != "6174"){
	int in, intss;
	stringstream ss1, ss2, ss3;
	string cncp;
	sort(cn.begin(), cn.end(), greater<char>());
	ss1 << cn;
	ss1 >> intss;
	in = intss;
	for(int j = 0; j < 4; j++){
	  cncp += cn[3 - j];
	}
	ss2 << cncp;
	ss2 >> intss;
	in -= intss;
	ss3 << in;
	ss3 >> cn;
	ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int main()
{
	int N,T,C;
	int L, S;
	int data[4];
	int su[4] = { 1000,100,10,1 };
	while (1) {
		C = 0;
		cin >> N;
		if (N == 0) break;
		if (N % 1111 == 0) {
			cout << "NA" << endl;
			continue;
		}

		while (N != 6174) {
			L = S = 0;
			C++;
			data[0] = N / 1000;
			N %= 1000;
			data[1] = N / 100;
			N %= 100;
			data[2] = N / 10;
			N %= 10;
			data[3] = N;

			for (int i = 0; i < 3; i++) {
				for (int j = i + 1; j < 4; j++) {
					if (data[i] < data[j]) {
						T = data[i];
						data[i] = data[j];
						data[j] = T;
					}
				}
			}
			
			for (int i = 0; i < 4; i++) {
				L += data[i] * su[i];
				S += data[3 - i] * su[i];
			}
	
			N = L - S;
		}
		cout << C << endl;
	}
}

C++: #include <iostream>
//#include <cstdio>
#include <stdio.h>
#include <string>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <typeinfo>
#define PI 3.14159265359
#define INF 99999999;
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(n) rep(i, n)
#define EPS 1e-10
typedef long long ll;
using namespace std;



/*
class Target
{
public:
	vector <string> draw(int n)
	{

	}
};
*/



int main()
{
	for (char s[5]; scanf("%s", s), strcmp(s, "0000")!=0;)
	{
		if (count(s, s + 4, s[0]) == 4)
		{
			cout << "NA" << endl;
			continue;
		}
		
		int ans = 0;
		while (strcmp(s, "6174") != 0)
		{
			char s1[5];
			strcpy(s1, s);
			sort(s1, s1 + 4);
			char s2[5];
			strcpy(s2, s);
			sort(s2, s2 + 4, greater<char>());
			int a1 = atoi(s1);
			int a2 = atoi(s2);
			sprintf(s, "%04d", a2 - a1);
			ans++;
		}
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define all(vec) vec.begin(),vec.end()
typedef long long int ll;
typedef pair<ll,ll> P;
const ll MOD=1000000007;
const ll INF=1000000010;
const ll LINF=4000000000000000010LL;
const int MAX=310;
const double EPS=1e-9;
int main(){
    int n;
    while(cin>>n){
        if((n%10000)/1000==(n%1000)/100&&(n%1000)/100==(n%100)/10&&(n%100)/10==n%10){
            if(n%10==0){
                break;
            }
            cout<<"NA"<<endl;
            continue;
        }
    int c=0;
    while(n!=6174){
        int a[4];
        a[0]=(n%10000)/1000;
        a[1]=(n%1000)/100;
        a[2]=(n%100)/10;
        a[3]=n%10;
        int p;
        sort(a,a+4);
        p=1;
        int l=0,s=0;
        for(int i=0;i<4;i++){
            s+=a[i]*p;
            p*=10;
        }
        p=1;
        for(int i=3;i>=0;i--){
            l+=a[i]*p;
            p*=10;
        }
        n=s-l;
        c++;
    }
    cout<<c<<endl;
    }
}