Go: package main

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

func main() {
	var v int
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Print("NA")
			continue
		}
		count := 0
		for v != 6174 {
			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))
			v = l - s
			count++
		}
		fmt.Println(count)
	}
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <cstdio>
#include <cassert>

using namespace std;

int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

int main(void)
{
	string n;
	while (cin >> n && n != "0000"){
		if (count(n.begin(), n.end(), n[0]) == 4){
			cout << "NA" << endl;
		}
		assert(n != "0000");

		int tmp = toInt(n);
		char str[128];
		sprintf(str, "%04d", tmp);
		n = string(str);
		int cnt = 0;
		while (n != "6174"){
			assert(count(n.begin(), n.end(), n[0]) != 4);
			string L = n; sort(L.rbegin(), L.rend());
			string S = n; sort(S.begin(), S.end());
			// cout << n << " " << L << " " << S << endl;
			int tmp = toInt(L) - toInt(S);
			char str[128];
			sprintf(str, "%04d", tmp);
			n = string(str);
			cnt++;
		}
		cout << cnt << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#include<string>
using namespace std;

char N[4], n[4];
int L, S, d;

int main()
{
  while(true)
  {
    cin >> N;
    if(N[0] == '0' && N[1] == '0' && N[2] == '0' && N[3] == '0')
      break;
    if(N[0] == N[1] && N[1] == N[2] && N[2] == N[3])
    {
      cout << "NA" << endl;
      continue;
    }
    for(int i = 0; i < 10; i++)
    {
      if(N[0] == '6' && N[1] == '1' && N[2] == '7' && N[3] == '4')
      {
        cout << i << endl;
        break;
      }

      L = S = 0;
      sort(N, N + 4);
      for(int j = 0; j < 4; j++)
        S += (N[j] - '0') * pow(10, 3 - j);
      reverse(N , N + 4);
      for(int j = 0; j < 4; j++)
        L += (N[j] - '0') * pow(10, 3 - j);
      d = L - S;
      for(int j = 0; j < 4; j++)
      {
        N[j] = (d / (int)pow(10, 3 - j)) + '0';
        d -= (N[j] - '0') * (int)pow(10, 3 - j);
      }
    }
  }
}

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