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 "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

vector<int> solve(vector<int> a,vector<int> b){
    string x,y;
    rep(i,a.size()){
        x+='0'+a[i];
    }
    rep(i,b.size()){
        y+='0'+b[i];
    }
    int r=stoi(x)-stoi(y);
    vector<int> ret;
    stringstream ss;
    ss<<r;
    string z=ss.str();
    rep(i,z.size())ret.pb(z[i]-'0');
    return ret;
}

int main(){
    string s;
    while(cin>>s){
        if(s=="0000")break;
        int x=0;
        vector<int> c;
        rep(i,s.size())c.pb(s[i]-'0');
        while(1){
            if(c[0]==6&&c[1]==1&&c[2]==7&&c[3]==4){cout<<x<<endl;break;}
            if(c[0]==c[1]&&c[1]==c[2]&&c[2]==c[3]){
                cout<<"NA"<<endl;
                break;
            }
            vector<int> l=c,s=c;
            sort(all(s));
            sort(all(l),greater<int>());
            c=solve(l,s);
            x++;
        }
    }
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int main(){
    while(true){
	string s;
	ll ans = 0;
	cin >> s;
	if(s == "0000") break;
	else if(s[0] == s[1] && s[0] == s[2] && s[0] == s[3]){
	    cout << "NA" << endl;
	    continue;
	}
	while(s != "6174"){
	    vector<int> a(4);
	    vector<int> b(4);
	    a[0] = (int)(s[0] - '0');
	    a[1] = (int)(s[1] - '0');
	    a[2] = (int)(s[2] - '0');
	    a[3] = (int)(s[3] - '0');
	    b[0] = (int)(s[0] - '0');
	    b[1] = (int)(s[1] - '0');
	    b[2] = (int)(s[2] - '0');
	    b[3] = (int)(s[3] - '0');
	    sort(ALL(b));
	    sort(RALL(a));
	    int an = 1000 * a[0] + 100 * a[1] + 10 * a[2] + a[3];
	    int bn = 1000 * b[0] + 100 * b[1] + 10 * b[2] + b[3];
	    s = to_string(an - bn);
	    ans++;
	}
	cout << ans << endl;
    }
    return 0;
}

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


using namespace std ;


int check( int *p ,int sum )
{
	if( sum == 0 )
	{
		return -1 ;
	}

	int i = 1 ;
	for( ; i < 4 ; ++i )
	{
		if( p[ i ] != p[ i - 1 ] )
		{
			return 1 ;
		}
	}


	return 0 ;
}


int main()
{
	int n[ 4 ] = { 0 } ;

	int desc[] = { 1 ,10 ,100 ,1000 } ;
	int asc[] = { 1000 ,100 ,10 ,1 } ;


	int i ;
	while( 1 )
	{
		int sum = 0 ,ans = 0 ;

		for( i = 0 ; i < 4 ; ++i )
		{
			scanf( "%1d" ,&n[ i ] ) ;

			sum += n[ i ] ;
		}


		int ok = check( n ,sum ) ;

		if( ok == -1 )
		{
			return 0 ;
		}

		else if( ok == 0 )
		{
			puts( "NA" ) ;
		}

		else
		{
			sum = n[ 0 ] * asc[ 0 ] + n[ 1 ] * asc[ 1 ] + n[ 2 ] * asc[ 2 ] + n[ 3 ] * asc[ 3 ] ;

			while( sum != 6174 )
			{
				sort( n ,n + 4 ) ;


				sum = ( n[ 0 ] * desc[ 0 ] + n[ 1 ] * desc[ 1 ] + n[ 2 ] * desc[ 2 ] + n[ 3 ] * desc[ 3 ] ) - ( n[ 0 ] * asc[ 0 ] + n[ 1 ] * asc[ 1 ] + n[ 2 ] * asc[ 2 ] + n[ 3 ] * asc[ 3 ] )	;


				++ans ;


				n[ 0 ] = sum / asc[ 0 ] ;
				sum = sum - ( sum / asc[ 0 ] ) * asc[ 0 ] ;

				n[ 1 ] = sum / asc[ 1 ] ;
				sum = sum - ( sum / asc[ 1 ] ) * asc[ 1 ] ;

				n[ 2 ] = sum / asc[ 2 ] ;
				sum = sum - ( sum / asc[ 2 ] ) * asc[ 2 ] ;

				n[ 3 ] = sum / asc[ 3 ] ;


				sum = n[ 0 ] * asc[ 0 ] + n[ 1 ] * asc[ 1 ] + n[ 2 ] * asc[ 2 ] + n[ 3 ] * asc[ 3 ] ;
			}


			printf( "%d\n" ,ans ) ;
		}
	}
}

C++: #include <stdio.h>

void sort4(int *a){
	int min, minID;
	
	for (int i=0; i<4; i++){
		min = a[i], minID = i;
		for (int j=i+1; j<4; j++){
			if (a[j] < min) min = a[minID=j];
		}
		a[minID] = a[i]; a[i] = min;
	}
}

int main(void){
	int n,l,s,cnt,a[4];
	
	while (scanf("%d",&n), n){
		if (n % 1111 == 0){
			puts("NA");
		} else {
			cnt = 0;
			while (n != 6174){
				for (int j=0; j<4; j++){
					a[j] = n%10; n /= 10;
				}
				sort4(a);
				l = a[0]+10*(a[1]+10*(a[2]+10*a[3]));
				s = a[3]+10*(a[2]+10*(a[1]+10*a[0]));
//				printf("l=%d, s=%d, n=%d\n", l, s, l-s);
				n = l-s;
				cnt++;
			}
			printf("%d\n", cnt);
		}
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(v, a, b) for(int v = (a); v < (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define UNWORDS(vec, str) for(int i = 0; i < (vec).size(); ++i) cout << (i ? str : "") << (vec)[i]
#define UNLINES(vec) for(int i = 0; i < (vec).size(); ++i) cout << (vec)[i] << endl
#define RS resize
#define CLR clear
#define PB push_back
using namespace std;

int main(){
	int n, cnt;
	
	int rep[] = {1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999};
	vector< int > repdig(rep, rep + (sizeof(rep) / sizeof(int)));
	
	while(cin >> n, n){
		
		if(find(repdig.begin(), repdig.end(), n) != repdig.end()){
			cout << "NA" << endl;
			continue;
		}
		
		cnt = 0;
		
		while(n != 6174){
			stringstream ss;
			ss << setfill('0') << setw(4) << n;
			string str = ss.str();
			
			sort(str.begin(), str.end());
			
			int s = atoi(str.c_str());
			
			reverse(str.begin(), str.end());
			
			int l = atoi(str.c_str());
			
			n = l - s;
			++cnt;
		}
		cout << cnt << endl;
	}
	
	return 0;
}

C++: #include<iostream>

using namespace std;

bool func_a(int *N, int *count);

int main()
{
	while(true)
	{
		int N = 0;
		int count = 0;
		bool flag = true;


		cin >> N;

		if(N == 0)
		{
			break;
		}

		if(N == 6174)
		{
			cout << 0 << endl;
			flag = false;
		}

		for(int i = 0; i <= 9; i++)
		{
			if(N == i + i * 10 + i * 100 + i * 1000)
			{
				cout << "NA" << endl; 
				flag = false;
			}
		}
	
		if(flag)
		{
			while( func_a(&N, &count) ){};

			cout << count << endl;
		}
	}

	return false;
}

bool func_a(int *N, int *count)
{	
	int num = *N;
	int Nmax[4];
	int Nmin[4];
	int L = 0, S = 0;

	*count += 1;

	for(int i = 0; i < 4; i++)
	{
		Nmax[i] = Nmin[i] = num % 10;
		num /= 10;
	}

	for(int i = 0; i < 4; i++)
	{
		for(int j = 0; j < 4; j++)
		{
			if(Nmax[i] < Nmax[j])
			{
				int tmp = Nmax[i];
				Nmax[i] = Nmax[j];
				Nmax[j] = tmp;
			}
			if(Nmin[i] > Nmin[j])
			{
				int tmp = Nmin[i];
				Nmin[i] = Nmin[j];
				Nmin[j] = tmp;
			}
		}
	}

	int dz[4] = {1, 10, 100, 1000};

	for(int i= 0; i < 4; i++)
	{
		L += Nmax[i] * dz[i];
		S += Nmin[i] * dz[i];
	}

	*N = L - S;

	if(*N == 6174)
	{
		return false;
	}
	else
	{
		return true;
	}
}