Go: package main

import (
	"fmt"
	"sort"
	"strconv"
)

var memo map[int]int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if _, ok := memo[v]; !ok {
		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))
		memo[v] = solve(l-s) + 1
	}
	return memo[v]
}

func main() {
	var v int
	memo = make(map[int]int)
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

C++: #include<iostream>
#include<algorithm>
#include<functional>
#include<string>
#include<boost/lexical_cast.hpp>
using namespace std;

int main(){
  while(1){
    string N, L, S;
    cin >> N;
    if(N == "0000") break;
    
    int ans = 0;
    while(1){
      if(N == "6174"){
        cout << ans << endl;
        break;
      }
      
      L = N;
      S = N;
      
      sort(L.begin(), L.end(), greater<char>());
      sort(S.begin(), S.end());
      
      if(L == S){
        cout << "NA" << endl;
        break;
      }
      
      int n = boost::lexical_cast<int>(L) - boost::lexical_cast<int>(S);
      N = boost::lexical_cast<string>(n);
      
      ans++;
    }
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define F first
#define S second
#define endl '\n'
#define lcm( a, b ) ((a)/__gcd(a,b)*(b))
#define MP make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()

using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<int, P> i_P;
typedef pair< LL, LL > L_P;

const int INF = INT_MAX;
const int MIN = INT_MIN;
const LL LINF = LLONG_MAX;
const int MOD = 1000000000 + 7;
const int SIZE = 100005;

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

    while( true ) {
        string nu;
        int n;
        cin >> nu;
        if( nu == "0000" ) break;
        int res = 0;

        bool flg = true;
        int cnt[10] = {};

        int dig = 1;
        n = 0;
        for( int i = 3; i >= 0; --i ) {
            n += ( nu[i] - '0' ) * dig;
            dig *= 10;
        }

        int t = n;
        while( t > 0 ) {
            ++cnt[t % 10];
            t /= 10;

        }
        for( int i = 0; i < 10; ++i ) {
            if( cnt[i] == 4 ) {
                  flg = false;
                  break;

            }
        }

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

        }


        while( n != 6174 ) {
            int l = 0, s = 0;
            vector< int > tmp;
            /*while( n > 0 ) {
                tmp.pb( n % 10 );
                n /= 10;

            }*/

            tmp.pb( n % 10 ); n /= 10;
            tmp.pb( n % 10 ); n /= 10;
            tmp.pb( n % 10 ); n /= 10;
            tmp.pb( n % 10 ); n /= 10;
            sort( tmp.begin(), tmp.end() );
            int dig = 1;
            int pos = 0;
            while( pos < tmp.size() ) {
               s += tmp[pos] * dig;
               dig *= 10;
               ++pos;

            }
            pos = tmp.size() - 1;
            dig = 1;
            while( pos >= 0 ) {
                l += tmp[pos] * dig;
                dig *= 10;
                --pos;

            }
            n = s - l;
            ++res;

        }
        cout << res << endl;

    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);(i)++)

string N;

int solve(){
  string s = (N);
  int cou = 0;
  while(s != "6174"){
    sort(s.begin(),s.end());
    if(s.front() == s.back()) return -1;
    int S = atoi(s.c_str());
    reverse(s.begin(),s.end());
    int L = atoi(s.c_str());
    s = to_string(L - S);
    cou++;
  }

  return cou;
}
int main(){
  while(cin >> N,N != "0000"){
    int ans = solve();
    if(ans == -1){
      cout << "NA" << endl;
    }
    else{
      cout << ans << endl;
    }
  }
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
bool NA(string s) {
	if (s == "1111" || s == "2222" || s == "3333" || s == "4444" || s == "5555" || s == "6666" || s == "7777" || s == "8888" || s == "9999")return true;
	else return false;
}
signed main() {
	string s;
	while (cin >> s, s != "0000") {
		int ans(0);
		while (s != "6174") {
			if (NA(s)) {
				cout << "NA" << endl;
				goto here;
			}
			string big(s), small(s);
			sort(all(small));
			sort(all(big), greater<char>());
			s = to_string(stoi(big) - stoi(small));
			ans++;
		}
		cout << ans << endl;
	here:;

	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

map<string, int> mp;

int dfs(string curr) {
  if (mp.find(curr) != mp.end()) return mp[curr];
  if (curr == "6174") return 0;
  string L(curr), S(curr);
  sort(L.rbegin(), L.rend());
  sort(S.begin(), S.end());
  string next = to_string(stoi(L) - stoi(S));
  while (next.size() < 4) next = "0" + next;
  return mp[curr] = dfs(next) + 1;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  string N;
  while (cin >> N, N != "0000") {
    if (N[0] == N[1] && N[0] == N[2] && N[2] == N[3]) {
      cout << "NA" << endl;
      continue;
    }
    cout << dfs(N) << endl;
  }

  return 0;
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

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

int main(){
	string str;
	int n;
	while(cin>>str){
		if(str=="0000")return 0;
		stringstream ss(str);
		n =atoi(ss.str().c_str());
		//cout <<n<<endl;
		vector<int> L;
		vector<int> S;
		int cnt = 0;
		bool ok =true;
		while(n!=6174){
			cnt++;
			L.clear();
			S.clear();
			//cout <<n<<endl;
			while(n){
				int x;
				x =(n%10);
				n /=10;
				L.push_back(x);
			}
			S =L;
			sort(S.begin(),S.end());
			sort(L.begin(),L.end(),greater<int>());
			/*for(int i =0;i <L.size();i++){
				cout <<S[i]<<endl;
			}
			cout <<endl;
			for(int i =0;i <S.size();i++){
				cout <<L[i]<<endl;
			}
			cout <<endl;*/
			int c =4;
			int l,s;
			l = s=0;
			while(c--){
				l +=(L[3-c]*pow(10.0,c));
				s +=(S[3-c]*pow(10.0,c));
			}
			n =(l-s);
			//cout <<l<<" "<<s<<endl;
			/*if(cnt==3){
				return 0;
			}*/
				if(n==0){
					cout <<"NA"<<endl;
					ok =false;
					break;
				}
			}
			if(ok){
				cout <<cnt<<endl;
			}
		}
		return 0;
	}