Go: package main

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

var memo []int

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

C++: #include <stdio.h>
 
int main(void)
{
    int f;
    int i;
    int l, s, n;
    int b[10];
    int w, w2, count, num[4];
     
    while (scanf("%d", &n), n != 0){
        count = 0;
        f = 0;
        while (n != 6174){
            w = 1000;
            for (i = 0; i < 10; i++){
                b[i] = 0;
            }
            for (i = 0; i < 4; i++){
                num[i] = n / w;
                n %= w;
                w /= 10;
                b[num[i]]++;
            }
             
            w = 1000; w2 = 1;
            s = 0; l = 0;
            for (i = 0; i < 10; i++){
                while (b[i] > 0){
                    s += i * w;
                    w /= 10;
                    l += i * w2;
                    w2 *= 10;
                    b[i]--;
                }
            }
            n = l - s;
            count++;
            if (n == 0){
                printf("NA\n");
                f = 1;
                break;
            }
        }
        if (f == 0){
            printf("%d\n", count);
        }
    }
     
    return (0);
}

C++: #include <iostream>
using namespace std;
int main(){
  while(1){
  int count=0;
  int N;
  cin>>N;
  if(N==0)
  break;
  if(N%1111==0){
    cout<<"NA"<<endl;
    continue;
  }
   int L,S;
  while(N!=6174){
    int n[4];

   n[0]=N/1000;
   n[1]=N/100%10;
   n[2]=N/10%10;
   n[3]=N%10;

  sort(n,n+4);
  L=1000*n[3]+100*n[2]+10*n[1]+n[0];
  S=1000*n[0]+100*n[1]+10*n[2]+n[3];
  N=L-S;
  count++;
 }
 cout<<count<<endl;
 }
 return 0;
}

C++: #include <iostream>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;

void solve()
{
	string str;
	while(cin >> str)
	{
		if(str == "0000")
		{
			break;
		}
		else if(atoi(str.c_str()) % 1111 == 0)
		{
			cout << "NA" << endl;
			continue;
		}
		int count = 0;
		while(str != "6174")
		{
			sort(str.begin(), str.end());
			string small = str;
			string large;
			for(int i = str.size() - 1; i >= 0; --i)
			{
				large += str[i];
			}
			int num = atoi(large.c_str()) - atoi(small.c_str());
			stringstream ss;
			ss << num;
			str = ss.str();
			++count;
		}
		cout << count << endl;
	}
}

int main()
{
	solve();
	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){
      int L = make_L(N);
      int S = make_S(N);
      //cout <<L <<", " <<S <<endl;
      stringstream ss; ss << (L - S);
      N = ss.str();
      ++cnt;
    }
    cout <<cnt <<endl;
  }
  return 0;
}

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

int main(){
	int n,num[4],l,s,aa=0;
	while(cin>>n,n){
		aa=0;
		num[0]=n/1000;
		num[1]=n/100%10;
		num[2]=n/10%10;
		num[3]=n%10;
		if(num[0]==num[1]&&num[1]==num[2]&&num[2]==num[3]){
			cout<<"NA"<<endl;
			break;
		}
		while(num[0]!=6&&num[1]!=1&&num[2]!=7&&num[3]!=4){
			l=0,s=0;
			sort(num,num+4);
			//for(int i=0;i<4;i++)cout<<num[i];
			//cout<<endl;
			s=num[0]*1000+num[1]*100+num[2]*10+num[3];
			l=num[3]*1000+num[2]*100+num[1]*10+num[0];
			num[0]=(l-s)/1000;
			num[1]=(l-s)/100%10;
			num[2]=(l-s)/10%10;
			num[3]=(l-s)%10;
			aa++;
		}
		cout<<aa<<endl;
	}
}

C++: #include<stdio.h>
int main(void)
{
	int n;
	int l,s;
	int i,j,temp;
	int keta[4];
	int cnt;
	
	scanf("%d",&n);
	
	while(n!=0000){
		if(n%1111==0){
			printf("NA\n");
			break;
		}
		while(n!=6174){
			for(i=0;i<4;i++){
				keta[i]=n%10;
				n=n/10;
			}
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(keta[i]<keta[j]){
						temp=keta[i];
						keta[i]=keta[j];
						keta[j]=temp;
					}
				}
			}
			l=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];

			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(keta[i]>keta[j]){
						temp=keta[i];
						keta[i]=keta[j];
						keta[j]=temp;
					}
				}
			}
			s=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];
			n=l-s;
			cnt++;
		}
	printf("%d\n",cnt);
	scanf("%d",&n);
	}

	return 0;
}

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

int main()
{
	int n[4];
	string str;

	while (cin >> str, str != "0000"){
		for (int i = 0; i < 4; i++){
			n[i] = str[i] - '0';
		}

		if (n[0] == n[1] && n[0] == n[2] && n[0] == n[3]){
			cout << "NA" << endl;
		}
		else {
			int c = 0;
			while (n[0] != 6 || n[1] != 1 || n[2] != 7 || n[3] != 4){
				
				char l[4], s[4];
				for (int i = 0; i < 4; i++){
					l[i] = s[i] = n[i];
				}

				sort(l, l + 4);
				reverse(l, l + 4);
				sort(s, s + 4);

				for (int i = 3; 0 <= i; i--){
					n[i] = l[i] - s[i];
					if (n[i] < 0){
						n[i] += 10;
						l[i - 1]--;
					}
				}
				c++;
			}
			cout << c << endl;
		}
	}
	return (0);
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
long n,d[4],l,s,k,i;
while(cin>>n,n){
for(k=0;n!=6174;k++){
for(i=0;i<4;i++){d[i]=n%10;n/=10;}
sort(d,d+4,greater<long>());
for(l=i=0;i<4;i++){l=l*10+d[i];}
sort(d,d+4);
for(s=i=0;i<4;i++){s=s*10+d[i];}
n=l-s;
if(n==0)break;
}
if(n==0)cout<<"NA"<<endl;else cout<<k<<endl;
}
return 0;
}