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 <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
  int n;

  while (scanf("%d", &n), n){
    int cnt = 0;
    int num[4];
    int L, S;
    if (n % 1111 == 0){
      printf("NA\n");
      continue;
    }
    while (n != 6174){
      cnt++;
      for (int i = 0; i < 4; i++){
        num[i] = n % 10;
        n /= 10;
      }
      sort(num, num + 4);
      L = num[3] * 1000 + num[2] * 100 + num[1] * 10 + num[0];
      S = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3];
      n = L - S;
    }
    printf("%d\n", cnt);
  }

  return (0);
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
int simuration(string TES){
    if(TES[0] == TES[1] && TES[1] == TES[2] && TES[2] == TES[3]){
        return -1;
    }else{
    int z,cnt = 0;
    
        while(TES[0] != '6' && TES[0] != '1' && TES[0] != '4' && TES[0] != '7'){
            sort(TES.begin(),TES.end());
            int x = atoi(TES.c_str());
            sort(TES.begin(),TES.end(),greater<char>());
            int y = atoi(TES.c_str());
            z = y - x;
            TES = to_string(z);
            cnt++;
        }
    return cnt;
    }
}

int main(void){
    
    string TES;
    while(TES != "0000"){
    cin >> TES;
        if(TES == "0000")   return false;
    int judge = simuration(TES);
        if(judge == -1) {
            cout << "NA" << endl;
        }else{
            cout << simuration(TES) << endl;
        }
    }
}

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[128];
		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[128];
			sprintf(str, "%04d", tmp);
			n = string(str);
			cnt++;
		}
		cout << cnt << endl;
		END:;
	}

	return 0;
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"

string to_string_keta(const long long int l,const int keta) {
	ostringstream oss;

	oss << setprecision(keta) << l;

	return oss.str();
}

int main() {
	while (1) {
		int N; cin >> N;
		if (!N)break;
		if (N % 1111 == 0) {
			cout << "NA" << endl;
			continue;
		}
		int num = 0;
		while (1) {
			if (N == 6174)break;
			string st(to_string_keta(N, 4));
			sort(st.begin(), st.end());
			int amin(stoi(st));
			reverse(st.begin(), st.end());
			int amax(stoi(st));
			N = amax - amin;
			num++;

		}
		cout << num << endl;
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int rec(int n){
	vector<int> d;
	for(int i=0;i<4;i++){
		d.push_back(n%10);
		n/=10;
	}
	sort(d.begin(),d.end());
	int l=0,s=0,k=1;
	for(int i=0;i<4;i++){
		l+=d[i]*k;
		s+=d[3-i]*k;
		k*=10;
	}
	return l-s;
}
int main(){
	int n;
	while(cin>>n){
		if(n==0000)break;
		if(n%1111==0){
			cout<<"NA"<<endl;
			continue;
		}
		int cnt=0;
		while(1){
			if(n==6174){
				cout<<cnt<<endl;
				break;
			}
			n=rec(n);
			cnt++;
		}
	}
}

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

using namespace std;

// http://d.hatena.ne.jp/pogin/20121001/1349069143
inline 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() {
	while (true) {
		string s;
		cin >> s;
		if (s == "0000") {
			break;
		}
		if (toInt(s) % 1111 == 0) {
			cout << "NA" << endl;
			continue;
		}
		
		int ans = 0;
		while (s != "6174") {
			ans++;
			sort(s.begin(), s.end());
			string min = s;
			string max;
			max = min;
			reverse(max.begin(), max.end());
			//cout << "max: " << max << " min: " << min << endl;
			int next = toInt(max) - toInt(min);
			//cout << "next: " << next << endl;
			string next_string = toString<int>(next);
			int next_length = next_string.length();
			if (next_length <= 3) {
				for (int i = next_length; i < 4; i++) {
					next_string.append("0");
				}
			}
			s = next_string;
		}
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <sstream>
#include <numeric>
#include <cstdio>
#include <cassert>
using namespace std;

string to_str(int n) { stringstream ss; ss << n; return ss.str(); }
bool same_all(string s) { return count(s.begin(), s.end(), s[0]) == s.size(); }
int to_int(string s) { return atoi(s.data()); }

int main()
{
	for(string line; cin >> line && (line != "0000"); ) {
	  int times = 0;

		if(same_all(line)) {
			cout << "NA" << endl;
			continue;
		}
		
		for(; line != "6174"; times++) {
			sort(line.begin(), line.end());
			int small = to_int(line);
		
			reverse(line.begin(), line.end());
			int big = to_int(line);

			int next = big - small;

			assert(next >= 0);

			line = to_str(next);
		}

		cout << times << endl;
	}
}

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

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;
		}

		int tmp = toInt(n);
		char str[5];
		sprintf(str, "%04d", tmp);
		n = string(str);
		int cnt = 0;
		while (n != "6174"){
			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[5];
			sprintf(str, "%04d", tmp);
			n = string(str);
			cnt++;
		}
		cout << cnt << endl;
	}

	return 0;
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int make(vector<int> num)
{
	int s;
	s = 0;
	for (int i = 0; i < 4; i++){
		s = s * 10 + num[i];
	}
	
	return (s);
}

int main(void)
{
	vector<int> num(4);
	int n;
	int t;
	int ct;
	
	while (scanf("%d", &n) && n){
		
		if (n % 1111 == 0){
			printf("NA\n");
			continue;
		}
		
		ct = 0;
		while (n != 6174){
			ct++;
			t = n;
			for (int i = 0; i < 4; i++){
				num[i] = t % 10;
				t /= 10;
			}
			sort(num.rbegin(), num.rend());
			int L = make(num);
			sort(num.begin(), num.end());
			int S = make(num);
			n = L - S;
		}
		
		printf("%d\n", ct);
	}
	
	return (0);
}