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 <queue>
#include <vector>
#include <algorithm>
#include <vector>

std::string sub(const std::string& a, const std::string& b) {
    int a_ = 0, b_ = 0;

    int k = 1;
    for(int i = 3; i >= 0; --i) {
        a_ += k * (a[i] - '0');
        b_ += k * (b[i] - '0');

        k *= 10;
    }

    char hoge[4];
    sprintf(hoge, "%04d", a_ - b_);

    return hoge;
}

int func(int i, const std::string& s) {
    if(s == "6174") {
        return i;
    }

    auto a = s, b = s;

    std::sort(a.begin(), a.end());
    std::sort(b.begin(), b.end(), std::greater<char>());

    return func(i + 1, sub(b, a));
}

inline
bool same(const std::string& a) {
    bool same = true;

    for(int i = 1; i < a.size(); ++i) {
        if(a[i - 1] != a[i]) {
            same = false;
        }
    }

    return same;
}

int main() {
    std::string  n;

    while(std::cin>> n, n != "0000") {
        if(same(n)) {
            std::cout << "NA" << std::endl;

            continue;
        }

        std::cout << func(0, n) << std::endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  while(cin>>n,n){
    for(int i=0;i<4000;i++){
      if(n==6174){cout<<i<<endl;break;}
      vector<int>v;
      for(int j=0;j<4;j++){
        v.push_back(n%10);
        n/=10;
      }
      sort(v.begin(),v.end());
      if(v[0]==v[3]){cout<<"NA"<<endl;break;}
      int p1,p2;
      p1=v[0]+v[1]*10+v[2]*100+v[3]*1000;
      p2=v[3]+v[2]*10+v[1]*100+v[0]*1000;
      n=p1-p2;
    }
  }
}

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

int main()
{
    int N, L, S;
    while(cin>>N, N) {
        vector<int> v(4);
        int ans = N;
        for(int i=0; i<4; i++) {
            v[i] = ans%10;
            ans /= 10;
        }
        if(v[0]==v[1] && v[1]==v[2] && v[2]==v[3]) {
            cout<<"NA\n";
            continue;
        }
        while(N != 6174) {
            ans++;
            for(int i=0; i<4; i++) {
                v[i] = N%10;
                N /= 10;
            }
            sort(v.begin(), v.end());
            L = v[0]*1 + v[1]*10 + v[2]*100 + v[3]*1000;
            S = v[3]*1 + v[2]*10 + v[1]*100 + v[0]*1000;
            N = L-S;
        }
        cout<<ans<<endl;
    }
}

C++: #include<stdio.h>
int main(void)
{
	int n,i,j,c,p[5],a,x=0,z[5],flg;
	while(1){
		scanf("%d",&n);
		if(n==0000) break;
		flg=0;
		c=0;
		while(1){
			if(n==6174) break;
			for(i=0;i<4;i++){
				p[i]=n%10;
				a=n/10;
				n=a;
			}
			if(p[0]==p[1]&&p[0]==p[2]&&p[0]==p[3]){
				flg=1;
				printf("NA\n");
				break;
			}
			c++;
			for(i=0;i<4;i++) z[i]=p[i];
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(z[i]<z[j]){
						x=z[i];
						z[i]=z[j];
						z[j]=x;
					}
				}
			}
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(p[i]>p[j]){
						x=p[i];
						p[i]=p[j];
						p[j]=x;
					}
				}
			}
			n=(z[0]*1000+z[1]*100+z[2]*10+z[3])-(p[0]*1000+p[1]*100+p[2]*10+p[3]);
		}
		if(flg!=1) printf("%d\n",c);
	}
	return 0;
}

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

string operator-(const string &a, const string &b) {
	char buff[128];
	sprintf(buff, "%04d", stoi(a) - stoi(b));
	return string(buff);
}

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

	const string goal = "6174";

	for(string n; cin >> n && n != "0000";) {
		if(all_of(n.begin(), n.end(), [&](char c) { return c == n.front(); })) {
			cout << "NA" << endl;
			continue;
		}

		int cnt = 0;
		while(n != goal) {
			string L(n), S(n);
			sort(L.rbegin(), L.rend());
			sort(S.begin(), S.end());
			n = L - S;
			++cnt;
		}

		cout << cnt << endl;
	}

	return EXIT_SUCCESS;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <climits>
#include <algorithm>
#include <map>
#include <sstream>
using namespace std;

int toInt(string s) {
    stringstream ss(s);
    int ret;
    ss >> ret;
    return ret;
}

string toStr(int n) {
    stringstream ss;
    ss << n;
    return ss.str();
}

string next(string N) {
    sort(N.begin(), N.end());
    int S = toInt(N);
    reverse(N.begin(), N.end());
    int L = toInt(N);
    return toStr(L-S);
}

int main() {
    string N;
    while (cin >> N, N != "0000") {
        if (N[0] == N[1] && N[1] == N[2] && N[2] == N[3]) {
            cout << "NA" << endl;
            continue;
        }
        int cnt = 0;
        while (N != "6174") {
            N = next(N);
            ++ cnt;
        }
        cout << cnt << endl;
    }
    return 0;
}

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

int main()
{
    string n;
    while(cin >> n, n != "0000"){
        if(n[0] == n[1] && n[1] == n[2] && n[2] == n[3]){
            cout << "NA" << endl;
            continue;
        }
        int ans = 0;
        while(n != "6174"){
            string l = n, s = n;
            sort(l.begin(), l.end(), greater<char>()); sort(s.begin(), s.end());
            int t = atoi(l.c_str()) - atoi(s.c_str());
            for(int i = 3; i >= 0; --i){
                n[i] = '0' + t % 10;
                t /= 10;
            }
            ++ans;
        }
        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int d[4];
string in;
int data[10000];

void solve(){
  bool f = true;
  for(int i=1;i<4;i++) if(in[0] != in[i]) f = false;
  if(f) cout << "NA" << endl;
  else{
    int ans = 0;
    for(int i=0;i<4;i++) d[i] = in[i] - '0';
    while(1){
      if(d[0] == 6 && d[1] == 1 && d[2] == 7 && d[3] == 4) break;
      int l = 0, s = 0;
      sort(d, d+4);
      for(int i=0;i<4;i++){
        s *= 10;
        s += d[i];
        l *= 10;
        l += d[3-i];
      }
      ans++;
      int tmp  = l - s;
      if(data[tmp] != -1){
        ans += data[tmp];
        break;
      }
      for(int i=0;i<4;i++){
        d[3-i] = tmp % 10;
        tmp /= 10;
      }
    }
    int tmp = 0;
    for(int i=0;i<4;i++){
      tmp *= 10;
      tmp += d[3-i];
    }
    data[tmp] = ans;
    cout << ans << endl;
  }
}

main(){
  fill(data, data+10000, -1);
  while(cin >> in){
    if(in == "0000") break;
    solve();
  }
}