Go: package main

import (
	"fmt"
	"sort"
)

var memo []int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if memo[v] == 0 {
		a := make([]int, 4)
		for i := 0; i < 4; i++ {
			a[i] = v % 10
			v /= 10
		}
		sort.Slice(a, func(i, j int) bool {
			return a[i] > a[j]
		})
		l := int(0)
		for i := 0; i < 4; i++ {
			l *= 10
			l += a[i]
		}
		sort.Slice(a, func(i, j int) bool {
			return a[i] < a[j]
		})
		s := int(0)
		for i := 0; i < 4; i++ {
			s *= 10
			s += a[i]
		}
		memo[v] = solve(l-s) + 1
	}
	return memo[v]
}

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

C++: #include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12 
//priority_queue<int,vector<int>, greater<int> > q2;



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

	int N = 1;
	while (N) {
		string S; cin >> S;
		N = (S[0] - '0') * 1000 + (S[1] - '0') * 100 + (S[2] - '0') * 10 + (S[3] - '0');
		if (!N)break;
		VI A(4), B(4);
		A[0] = B[0] = N % 10;
		A[1] = B[1] = (N / 10) % 10;
		A[2] = B[2] = (N / 100) % 10;
		A[3] = B[3] = N / 1000;
		SORT(A);
		rSORT(B);
		if (A == B) {
			cout << "NA" << '\n';
			continue;
		}
		int ans = 0;
		while (N != 6174) {
			ans++;
			int b = B[0] * 1000 + B[1] * 100 + B[2] * 10 + B[3];
			int a = A[0] * 1000 + A[1] * 100 + A[2] * 10 + A[3];
			N = b - a;
			A[0] = B[0] = N % 10;
			A[1] = B[1] = (N / 10) % 10;
			A[2] = B[2] = (N / 100) % 10;
			A[3] = B[3] = N / 1000;
			SORT(A);
			rSORT(B);
		}
		cout << ans << 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(){
	int n;
	int cnt;
	int num[4];
	while(scanf("%d",&n)&&n){
		cnt = 0;
		if(n%1111==0){
			cout <<"NA"<<endl;
			continue;
		}
		while(n!=6174){
			cnt++;
			int tmp =n;
			for(int i =0;i <4;i++){
				num[i]=tmp%10;
				tmp /=10;
			}
			int x = 0;
			sort(num,num+4,greater<int>());
			for(int i =0;i <4;i++){
				x =x*10+num[i];
			}
			int L,S;
			L =x;
			sort(num,num+4);
			x = 0;
			for(int i =0;i <4;i++){
				x =x*10+num[i];
			}
			S =x;
			n =L-S;
		}
		cout <<cnt<<endl;
	}
	return 0;
}

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

int main()
{
  int n;
  int l, s;
  vector<int> vi1;
  vector<int> vi2;
  int cnt;

  while(1){
    cin >> n;
    if(n == 0) break;

    cnt = 0;

    while(1){
      l = s = 0;
      vi1.clear();
      vi2.clear();
      if(n == 6174){
	cout << cnt << endl;
	break;
      }else if(n == 0){
	cout << "NA" << endl;
	break;
      }

      for(int i = 0;i < 4;++i){
	vi1.push_back(n % 10);
	vi2.push_back(n % 10);
	n /= 10;
      }
      
      //l,sを求める
      sort(vi1.begin(), vi1.end());
      sort(vi2.begin(), vi2.end(), greater<int>());

      /*
      cout << "vi1:";
      for(int i = 0;i < 4;++i){
	cout << vi1[i] << ' ';
      }
      cout << endl;
      cout << "vi2:";
      for(int i = 0;i < 4;++i){
	cout << vi2[i] << ' ';
      }
      cout << endl;
      */
      
      for(int i = 0;i < 4;++i){
	l *= 10;
	l += vi1[i];
	
	s *= 10;
	s += vi2[i];
      }

      /*
      cout << s << "-" << l << "=" << s - l << endl;
      */
      
      //新しいNを求める
      n = s - l;
      ++cnt;
    }

  }

  return 0;
}

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

#define DEBUG(x) cerr << #x << " = " << x << endl

template<class T, class U> T lexical_cast(const U &from) {
    stringstream ss;
    ss << from;
    T to;
    ss >> to;
    return to;
}

bool same(const string &s) {
    return s[0] == s[1] && s[0] == s[2] && s[0] == s[3];
}

int main() {
    for(string S; cin >> S, S != "0000"; ) {
        if(same(S)) {
            cout << "NA" << endl;
            continue;
        }
        int ans = 0;
        while(S != "6174") {
            sort(begin(S), end(S));
            string L = "    ";
            for(int i = 0; i < 4; i++) L[i] = S[3 - i];
            S = lexical_cast<string>(lexical_cast<int>(L) - lexical_cast<int>(S));
            ans++;
        }
        cout << ans << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int a;
  int b=0;
  int c[8],d[8];
  int h,l;
  while(true){
    b=0;
    cin>>a;
    if(a==0000){
      break;
    }
    while(true){
      if(a==6174){
	cout<<b<<"\n";
	break;
      }
      if(a==0){
	cout<<"NA\n";
	break;
      }
      for(int i=0;i<4;i++){
	c[i]=a%10;
	a=a/10;
	d[i]=c[i];
      }
      sort(c,c+4);
      sort(d,d+4,greater<int>());
      for(int i=0;i<4;i++){
	if(i==0){
	h=c[i]*1000;
	l=d[i]*1000;
	}
	if(i==1){
	   h=c[i]*100+h;
	   l=d[i]*100+l;
	}
	if(i==2){
	  h=c[i]*10+h;
	  l=d[i]*10+l;
	}
	if(i==3){
	  h=c[i]+h;
	  l=d[i]+l;
	}
      }
      a=l-h;
      b++;
    }
  }
  return(0);
}

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

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, n)  FOR(i, 0, n)

int main()
{
    int num;

    while (cin >> num, num) {
        if (!(num % 1111)) {
            cout << "NA" << endl;
        }
        else {
            int cnt = 0;
            while (num != 6174){
                int L = 0, S = 0, n[4];
                REP (i, 4) {
                    n[i] = num % 10;
                    num /= 10;
                }
                sort(n, n + 4);
                REP (i, 4) {
                    L = L * 10 + n[3 - i];
                    S = S * 10 + n[i];
                }
                cnt++;
                num = L - S;
            }
            cout << cnt << endl;
        }
    }
    return (0);
}