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.Println("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 <algorithm>
#include <math.h>
#include <cmath>
#include <stdio.h>
#include <string>
#include <string.h>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

int main (void)
{
	int y = 0;
	for (long u = 0; u < 10000; u++)
	{
		int co = 0;
		string a;
		int g[4];

		cin >> a;
		for (int k = 0; k < 100; k++)
		{
			if (a == "6174")
			{
				cout << co << endl;

				break;
			}
			if (a == "0000")
			{
				y = -1;
				break;
			}

			for (int i = 0; i < 4; i++)
			{
				g[i] = 0;
				g[i] = stoi (a.substr(i,1));
			}
			if (g[0] == g[1] && g[1] == g[2])
			{
				if (g[2] == g[3])
				{
					cout << "NA" << endl;
					break;
				}
			}
			long Max = 0,Min = 0;

			sort(g,g + 4);

			Min = (g[0] * 1000) + (g[1] * 100) + (g[2] * 10) + g[3];
			Max = (g[3] * 1000) + (g[2] * 100) + (g[1] * 10) + g[0];

			co += 1;
			if (Max - Min == 6174)
			{
				cout << co << endl;
				break;
			}
			ostringstream p;
			p << Max - Min;
			a = p.str();
		}
		if (y != 0)
		{
			break;
		}
	}
}

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

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)
{
	string n;
	while (cin >> n && n != "0000"){
		if (count(n.begin(), n.end(), n[0]) == 4){
			cout << "NA" << endl;
		}
		// assert(n != "0000");

		int tmp = toInt(n);
		char str[10];
		sprintf(str, "%04d", tmp);
		n = string(str);
		int cnt = 0;
		while (n != "6174"){
			if (count(n.begin(), n.end(), n[0]) == 4){
				cout << "NA" << endl;
				goto END;
			}
			string L = n; sort(L.rbegin(), L.rend());
			string S = n; sort(S.begin(), S.end());
			// cout << n << " " << L << " " << S << endl;
			int tmp = toInt(L) - toInt(S);
			char str[10];
			sprintf(str, "%04d", tmp);
			n = string(str);
			cnt++;
		}
		cout << cnt << endl;
		END:;
	}

	return 0;
}

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

int main() {


  string n;

  while(1) {
    cin >>n;
    
    int flg = 0;
    for(int i=0;i<4;i++) if(n[i] != '0') flg =1;
    if(flg == 0) break;
    
    int a[4];
    for(int i=0;i<4;i++) a[i] = n[i]-'0';
    int N=0;
    int t =1;
    for(int i=0;i<4;i++) N += a[3-i]*t,t*=10;
    int ans = 0;
    while(1) {
      if( N == 6174 || ans > 10000) break;
      ans++;
      sort(a,a+4);
      int maxn = 0;
      int minn = 0;
      t = 1;
      for(int i=0;i<4;i++) {
	maxn += a[i] * t;
	minn += a[3-i] * t;
	t*=10;
      }
      
      N = maxn - minn;
      t = N;
      for(int i=0;i<4;i++) a[i] = t%10, t/=10;
    }
    if(ans > 10000) cout <<"NA"<<endl; 
    else     cout << ans << endl;
  }

    
    return 0;    
    
    
}

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

int main () {
    string str;
    while (cin >> str) {
        bool f = false;
        for (auto i : str) if (i != '0') f = true;

        if (!f) break;

        f = false;
        for (auto i : str) if (i != str[0]) f = true;

        if (!f) {
            cout << "NA" << endl;
            continue;
        }

        int count = 0;
        int now;
        stringstream org;
        org << str;
        org >> now;
        while (1) {
            if (now == 6174) {
                cout << count << endl;
                break;
            }
            count++;
            sort(str.begin(), str.end());
            auto rstr = str;
            reverse(rstr.begin(), rstr.end());

            stringstream ss;
            ss << rstr << " " << str;
            int A, B;
            ss >> A >> B;
            now = A - B;
            stringstream next;
            next << now;
            next >> str;
        }
    }

    return 0;
}

C++: //============================================================================
// Name        : AOJ.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstdio>
#include <string>
#include <cctype>
using namespace std;

int main() {
	string n,L,S;
	int R=0;
	while(1){
		cin>>n;
		if(n=="0000")break;
		if(n=="6174")cout<<"0"<<endl;
		L=S=n;
		sort(L.begin(),L.end(),greater<char>());
		sort(S.begin(),S.end());

		cout<<R<<endl;
	}



	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int m,n,l;
  cin>>n;
  while(n!=0){
    int nn[4]={n%10,(n/10)%10,(n/100)%10,(n/1000)%10};
    //cout<<nn[3]<<nn[2]<<nn[1]<<nn[0]<<endl;
    if(nn[0]==nn[1]&&nn[1]==nn[2]&&nn[2]==nn[3]){
      cout<<"NA"<<endl;
    }else{
      int cou=0;
      while(nn[0]!=4||nn[1]!=7||nn[2]!=1||nn[3]!=6){
	int rn[4];
	for(int i=0;i<3;i++){
	  for(int j=i+1;j<4;j++){
	    if(nn[i]>nn[j]){
	      int exi=nn[i];
	      nn[i]=nn[j];
	      nn[j]=exi;
	    }
	  }
	}
	//cout<<nn[3]<<nn[2]<<nn[1]<<nn[0]<<' '<<nn[0]<<nn[1]<<nn[2]<<nn[3]<<endl;
	for(int i=0;i<4;i++){
	  rn[i]=nn[i];
	}
	for(int i=0;i<4;i++){
	  if(rn[i]-nn[3-i]<0){
	    rn[i]+=10;
	    rn[i]-=nn[3-i];
	    rn[i+1]--;
	  }else{
	    rn[i]-=nn[3-i];
	  }
	}
	for(int i=0;i<4;i++){
	  nn[i]=rn[i];
	}
	//cout<<nn[3]<<nn[2]<<nn[1]<<nn[0]<<endl;
	cou++;
      }
      cout<<cou<<endl;
    }
    cin>>n;
  }
  return (0);
}

C++: #include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string s;
    while (cin >> s) {
        if (s == "0000") return 0;
        vector<int> nums(4);
        for (int i = 0; i < 4; ++i) {
            nums[i] = s[i] - '0';
        }
        bool ok = false;
        for (int i = 0; i < 3; ++i) {
            if (nums[i] != nums[i + 1]) {
                ok = true;
                break;
            }
        }
        if (!ok) {
            cout << "NA" << '\n';
        } else {
            int res = 0;
            int nnow = 1000;
            for (int i = 0; i < 4; ++i) {
                res += nums[i] * nnow;
                nnow /= 10;
            }
            int cnt = 0;
            while (true) {
                if (res == 6174) break;
                vector<int> num(4);
                for (int i = 0; i < 4; ++i) {
                    num[i] = res % 10;
                    res /= 10;
                }
                sort(num.begin(), num.end());
                int large = 0, small = 0;
                int now = 1;
                for (int i = 0; i < 4; ++i) {
                    large += num[i] * now;
                    now *= 10;
                }
                now = 1;
                for (int i = 3; i >= 0; --i) {
                    small += num[i] * now;
                    now *= 10;
                }
                res = large - small;
                ++cnt;
            }
            cout << cnt << '\n';
        }
    }
    return 0;
}