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 <iomanip>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	while (true)
	{
		string N;
		cin >> N;
		if (N == "0000")
		{
			break;
		}
		else if (N[0]==N[1] && N[1]==N[2] && N[2]==N[3])
		{
			cout << "NA" << endl;
			continue;
		}

		int cnt1 = 0;
		while (N != "6174")
		{
			int cnts[10];
			for (int i=0; i<10; i++)
			{
				cnts[i] = 0;
				unsigned int j = 0;
				while ((j=N.find('0'+i, j)) != string::npos)
				{
					++j;
					++cnts[i];
				}
			}
			string L="0000", S="0000";
			int cnt2 = 0;
			for (int i=0; i<10; i++)
			{
				for (int j=0; j<cnts[i]; j++)
				{
					L[3-cnt2] = S[cnt2] = '0'+i;
					++cnt2;
				}
			}

			stringstream ss_L(L), ss_S(S);
			int i_L, i_S;
			ss_L >> i_L;
			ss_S >> i_S;
			stringstream ss_N;
			ss_N << setfill('0') << setw(4) << i_L-i_S;
			N = ss_N.str();
			++cnt1;
		}
		cout << cnt1 << endl;
	}

	return 0;
}

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

#define ASC(vec) vec.begin(), vec.end()		// 昇順ソート 例：sort(ASC(vec));
#define DESC(vec) vec.rbegin(), vec.rend()	// 降順ソート 例:sort(DESC(vec));
#define rep(i, n) for(int i = 0; i < n; i++)
#define Rep(i, n) for(int i = 1; i < n; i++)
#define In(vec) rep(i, n) cin>>vec[i];
#define ANS(s) cout << s << endl
#define inc(a, b) cin >> a >> b;

const int mod = 1000000007;
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;

using ii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
using vl = vector<ll>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vii = vector<pair<int, int>>;

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
int round_int(int a, int b) { return (a + (b - 1)) / b; }


// 4,8方向に探索する際に用いる配列
int dx[4] = { 0,  0, 1, -1 };
int dy[4] = { 1, -1, 0,  0 };
//int dx[8] = { 1, 0, -1,  0, 1, -1, -1,  1 };
//int dy[8] = { 0, 1,  0, -1, 1,  1, -1, -1 };

constexpr auto MAX = 100010;

// ────────────────────────────────────────────────────

string solve(string s) {
	sort(DESC(s));
	int ma = atoi(s.c_str());
	sort(ASC(s));
	int mi = atoi(s.c_str());

	ostringstream sout;
	sout << setfill('0') << setw(4) << to_string(ma - mi);
	string ret = sout.str();
	return ret;
}

int main() {
	while (1) {
		string s; cin >> s;
		int tmp = atoi(s.c_str());
		if (!tmp) break;

		if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
			cout << "NA" << endl;
			continue;
		}

		int cnt = 0;
		while (s != "6174") {
			s = solve(s);
			cnt++;
		}
		cout << cnt << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
// define
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)),UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a+mod-b)%mod
#define MUL(a, b) a = (((a)%mod) * ((b)%mod)) % mod
#define rollcall cout << "I'm Sucu." << endl;
#define repi(i,m,n) for(int i = m;i < n;i++)
#define drep(i,n,m) for(int i = n;i >= m;i--)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) repi(i,1,n+1)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x,y) make_pair(x,y)
#define dmt(x, y, z) make_tuple(x, y, z)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define fi first
#define se second
// typedef
typedef complex<double> Point;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9+7;
const int INF = 1e18+7;
// int sum = accumulate(a, a+n, 0LL);
// int mx = max_element(a, a+n);
// int mn = min_element(a, a+n);
// int cnt = count(a, a+n, k);


string to_str(int num){
  string str;
  drep(i,3,0){
    int c = 1;
    rep(j,i)c *= 10;
    str.pb(num/c + '0');
    num = num%c;
  }
  return str;
}

signed main(){
  while(true){
    string str;cin >> str;
    if(str == "0000")break;
    if(str[0] == str[1] and str[1] == str[2] and str[2] == str[3]){
      cout << "NA" << endl;continue;
    }
    int cnt = 0;
    while(true){
      if(str == "6174")break;
      string s1, s2;
      sort(all(str));
      s1 = str;
      reverse(all(str));
      s2 = str;
      int res = atoll(s2.c_str()) - atoll(s1.c_str());
      str = to_str(res);
      cnt++;
      // cout << s1 << " " << s2 << " " << str << endl;
    }
    cout << cnt << endl;
  }
  return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int N,L,S,N1,N2,N3,N4,N5,N6,i,temp,j,cnt,flg,flg1;
	int a[4];
	cnt=1;
	flg=0;
	flg1=0;
	scanf("%d",&N);
	if(N==6174){
		flg1=1;
	}
	while(!(N==0000)){
		if(flg1==1){
			cnt=0;
			flg=1;
			break;
		}
		else{
			N1=N/1000;/*6!*/
			N2=N%1000;/*174*/
			N3=N2/100;/*1!*/
			N4=N2%100;/*74*/
			N5=N4/10;/*7*/
			N6=N4%10;/*4!*/
			if(N1==N3 && N1==N5 && N1==N6){
				printf("NA\n");
				break;
			}
			else{
				a[0]=N1;
				a[1]=N3;
				a[2]=N5;
				a[3]=N6;
				for(i=0;i<4;i++){
					for(j=i+1;j<4;j++){
						if(a[i]<a[j]){
							temp=a[j];
							a[j]=a[i];
							a[i]=temp;	
						}
					}
				}
				N1=a[0]*1000;
				N3=a[1]*100;
				N5=a[2]*10;
				N6=a[3];
				L=N1+N3+N5+N6;
				for(i=0;i<4;i++){
					for(j=i+1;j<4;j++){
						if(a[i]>a[j]){
							temp=a[i];
							a[i]=a[j];
							a[j]=temp;	
						}
						
					}
				}
				N1=a[0]*1000;
				N3=a[1]*100;
				N5=a[2]*10;
				N6=a[3];
				S=N1+N3+N5+N6;
				N=L-S;
				if(N==6174){
				
					break;
				}
				else{
				flg=1;
					cnt++;
				}
			}
		}
	}
	if(flg==1){
	printf("%d\n",cnt);
	}
	return 0;
}

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

int main(){
  int n;
  int num[5];
  int l[5],s[5];
  int ans;

  while(1){
    cin >> n;
    if(n==0){
      break;
    }
    for(int i=3;i>=0;i--){
      num[i]=n%10;
      n/=10;
    }
    ans=0;
    while(1){
      if(num[0]==6 && num[1]==1 && num[2]==7 && num[3]==4){
	cout << ans << '\n';
	break;
      }
      if(num[0]==num[1] && num[1]==num[2] && num[2]==num[3]){
	cout << "NA\n";
	break;
      } 
      sort(num,num+4,greater<int>());
      for(int i=0;i<4;i++){
	l[i]=num[i];
      }
      int j=3;
      for(int i=0;i<4;i++){
	s[i]=num[j];
	j--;
      }
      
      for(int i=3;i>=0;i--){
	if(l[i]<s[i]){
	  l[i]+=10;
	  l[i-1]--;
	  num[i]=l[i]-s[i];
	}else{
	  num[i]=l[i]-s[i];
	}
      }
      ans++;
    }
  }
  
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    char s[8];
    while (atoi(fgets(s,8,stdin))) {
        *strchr(s,'\n')=0;
        int res=0;
        while (strcmp(s,"6174")) {
            sort(s, s+4);
            int S=atoi(s);
            reverse(s, s+4);
            int L=atoi(s);
            if (S==L) { res=-1; break; }
            res++;
            sprintf(s,"%04d",L-S);
        }
        if (res<0) puts("NA");
        else printf("%d\n",res);
    }
    return 0;
}