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<iostream>
#include<algorithm>
using namespace std;
int check(int n){
	int a[4];
	int s1=0,s2=0,s3;
	for(int i=0;i<4;i++){
		a[i]=n%10;
		n/=10;
	}
	for(int i=0;i<4;i++){
		for(int j=0;j<3;j++){
			if(a[j]<a[j+1])swap(a[j],a[j+1]);
		}
	}
	for(int i=0;i<4;i++){
		int j=3-i;
		s1*=10;   s2*=10;
		s1+=a[i]; s2+=a[j];
	}
	s3=s1-s2;
	return s3;
}
int s[10005];
int main(){
	int n;
	int i=0,j=0;
	while(cin>>n && n){
		for(int k=0;k<10000;k+=1111){
			if(n==k){
				s[i]=-1;
				break;
			}
		}
		if(s[i]==-1){
			i++;
			continue;
		}
		while(1){
			if(n==6174)break;
			j++;
			n=check(n);
		}
		s[i]=j;
		j=0;
		i++;
	}
	for(j=0;j<i;j++){
		if(s[j]==-1){
			cout<<"NA"<<endl;
		}else{
			cout<<s[j]<<endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;

int main(){
    int cnt,a,b;
    char num[5];
    while(1){
        cin >> num;
        cnt = 0;
        if(strcmp(num,"0000") == 0) break;
        if(num[0] == num[1] && num[1] == num[2] && num[2] == num[3]){
            cout << "NA" << endl;
            continue;
        }
        while(1){
            if(strcmp(num,"6174") == 0) break;
            cnt++;
            sort(num,num + 4);
            a = atoi(num);
            sort(num,num + 4,greater<char>());
            b = atoi(num);
            b -= a;
            sprintf(num,"%04d",b);
        }
        cout << cnt << endl;
    }
}

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) {
		if (s == "0000") {
			break;
		}
		int ans = 0;
		if (toInt(s) % 1111 == 0) {
			cout << "NA" << endl;
		}
		else {
			while (s != "6174") {
				string a = s, b = s;
				stable_sort(ALL(a));
				stable_sort(RALL(b));
				s = toStr(toInt(b) - toInt(a));
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include <strstream>
#include<algorithm>

#include<functional>

using namespace std;


string IntToString(int number)
{
stringstream ss;
ss << number;
return ss.str();
}

int main(){
	string s;
	while(cin>>s)
	{
		if(s=="0000")break;
		int count=0;
		while (1)
		{
			
			if(s=="6174"){
				puts("0");
				break;
			}
			
			vector<int>n;
			
			for(int i=0; i<4; i++)
			{
				
				n.push_back( (int)s[i]-48 );
			}
			
			sort( n.begin(),n.end(),greater<int>() );
			int L=0,S=0;

			if( (n[0]+n[1]) - (n[2]+n[3]) == 0){
				puts("NA");
				break; 
			}
			
			L += n[0]*1000;
			L += n[1]*100;
			L += n[2]*10;
			L += n[3];
			
			reverse(n.begin(),n.end());
			
			S += n[0]*1000;
			S += n[1]*100;
			S += n[2]*10;
			S += n[3];
			
			s.clear();
			s = IntToString(L-S);
			count++;
			
			if(L-S==6174){
				cout<<count<<endl;
				break;
			}
		}

	}
	return 0;
}

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

int solve(string &s, int cnt = 0){
  while(s != "6174"){
    if(s == "0000")return -1;
    cnt++;
    sort(s.begin(), s.end());
    string tmp = s;
    reverse(tmp.begin(), tmp.end());
    int a = stoi(s), b = stoi(tmp);
    s = to_string(b - a);
    while(s.length() != 4)s.push_back('0');
  }
  return cnt;
}

int main(int argc, char *argv[]){
  string s;
  while(cin >> s){
    if(s == "0000")break;
    int ans = solve(s);
    if(ans != -1)
      std::cout << ans << std::endl;
    else
      std::cout << "NA" << std::endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main(void){
	string S;
	while(cin >> S){
		int cost = 0;
		string str = S;
		if(S == "6174"){
			cout << cost << endl;
			continue;
		}
		if(S == "0000")break;
		if(StrToInt(S) % 1111 == 0){
			cout << "NA" << endl;
			continue;
		}
		while(1){
			cost++;
			vector<int> v;
			rep(i,4){
				int a = str[i]-'0';
				v.push_back(a);
			}
			sort(v.begin(), v.end(), greater<int>());
			int L = v[0] * 1000 + v[1] * 100 + v[2] * 10 + v[3];
			sort(v.begin(), v.end());
			int S = v[0] * 1000 + v[1] * 100 + v[2] * 10 + v[3];
			int sum = L - S;
			//cout << "L:" << L << " S:" << S << endl;
			//cout << "cost:" << cost << " " << sum << endl;
			if(sum == 6174){
				cout << cost << endl;
				break;
			}
			str = IntToStr(sum);
		}


	}
	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  while(cin>>n,n){
    for(int i=0;i<4000;i++){
      if(n==6174){cout<<i<<endl;break;}
      vector<int>v;
      for(int j=0;j<4;j++){
        v.push_back(n%10);
        n/=10;
      }
      sort(v.begin(),v.end());
      if(v[0]==v[3]){cout<<"NA"<<endl;break;}
      int p1,p2;
      p1=v[0]+v[1]*10+v[2]*100+v[3]*1000;
      p2=v[3]+v[2]*10+v[1]*100+v[0]*1000;
      n=p1-p2;
    }
  }
}