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 <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, n) for(int i = 0; i < (n); i++)
#define dep(i, a, n) for(int i = a; i >= (n); i--)
#define FOR(i, a, n) for(int i = a; i < (n); i++)
#define mod (ll)(1e9+7)
#define int ll
#define INF 100000000

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int ch(int n) {
  vector<int> v;
  rep(i, 4) {
    v.pb(n % 10);
    n /= 10;
  }
  sort(v.begin(), v.end());
  int la = 0, sm = 0;
  rep(i, 4) {
    la += v[i] * pow(10, i);
    sm += v[3 - i] * pow(10, i);
  }
  return la - sm;
}

signed main() {
  ll n;
  while(cin >> n) {
    if(n == 0000) break;
    if(n % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }
    ll cnt = 0;
    while(1) {
      if(n == 6174) {
        cout << cnt << endl;
        break;
      }
      n = ch(n);
      cnt++;
    }
  }
	return 0;
}

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

int main(){
    int n, d[4];
    while(cin >> n){
        if(n == 0)  break;
        
        if(n % 1111 == 0){
            cout << "NA" << endl;
        }else{
            int tmpn = n;
            for(int i = 3; i >= 0; i--){
                d[i] = tmpn%10;
                tmpn /= 10;
            }
            
            int ans = 0;
            while(n != 6174){
                ans++;
                sort(d, d+4);
                n = (1000*d[3] + 100*d[2] + 10*d[1] + d[0]) -
                        (1000*d[0] + 100*d[1] + 10*d[2] + d[3]);
                tmpn = n;
                for(int i = 3; i >= 0; i--){
                    d[i] = tmpn%10;
                    tmpn /= 10;
                }
            }
            
            cout << ans << endl;
        }
    }
    return 0;
}

C++: #include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    while(1){
        int num;
        cin>>num;
        if(num==0)break;
        int numnum[4];
        int ans=0;
        while(1){
            if(num==6174){
                cout<<ans<<endl;
                break;
            }
            numnum[0]=num%10000/1000;
            numnum[1]=num%1000/100;
            numnum[2]=num%100/10;
            numnum[3]=num%10/1;
            if(numnum[0]==numnum[1]&&numnum[0]==numnum[2]&&numnum[0]==numnum[3]){
                puts("NA");
                break;
            }
            int nummax[4];
            int nummin[4];
            for(int i=0;i<4;i++){
                nummax[i]=numnum[i];
                nummin[i]=numnum[i];
            }
            sort(nummax,nummax+4);
            sort(nummin,nummin+4);
            swap(nummax[0],nummax[3]);swap(nummax[1],nummax[2]);
            int mx=nummax[0]*1000+nummax[1]*100+nummax[2]*10+nummax[3]*1;
            int mn=nummin[0]*1000+nummin[1]*100+nummin[2]*10+nummin[3]*1;
            //printf("[%d][%d]\n",mx,mn);
            num=mx-mn;
            ans++;
        }
    }
    return 0;
}

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

// int型からstring型に変換する(非負整数)
string to_s(int n){
	string s;
	do{
		s.push_back( (n%10)+'0' );
		n /= 10;
	}while( n != 0 );
	reverse( s.begin() , s.end() );
	return s;
}

// string型からint型に変換する(非負整数)
int to_i(string s){
	int n = 0;
	for(int i=0 ; i < s.size() ; i++ ){
		n = (n * 10) + s[i] - '0';
	}
	return n;
}

int main(){
	string N;
	while( cin >> N, N != "0000" ){
		if( N[0] == N[1] && N[1] == N[2] && N[2] == N[3] ){
			cout << "NA" << endl;
		}else{
			int ans = 0;
			while( N != "6174" ){
				string L = N, S = N;
				sort(L.rbegin(), L.rend());
				sort(S.begin(), S.end());
				N = to_s(to_i(L) - to_i(S));
				while( N.size() < 4 ) N = "0" + N;
				ans++;
			}
			cout << ans << endl;
		}
	}
}

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

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)
{
	int a;
	while (cin >> a, a){
		string n = toString(a);
		if (n.length() < 4) {
			for (int i = 0; i <= 4-n.length(); i++) {
				n += '0';
			}
		}
		if (n[0] == n[1] && n[0] == n[2] && n[0] == n[3]){
			cout << "NA" << endl;
			continue;
		}

		int cnt = 0;
		int tmp;
		string N = n;
		while (1){
			if (N == "6174"){
				cout << cnt << endl;
				break;
			}
			//cout << N << endl;
			//cin >> tmp;
			string L = N; sort(L.rbegin(), L.rend());
			string S = N; sort(S.begin(), S.end());
			int d = toInt(L) - toInt(S);
			N = toString(d);
			if (N.length() < 4) {
			for (int i = 0; i <= 4-N.length(); i++) {
				N += '0';
			}
		}
			cnt++;
		}
	}

	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>

using namespace std;

void st(char *hoge){
	for(int i=0;i<4;i++){
		for(int j=i+1;j<4;j++){
			if(hoge[i] < hoge[j]){
				char t = hoge[i];
				hoge[i] = hoge[j];
				hoge[j] = t;
			}
		}
	}
}

void rst(char *hoge){
	for(int i=0;i<4;i++){
		for(int j=i+1;j<4;j++){
			if(hoge[i] > hoge[j]){
				char t = hoge[i];
				hoge[i] = hoge[j];
				hoge[j] = t;
			}
		}
	}
}

int main(){

	while(1){
		int n=0,l,s;
		int cnt = 0;
		char tmp[5] = {0};

		scanf("%s",tmp);
		n = atoi(tmp);

		if(n == 0)break;
		if(n % 1111 == 0){
			printf("NA\n");
			continue;
		}

		while(n != 6174){
			sprintf(tmp, "%d", n);
			rst(tmp);
			s = atoi(tmp);

			st(tmp);
			l = atoi(tmp);
			n = l - s;

			cnt++;
		}
		printf("%d\n", cnt);
	}
	return 0;
}

C++: #include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std;
char B[9];
#define I(s)strtol(s.c_str(),NULL,10)
string S(int i,int L){sprintf(B,"%0*d",L,i);return B;}
int main(){
	int a,i;
	for(;scanf("%d",&a),a;printf(a?"%d\n":"NA\n",i)){
		for(i=0;a&&a!=6174;i++){
			string t=S(a,4),s;
			sort(t.begin(),t.end());
			s=t;
			reverse(s.begin(),s.end());
			a=I(s)-I(t);			
		}
	}
}

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");
		}
		else {
			cnt=0;	
			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;
}