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 <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str(); }
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 2;
const int MOD = 1000000007;

using namespace std;
typedef vector<ll> vl;
typedef vector<vl> vvl;

int main() {
	string s;
	while (cin >> s, s != "0000") {
		int ans = 0;
		if (toInt(s) % 1111 == 0) {
			cout << "NA" << endl;
		}
		else {
			while (s != "6174") {
				string a = s, b = s;
				sort(ALL(a));
				sort(RALL(b));
				s = toStr(toInt(b) - toInt(a));
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<sstream>
#include<algorithm>
using namespace std;
inline 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()
{
  string n;
  for(;cin>>n,n!="0000";)
    {
      int cnt=0;
      while(n!="6174")
	{
	  string s=n;
	  string l=n;
	  sort(s.begin(),s.end());
	  sort(l.begin(),l.end(),greater<char>());
	  n=toString(toInt(l)-toInt(s));
	  while(n.size()<4)
	    n="0"+n;
	  if(s==l)
	    {
	      cnt=-1;
	      break;
	    }  
	  cnt++;  
	}
      if(cnt>=0)
	cout<<cnt<<endl;
      else 
	cout<<"NA"<<endl;
    }
}

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.compare("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(RALL(a));
	    sort(ALL(b));
	    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];
	    if(an < bn) swap(an,bn);
	    s = to_string(an - bn);
	    ans++;
	}
	cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <cmath>
#include <complex>
#include <map>
using namespace std;

#define rep(i,n) for(int i=0;i<int(n);++i)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<long long,int> P;
static const int INF=1<<24;

string s;

int main(){
	while(cin>>s,s!="0000"){
		if(s=="6174"){
			cout<<0<<endl;
			continue;
		}
		if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3]){
			cout<<"NA\n";
			continue;
		}
		int c=0;
		while(1){
		int MIN,MAX;
		vector <int> v;
		rep(i,4){
			s[i]-='0';
			v.PB(s[i]);
		}
		MIN=0;MAX=0;
		sort(v.begin(),v.end());
		rep(i,4){
			MIN*=10;
			MIN+=v[i];
		}
		sort(v.begin(),v.end(),greater<int>());
		rep(i,4){
			MAX*=10;
			MAX+=v[i];
		}
			int a=MAX-MIN;
			if(MAX-MIN==6174) break;
			
			else{
				rep(i,4){
					s[3-i]=a%10;
					a/=10;
				}
				c++;
			}
		}
		cout<<c+1<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define P pair<int, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
const string END = "6174";

bool isNG(string s){
  REP(i, s.length()) if(s[0] != s[i]) return false;
  return true;
}

int make_L(string N){
  SORT(N);
  REVERSE(N);
  int ret = 0;
  REP(i, N.length()){
    ret *= 10;
    ret += (N[i] - '0');
  }
  return ret;
}

int make_S(string N){
  SORT(N);
  int ret = 0;
  REP(i, N.length()){
    ret *= 10;
    ret += (N[i] - '0');
  }
  return ret;
}

int main() {
  string N;
  while(cin >>N && N != "0000"){
    int cnt = 0;
    if(isNG(N)){
      cout <<"NA" <<endl;
      continue;
    }
    while(N != END){
    //REP(i, 10){
      int L = make_L(N);
      int S = make_S(N);
      //cout <<L <<", " <<S <<endl;
      stringstream ss; ss << (L - S);
      string s = ss.str();
      while(s.length() < 4) s = "0" + s;
      //cout <<s <<endl;
      N = s;
      ++cnt;
    }
    cout <<cnt <<endl;
  }
  return 0; }

C++: #include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    int N,S = 0;
   while(1)
   {
       cin >> N;
       int big[4] = {0},small[4],ans = 0;
       if(N == 0000)break;
       if(N % 1111 == 0)puts("NA");
       else
       {
           while(N != 6174)
           {


               big[0] = small[0] = N / 1000;
               big[1] = small[1] = (N - (big[0]*1000)) / 100;
               big[2] = small[2] = (N - (big[0]*1000 + big[1]*100)) / 10;
               big[3] = small[3] = N % 10;
               /*for(int i = 0; i < 4; i++)
               {
                   cout << big[i] << "??¨" << small[i] << endl;
               }*/
               for(int i = 0; i < 4; i++)
               {
                   for(int j = i+1; j < 4; j++)
                   {
                       if(big[i] > big[j])swap(big[i],big[j]);
                       if(small[i] < small[j])swap(small[i],small[j]);
                   }
               }
               N = 0; S = 0;
               int aaa = 1;
               for(int i = 0; i < 4; i++)
               {

                   S += small[i]*aaa;
                   N += big[i]*aaa;
                   aaa = aaa*10;
               }
               N -= S;
               ans++;
           }
       }
       cout << ans << endl;

   }
}