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 <bits/stdc++.h>
using namespace std;

typedef long int lint;
typedef long long int llint;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
typedef pair<long, long> pll;

#define EACH(i, c) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, e) for (int i = (s); i < (int)(e); ++i)
#define REP(i, n) FOR(i, 0, n)
#define endl '\n'
#define mod 1000000007
#define INF 100000000
#define fk first
#define sv second

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, 1, 0, -1, 1, -1, 1, -1};

template<typename T>
void print(const std::vector<T>& v, char sep=' ') {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i != 0) cout << sep;
    cout << v[i];
  }
  cout << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;

  while (cin >> n, n){
    int ct = 0;
    if (n % 1111 == 0){
      cout << "NA\n";
      continue;
    }
    
    while (n != 6174){
      int l, s, num[4];
      REP(i, 4){
        num[i] = n % 10;
        n /= 10;
      }
      sort(num, num + 4);
      l = s = 0;
      REP(i, 4){
        l = l * 10 + num[3 - i];
        s = s * 10 + num[i];
      }
      n = l - s;
      ct++;
    }
    cout << ct << endl;
  }
}

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

int main()
{
    string str;

    while( cin >> str )
    {
        if( str == "0000" )
            break;
        if( str[0] == str[1] && str[2] == str[3] && str[0] == str[2] )
            cout << "NA" << endl;
        else
        {
            int cnt = 0;
            while( str != "6174" )
            {
                for( int i = 0; i < 4; i++ )
                {
                    for( int j = 3; j >= i + 1; j-- )
                    {
                        if( str[j] < str[j-1] )
                        {
                            char temp = str[j];
                            str[j] = str[j-1];
                            str[j-1] = temp;
                        }
                    }
                }

                int l = 0, r = 0, d = 1;
                for( int i = 0; i < 4; i++ )
                {
                    l += ( str[i] - '0' ) * d;
                    r += ( str[3-i] - '0' ) * d;
                    d *= 10;
                }

                l -= r;
                d = 1000;
                for( int i = 0; i < 4; i++ )
                {
                    str[i] = ( l / d ) % 10 + '0';
                    d /= 10;
                }
                cnt++;
            }
            cout << cnt << endl;
        }
    }
    return 0;
}

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

#include<functional>

using namespace std;


string IntToString(int number)
{
stringstream ss;
ss << number;
return ss.str();
}

int main(){
	string s;
	while(cin>>s)
	{
		if(s=="0000")break;
		int count=0;
		while (1)
		{
			
			if(s=="6174"){
				puts("0");
				break;
			}
			
			vector<int>n;
			
			for(int i=0; i<4; i++)
			{
				
				n.push_back( (int)s[i]-48 );
			}
			
			sort( n.begin(),n.end(),greater<int>() );
			int L=0,S=0;

			if( (n[0]+n[1]) - (n[2]+n[3]) == 0){
				puts("NA");
				break; 
			}
			
			L += n[0]*1000;
			L += n[1]*100;
			L += n[2]*10;
			L += n[3];
			
			reverse(n.begin(),n.end());
			
			S += n[0]*1000;
			S += n[1]*100;
			S += n[2]*10;
			S += n[3];
			
			s.clear();
			s = IntToString(L-S);
			count++;
			
			if(L-S==6174){
				cout<<count<<endl;
				break;
			}
		}

	}
	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:https://vjudge.net/contest/241120#problem/F>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */
string solve(string S){
    string res;
    for(int i = 0; i < 10;i++){
        if(count(S.begin(),S.end(),'0'+i) == 4) return "NA";
    }
    ll cnt = 0;
    while(true){
        while(S.length() < 4){
            S = "0" + S;
        }
        if(S == "6174") break;
        string L = S;
        sort(L.rbegin(),L.rend());
        sort(S.begin(),S.end());
        S = to_string(atoll(L.c_str())-atoll(S.c_str()));
        cnt++;
    }
    res = to_string(cnt);
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    string S;
    while(cin >> S){
        if(S == "0000") break;
        cout << solve(S) << endl;
    }
    return 0;
}

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

string toString(int n){
  string ret = "";
  while(n){
    ret += (n % 10) + '0';
    n /= 10;
  }
  reverse(ret.begin(), ret.end());
  return ret;
}

int main(){
  string s;

  while(cin >> s){
    if(s == "0000") break;    
    
    if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]){
      cout << "NA" << endl;
      continue;
    }
    
    int cnt = 0;
    while(s != "6174"){
      string L = s, S = s;
      sort(L.rbegin(), L.rend());
      sort(S.begin(), S.end());
      int nex = atoi(L.c_str()) - atoi(S.c_str());
      s = toString(nex);
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

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

int main()
{
    string N;
    while ( cin >> N, N != "0000" ) {
        int cnt = 0;
        set < string > st;

        while ( N != "6174" && st.find(N) == st.end() ) {
            string L = N, S = N;
            int val = 0;
            st.insert(N);
            sort(rbegin(L), rend(L));
            sort(begin(S), end(S));
            val = atoi(L.c_str()) - atoi(S.c_str());
            N = to_string(val);
            while ( N.size() < 4 ) {
                N = '0' + N;
            }
            cnt++;
        }

        if ( N == "6174" ) {
            cout << cnt << endl;
        } else {
            cout << "NA" << endl;
        }
    }

    return ( 0 );
}