Go: package main

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

var memo map[int]int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if _, ok := memo[v]; !ok {
		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(map[int]int)
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int ton(string s){
  return atoi(s.c_str());
}
string tos(int N){
  stringstream ss;
  ss<<N;
  string s = ss.str();
  while(s.size() < 4) s = "0" + s;
  return s;
}
int solve(string s){
  if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) return -1;
  int ans = 0;
  while(true){
    if(s == "6174") break;
    sort(s.begin(), s.end());
    int S = ton(s);
    reverse(s.begin(), s.end());
    int L = ton(s);
    int N = L - S;
    s = tos(N);
    ans += 1;
  }
  return ans;
}

int main(){
  string s;
  while( cin>>s && s != "0000"){
    int ans = solve(s);
    if(ans == -1) cout<<"NA"<<endl;
    else cout<<ans<<endl;
  }
  return 0;
}

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