Go: package main

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

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 {
		var s string
		fmt.Scan(&s)
		v, _ = strconv.Atoi(s)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

C++: #include <vector>
#include <algorithm>
#include <iostream>
#include <functional>

using namespace std;

int func2(int *temp) {
    int n = 0;
    
    n += (temp[0] * 1000);
    n += (temp[1] * 100);
    n += (temp[2] * 10);
    n += temp[3];
       
    return n;
}

int main() {
    while (true) {
        int N = 0, Nm[4] = {0};
        unsigned int count = 0;
        int Lm[4] = {0}, Sm[4]={0};
        int S = 0, L = 0;
        
	cin >> N;
        
        Nm[0] = N / 1000;
        N %= 1000;
        Nm[1] = N / 100;
        N %= 100;
        Nm[2] = N / 10;
        N %= 10;
        Nm[3] = N;
        
        if((Nm[0] == 0 && Nm[1] == 0) && (Nm[2] == 0 && Nm[3] == 0))    break;
        
        if (!((Nm[0] == Nm[1]) && (Nm[2] == Nm[3]))) {
            while (true) {
                if (Nm[0] == 6 && Nm[1] == 1 && Nm[2] == 7 && Nm[3] == 4) {
                    break;
                }

                //Sを作成
                sort(Nm, Nm + 4);
                for (int i = 0; i < 4; i++) {   
                    Sm[i] = Nm[i];
                }   
                S = func2(Sm);

                //Lを作成
                sort(Nm, Nm + 4, greater<int>());
                for (int i = 0; i < 4; i++) {
                    Lm[i] = Nm[i];
                }
                L = func2(Lm);

                N = L - S;
                count++;

                Nm[0] = N / 1000;
                N %= 1000;
                Nm[1] = N / 100;
                N %= 100;
                Nm[2] = N / 10;
                N %= 10;
                Nm[3] = N;
            }
            cout << count << endl;
        }
        
        if(((Nm[0] == Nm[1]) && (Nm[2] == Nm[3]))) {
            cout << "NA" << 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;
  }
  while(ret.size() < 4) ret += "0";
  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<iostream>
using namespace std;
int main()
{
    int input;
    int digit[4];
    int max, min;
    int base[4]={1000,100,10,1};
    int c=0,i,j,tmp;

    while(input != 6174)
    {
        cin >> input;
        if(input == 0) return 0;
        if(input % 1111 == 0){
	    cout << "NA" << endl;
	    return 0;
        }
        for(i=0; i<4; i++) // 各桁を取り出す
        {
            digit[i]= input / base[i];
            input = input % base[i]; // Highestを除く
        }
        for(i=0;i<3;i++) // sort
        {
            for(j=i+1;j<4;j++)
            {
                if(digit[i]<digit[j])
                {
                    tmp=digit[i];
                    digit[i]=digit[j];
                    digit[j]=tmp;
                }
            }
        }
	max = min = 0;
        for(i=0;i<4;i++)
        {
            max += (digit[i]*base[i]);
            min += (digit[i]*base[3-i]);
        }
        input = max-min;
        c++;
    }
    cout << c << endl;
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define reps(i, m, n) for (int i = m; i <= n; ++i)

typedef long long i64;
typedef pair<i64, i64> pii;

constexpr int INF = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;

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

  int N;
  while (cin >> N, N) {
    int cnt = 0;
    bool isNA = false;
    while (N != 6174) {
      int tmp[20];
      rep(i, 4) {
        tmp[i] = N % 10;
        N /= 10;
      }
      if (tmp[0] == tmp[1] && tmp[1] == tmp[2] && tmp[2] == tmp[3]) {
        isNA = true;
        break;
      }
      else {
        sort(tmp, tmp + 4, greater<int>());
        int L = tmp[0] * 1000 + tmp[1] * 100 + tmp[2] * 10 + tmp[3];
        int S = tmp[3] * 1000 + tmp[2] * 100 + tmp[1] * 10 + tmp[0];
        N = L - S;
        ++cnt;
        if (N == 6174) break;
      }
    }
    if (isNA) cout << "NA" << '\n';
    else cout << cnt << '\n';
  }

  return 0;
}

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


bool allsame(int n){
  int tmp=n%10;
  n/=10;
  while(n!=0){
    if(tmp!=n%10){
      return 0;
    }
    tmp=n%10;
    n/=10;
  }
  return 1;
}

string toSortString(int n){
  stringstream ss;
  ss<<n;
  string tmp;
  ss>>tmp;
  sort(tmp.begin(),tmp.end());
  for(;tmp.size()!=4;){
    tmp+='0'+tmp;
  }
  return tmp;
}

int make_l(string s){
  int res=0;
  for(int i=0;i<s.size();i++){
    res+=(s[i]-'0')*pow(double(10),i);
  }
  return res;
}

int make_s(string s){
  int res=0;
  for(int i=0;i<s.size();i++){
    res+=(s[s.size()-1-i]-'0')*pow(double(10),i);
  }
  return res;
}

int main(){
  int n;
  int ans=0;
  while(cin>>n,n!=0){
    while(n!=6174){
      if(allsame(n)){
        cout<<"NA"<<endl;
        break;
      }
      ans++;
      int l,s;
      string tmp_n=toSortString(n);
      l=make_l(tmp_n);
      s=make_s(tmp_n);
      n=l-s;
      assert(ans<=7);
    }
    if(n==6174){
      cout<<ans<<endl;
    }
  }
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(){

    string c;
    while(1){
        cin>>c;
        if(c[0]==c[1]&&c[1]==c[2]&&c[2]==c[3]){
            if(c[0]=='0')break;
            cout<<"NA"<<endl;
            continue;
        }
        int a[4];
        for(int i=0;i<4;i++){
            a[i] = c[i] - '0';
        }
        int t=0;
        while(t++,1){
            if(a[0]==6&&a[1]==1&&a[2]==7&&a[3]==4){
                cout<<t-1<<endl;
                break;
            }
            int L,S;
            sort(a,a+4);
            L = a[0]+10*a[1]+100*a[2]+1000*a[3];
            reverse(a,a+4);
            S = a[0]+10*a[1]+100*a[2]+1000*a[3];
            int next = L - S;
            a[3] = next%10;
            a[2] = (next/10)%10;
            a[1] = (next/100)%10;
            a[0] = (next/1000)%10;
        }

    }
    return 0;
}