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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>

using namespace std;

int main(){
	int work[4],L,S,Result,count;
	char buf[5];

	while(true){
		scanf("%s",buf);

		if(buf[0] == '0' && buf[1] == '0' && buf[2] == '0' && buf[3] == '0')break;

		if(buf[0] == buf[1] && buf[0] == buf[2] && buf[0] == buf[3]){
			printf("NA\n");
			continue;
		}

		work[0] = buf[0] - '0';
		work[1] = buf[1] - '0';
		work[2] = buf[2] - '0';
		work[3] = buf[3] - '0';

		count = 0;

		while(true){
			if(work[0] == 6 && work[1] == 1 && work[2] == 7 && work[3] == 4)break;

			count++;
			for(int i = 1; i <= 3; i++){
				for(int k = 3; k >= i; k--){
					if(work[k] > work[k-1])swap(work[k],work[k-1]);
				}
			}

			L = 1000*work[0]+100*work[1]+10*work[2]+work[3];

			for(int i = 1; i <= 3; i++){
				for(int k = 3; k >= i; k--){
					if(work[k] < work[k-1])swap(work[k],work[k-1]);
				}
			}
			S = 1000*work[0]+100*work[1]+10*work[2]+work[3];
			Result = L - S;

			for(int i = 0,k=1000; i < 4; i++, k /= 10){
				work[i] = Result/k;
				Result %= k;
			}
		}
		printf("%d\n",count);
	}

	return 0;
}

C++: #include<bits/stdc++.h>
#define endl '\n'
#define INF (1 << 21)
const int MOD = 1000000007;
using namespace std;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  string str;
  int n;
  int l, s;
  int ans;

  cin >> str;
  while(str != "0000"){
    if(str == "6174"){
      cout << 0 << endl;
    }else if(str[0] == str[1] && str[1] == str[2] && str[2] == str[3]){
      cout << "NA" << endl;
    }else{
      ans = 0;

      while(1){
	l = s = 0;
	ans ++;

	int r = 1;
	sort(str.begin(), str.end());
	for(int i = 3;i >= 0;i--){
	  s += (str[i] - '0') * r;
	  r *= 10;
	}
	
	r = 1;
	reverse(str.begin(), str.end());
	for(int i = 3;i >= 0;i--){
	  l += (str[i] - '0') * r;
	  r *= 10;
	}
	
	if((n = l - s) == 6174){
	  break;
	}else{
	  for(int i = 3;i >= 0;i--){
	    str[i] = (n % 10) + '0';
	    n /= 10;
	  }
	}
      }
      cout << ans << endl;
    }      
    cin >> str;
  }
  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};
    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;
	    }
	  }
	}
	for(int i=0;i<4;i++){
	  rn[i]=nn[i];
	}
	for(int i=0;i<4;i++){
	  if(nn[3-i]>nn[i]){
	    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];
	}
	cou++;
      }
      cout<<cou<<endl;
    }
    cin>>n;
  }
  return (0);
}

C++: #include<iostream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<cstdlib>

int main() {
	int n;
	int str[4];
	while (std::cin >> n) {
		int str[4] = { (n / 1000),(n / 100) % 10,(n / 10) % 10,n % 10 };
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == '0')
				break;
			else
				std::cout << "NA" << std::endl;
		}
		int l[4], s[4];
		int cnt = 0;
		while(n!=6174) {
			if (str[0] == str[1] == str[2] == str[3])
				std::cout << "NA" << std::endl;
			++cnt;
			std::memcpy(l, str, 4);
			std::memcpy(s, str, 4);
			std::sort(l, l + 5, std::greater<char>());
			std::sort(s, s + 5);
			int _l = l[0] * 1000 + l[1] * 100 + l[2] * 10 + l[3],
				_s = s[0] * 1000 + s[1] * 100 + s[2] * 10 + s[3];
			n = l - s;
			str[0] = (n / 1000) % 10;
			str[1] = (n / 100) % 10;
			str[2] = (n / 10) % 10;
			str[3] = n % 10;
		}
		std::cout << cnt << std::endl;
	}
	return 0;
}

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

int main(){
  int n, c[4];
  while((cin >> n), n){
    if(n%1111==0){
      cout << "NA\n";
      continue;
    }
    int num = 0;
    while(n!=6174){
      for(int i=0; i<4; i++){
	c[i] = n%10;
	n/=10;
      }
      sort(c,c+4);
      int p=0, q=0;
      for(int i=0; i<4; i++){
	p*=10; q*=10;
	p+=c[i]; q+=c[3-i];
      }
      n = q-p;
      num++;
    }
    cout << num << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <cctype>
#include <utility>
#include <sstream>
#include <iomanip>
#include <string>
#include <array>
#include <algorithm>
using namespace std;

#define _gp(l) const auto gcu{getchar##l}; const auto pcu{putchar##l}
#ifdef __linux
_gp(_unlocked);
#else
_gp();
#endif
#define _DEF(r, n, ...) inline r n(__VA_ARGS__) noexcept
#define _T template <typename T>
#define _HT template <typename H,typename... T>
_T _DEF(T,in,int c){T n=0;int m=1;while(isspace(c)){c=gcu();}if(c=='-')m=-1,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;}
_DEF(int,in,){return in<int>(gcu());}
#define _SCAN(...) _DEF(bool,scan,__VA_ARGS__)
_T _SCAN(T &n){int c=gcu();return c==EOF?false:(n=in<T>(c),true);}
_DEF(bool, scan, int &c){c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_SCAN(string &s){int c;s="";
	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=(char)c;}}
#endif
_HT _SCAN(H &h,T&&... t){return scan(h)&&scan(t...);}
#define _OUT(...) _DEF(void,out,__VA_ARGS__)
#define _OUTL(...) _DEF(void,outl,__VA_ARGS__)
_OUT(bool b){pcu('0'+b);} _OUT(const char *s){while(*s)pcu(*s++);} _OUT(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_OUT(string s){for(char c:s)pcu(c);}
#endif
_T _OUT(T n){static char b[20];char *p=b;T m=n<0?pcu('-'),-1:1;
	if(!n)*p++='0';else while(n)*p++=(char)(n%10*m+'0'),n/=10;while(p!=b)pcu(*--p);}
_OUTL(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _OUT(vector<T> v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);}
#endif
_HT _OUT(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _OUTL(T&&... t){out(move(t)...);outl();}
struct range{
	int e,b=0,s=1; range(int _b,int _e,int _s):e(_e),b(_b),s(_s){} range(int _b,int _e): e(_e), b(_b){} range(int _e):e(_e){}
	struct it { int v, s; it (int _v, int _s) : v(_v), s(_s) {} operator int()const{return v;} operator int&(){return v;} int operator*()const{return v;}
		it& operator++(){v+=s;return *this;} }; it begin() {return {b, s};} it end() {return {e, s};}};

int loop(int n) {
	static array<int, 10000> m {};
	if (n == 6174)
		return 0;
	else if (m[n])
		return m[n];
	stringstream s;
	s << setw(4) << setfill('0') << n;
	string a = s.str();
	sort(begin(a), end(a));
	int S {stoi(a)};
	reverse(begin(a), end(a));
	int L {stoi(a)};
	return m[n] = loop(L - S) + 1;
}

int main() {
	for (;;) {
		string a;
		scan(a);
		if ([&] {
			for (int i: range(1, 4))
				if (a[0] != a[i])
					return false;
			return true;}()) {
			if (a[0] == '0')
				return 0;
			else {
				outl("NA");
				continue;
			}
		}
		outl(loop(stoi(a)));
	}
}