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 <iostream>
#include <math.h>


using namespace std;




int main() {
	char a;
	int n[4];
	int kari;
	int counter;
	int l;
	int s;
	int count;
	while (1) {
		counter = 0;
		count = 0;
		for (int i = 0; i < 4; i++) {
			cin >> a;
			n[i] = a - '0';
			if (n[i] == 0) {
				counter++;
			}
		}
		if (counter == 4) {
			break;
		}

		if (n[0] == n[1] && n[1] == n[2] && n[2] == n[3]) {
			cout << "NA" << endl;
			continue;
		}
		
		while (1) {
			if (n[0] == 6 && n[1] == 1 && n[2] == 7 && n[3] == 4) {
				break;
			}
			for (int i = 3; i > 0; i--) {
				for (int j = 0; j < i; j++) {
					if (n[j] < n[j+1]) {
						kari = n[j];
						n[j] = n[j+1];
						n[j+1] = kari;
					}
				}
			}
			l = n[0] * 1000 + n[1] * 100 + n[2] * 10 + n[3];
			s = n[3] * 1000 + n[2] * 100 + n[1] * 10 + n[0];
			kari = l - s;
			for (int i = 0; i < 4; i++) {
				n[i] = kari / (int)pow(10,3-i);
				kari = kari % (int)pow(10,3-i);
			}
			count++;
		}
		cout << count << endl;
	}
	return 0;
}

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)
{
	string n;
	while (cin >> n){
		if (n == "0000" || n == "0"){
			break;
		}
		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;
		string N = n;
		while (1){
			if (N == "6174"){
				cout << cnt << endl;
				break;
			}
			// cout << N << endl;
			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);
			cnt++;
		}
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int main(){
    while(true){
	string s;
	ll ans = 0;
	cin >> s;
	if(s == "0000") break;
	else if(s[0] == s[1] && s[0] == s[2] && s[0] == s[3]){
	    cout << "NA" << endl;
	    continue;
	}
	while(s.compare("6174")){
	    vector<int> a(4);
	    vector<int> b(4);
	    a[0] = (int)(s[0] - '0');
	    a[1] = (int)(s[1] - '0');
	    a[2] = (int)(s[2] - '0');
	    a[3] = (int)(s[3] - '0');
	    b[0] = (int)(s[0] - '0');
	    b[1] = (int)(s[1] - '0');
	    b[2] = (int)(s[2] - '0');
	    b[3] = (int)(s[3] - '0');
	    sort(RALL(a));
	    sort(ALL(b));
	    int an = 1000 * a[0] + 100 * a[1] + 10 * a[2] + a[3];
	    int bn = 1000 * b[0] + 100 * b[1] + 10 * b[2] + b[3];
	    s = to_string(an - bn);
	    ans++;
	}
	cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <deque>
using namespace std;
string L(string);
string S(string);
string sa(string,string);
int main(void)
{
	int count;
	string n;

	while(1){
		cin >> n;
		if(n=="0000") break;
		if(n=="1111"||n=="2222"||n=="3333"||n=="4444"||n=="5555"||n=="6666"||n=="7777"||n=="8888"||n=="9999") {
			cout << "NA" << endl;
			continue;
		}
		count=0;
		while(n!="6174"){
			n=sa(L(n),S(n));
			count+=1;
		}
		cout << count << endl;
	}
	return 0;
}
string S(string n)
{
	string s;
	deque<char> dn;
	for(int i=0;i<4;i++) dn.push_back(n[i]);
	sort(dn.begin(),dn.end());
	for(int i=3;0<=i;i--){
		s+=dn[i];
		dn.pop_back();
	}
	return s;
}
string L(string n)
{
	string l;
	deque<char> dn;
	for(int i=0;i<4;i++) dn.push_back(n[i]);
	sort(dn.begin(),dn.end());
	for(int i=0;i<4;i++){
		l+=dn[i];
	}
	return l;
}
string sa(string l,string s)
{
	string n;
	int lnum=0,snum=0,num,b=1;
	for(int i=3;0<=i;i--){
		lnum+=(b*(l[3-i]-'0'));
		snum+=(b*(s[3-i]-'0'));
		b*=10;
	}
	num=lnum-snum;
	for(int i=0;i<4;i++){
		b/=10;
		n+=((num/b)%10+'0');
	}
	//cout << n << endl;
	return n;
}

C++: #include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<algorithm>
typedef unsigned long long ull;
#define rep(i,a) for(int i=0;i<a;i++)
#define loop(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1);
const double inf = (int)1e8;

int main(void){
  string s;
  while(cin>>s){
    bool brek = true;
    rep(i,4)if(s[i] != s[0])brek = false;
    if(brek&&s[0]=='0')break;
    if(brek&&s[0]!='0'){cout<<"NA"<<endl;continue;}

    int cnt = 0;
    while(s!="6174"){
      cnt ++;
      vector<char> used;
      rep(i,4){
	used.push_back(s[i]);
      }
    
      sort(used.begin(),used.end());
      string diff;
      rep(i,used.size()){
	diff += used[used.size()-i-1] - used[i] + '0';
	if(diff[i]<'0'){diff[i-1]--; diff[i]+= 10;}
      }
      //cout<<diff<<endl;
      s=diff;
    }
    cout<<cnt<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<sstream>
#include<string>
#include<cstdlib>
#define rep(i,a) for( int i = 0; i != (a); ++i )

std::string intToStr( int n )
{
	std::ostringstream oss;
	oss << n;

	return oss.str();
}

int main()
{
	std::string L, S;
	while( std::cin >> L, L != "0000" )
	{
		bool fl = true;
		rep( i, 3 )
			fl &= L[i+1] == L[i];
		if( fl )
		{
			puts("NA");
			continue;
		}

		int ans = 0;
		while( L != "6174" )
		{
			S = L;
			std::sort( L.rbegin(), L.rend() );
			std::sort( S.begin(), S.end() );

			++ans;

			L = intToStr( atoi( L.c_str() ) - atoi( S.c_str() ) );
		}

		printf( "%d\n", ans );
	}

	return 0;
}

C++: #include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std;
char A[9],B[9];
#define I(s)strtol(s.c_str(),NULL,10)
string S(int i,int L){sprintf(A,"%%0%dd",L),sprintf(B,A,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 <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main(){
  string str;
  while(cin >> str && str != "0000"){
    if(str == "1111" || 
       str == "2222" || 
       str == "3333" || 
       str == "4444" || 
       str == "5555" || 
       str == "6666" || 
       str == "7777" || 
       str == "8888" || 
       str == "9999") cout << "NA" << endl;
    else if(str == "6174") cout << 0 << endl;
    else{
      for(int n=1;;n++){
        int L=0,S=0,LS,a,b;
        bool f[4];
        fill(f,f+4,false);

        for(int i=1;i<=1000;i*=10){
          a = b = -1;
          for(int j=0;j<4;j++){
            if(f[j]) continue;
            else if(a < str[j] - '0'){
              a = str[j] - '0';
              b = j;
            }
          }
          f[b] = true;
          L += 1000 / i * a;
          S += i * a;
        }
        LS = L - S;

        if(LS == 6174){
          cout << n << endl;
          break;
        }

        for(int i=3;i>=0;i--){
          str[i] = LS % 10 + '0';
          LS /= 10;
        }
      }
    }
  }
}