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 <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

int main(){
  string s;

  while(cin >> s, s != "0000"){
    if(s[0] == s[1] && s[0] == s[2] && s[0] == s[3]){
      cout << "NA" << endl;
      continue;
    }

    int ans = 0;

    while(s != "6174"){
      sort(s.begin(), s.end());
      string S = s;
      sort(s.rbegin(), s.rend());
      string L = s;
      int N = atoi(L.c_str()) - atoi(S.c_str());

      char buff[10];
      sprintf(buff, "%04d", N);
      buff[4] = 0;
      s = string(buff);

      ans++;
    }

    cout << ans << endl;
  }
}

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

int main(){
    vector<int> n;
    int input;
    while(cin >> input){
        int ans = 0;
        if(input == 0) break;
        if(input % 1111 == 0){
            cout << "NA" << endl;
            continue;
        }
        while(input != 6174) {
            for(int i=0; i < 4; i++){
                n.push_back(input%10);
                input /= 10;
            }
            vector<int> minNum ,maxNum;

            sort(n.begin(), n.end());
            minNum = n;
            reverse(n.begin(), n.end());
            maxNum = n;

            int L=0, S=0;
            for(int i=0, m = 1000; i < 4; i++, m/=10){
                L += maxNum[i]*m;
                S += minNum[i]*m;
            }
            input = L-S;
            ans++;

            n.clear();
        }
        cout << ans << endl;
    }
}

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

bool isSameNumber(vector<int> n){
    bool isSame = true;
    for(int i=0; i < n.size(); i++){
        for(int j=0; j < n.size(); j++){
            if(n[i] != n[j])isSame = false;
        }
    }
    return isSame;
}

int main(){
    vector<int> n;
    int input;
    while(cin >> input){
        int ans = 0;
        bool isok = true;
        if(log10(input) < 3) n.push_back(0);
        if(input == 0) break;
        while(input != 6174) {
            while(input != 0){
                n.push_back(input%10);
                input /= 10;
            }
            if(isSameNumber(n)){
                cout << "NA" << endl;
                n.clear();
                isok = false;
                break;
            }
            vector<int> minNum ,maxNum;

            sort(n.begin(), n.end());
            minNum = n;
            reverse(n.begin(), n.end());
            maxNum = n;

            int p=0, q=0;
            for(int i=0, m = 1000; i < 4; i++, m/=10){
                p += minNum[i]*m;
                q += maxNum[i]*m;
            }
            input = q-p;
            ans++;

            n.clear();
        }
        if(isok)cout << ans << endl;
    }
}

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

string convert( string trg ){
    sort(trg.begin(), trg.end());
    int a = 0, b = 0, mul = 1;
    for(string::iterator it = trg.begin(); it != trg.end(); it++){
        int n = (*it)-'0';
        a += n * mul;
        b += n * 1000 / mul;
        mul *= 10;
    }
    a = a-b;
    char tmp[8] = {a/1000+'0', a/100%10+'0', a/10%10+'0', a%10+'0'};
    string res(tmp);
    return res;
}

int main(){
    string ss;
    string FIN("0000"), GOAL("6174");
    while( cin >> ss ){
        if( ss == FIN ) break;
        if( ss[0] == ss[1] && ss[1] == ss[2] && ss[2] == ss[3] ){
            cout << "NA" << endl;
            continue;
        }
        int c = 0;
        while( c < 10 && ss!=GOAL ){
            c++;
            ss = convert(ss);
        }
        cout << c << endl;

    }
    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <sstream>

#define rep(i, j) FOR(i, 0, j)
#define FOR(i, j, k) for(int i = j; i < k; ++i)

using namespace std;

int main(){
  char n[5];
  while(scanf("%s", n) && strcmp(n, "0000") != 0){
    if(n[0] == n[1] && n[1] == n[2] && n[2] == n[3]){ puts("NA"); continue;}
    int c = 0;
    while(strcmp(n, "6174") != 0 && c < 10){
      rep(i, 4) FOR(j, i + 1, 4){
	if(n[i] > n[j]){ char tmp = n[i]; n[i] = n[j]; n[j] = tmp;}
      }
      int a = atoi(n);
      char tmp = n[0]; n[0] = n[3]; n[3] = tmp;
      tmp = n[1]; n[1] = n[2]; n[2] = tmp;
      int b = atoi(n);
      b -=  a;
      sprintf(n, "%d", b);
      c++;
    }
    printf("%d\n", c);
  }
  return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> pl;

int main()
{
	while (1) {
		string s;
		cin >> s;
		if (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0')
			break;
		if (s == "6174") {
			cout << 0 << endl;
		}
		else if (s[0] == s[1] &&  s[2] == s[3] && s[1] == s[2])
			cout << "NA" << endl;
		else {
			ll ans = 0;
			while (s != "6174") {
				int a[4];
				for (int i = 0; i < 4; i++)
					a[i] = s[i] - '0';

				sort(a, a + 4);

				ll small=0,large=0;
				for (int i = 0; i < 4; i++)
					large += a[i] * (ll)pow(10, i);
				
				reverse(a,a+4);
				for (int i = 0; i < 4; i++) 
					small += a[i] * (ll)pow(10, i);
				
				s = to_string(large - small);
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

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

int main() {
  string s;
  while(cin >> s && s!="0000") {
    int cnt=0;
    bool ck=true;
    while(s!="6174") {
      cnt++;
      int a[4];
      fill(a,a+4,0);
      for(int i=0; i<4; i++) a[i]=(int)s[i]-48;
      sort(a,a+4);
      int b=0,c=0,k1=1,k2=1000;
      for(int i=0; i<4; i++) {
	b+=a[i]*k1;
	c+=a[i]*k2;
	k1*=10;
	k2/=10;
      }
      stringstream ss;
      ss << b-c;
      ss >> s;
      if(s.size()<4) {
	while(s.size()<4) s="0"+s;
      }
      if(s=="0000") {
	cout << "NA" << endl;
	ck=false;
	break;
      }
    }
    if(ck) cout << cnt << endl;
  }
  return 0;
}

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

int main(){
  int t[4];
  int tt[4];
  int a,b,bb,c;
  while(true){
    cin>>a;
    c=0;
    if(a==0)break;
    for(int i=1111;i<10000;i+=1111){
      if(i==a){
	cout<<"NA"<<endl;
	c=-1;
      }
    }
    if(c==-1)continue;
    while(a!=6174){
      b=a;
      c++;
      for(int i=3;i>=0;i--){
	t[i]=b%10;
	b/=10;
      }
      sort(t,t+4);
      reverse(t,t+4);
      b=t[0]*1000+t[1]*100+t[2]*10+t[3];

      bb=a;
      for(int i=3;i>=0;i--){
	t[i]=bb%10;
	bb/=10;
      }
      sort(t,t+4);
      bb=t[0]*1000+t[1]*100+t[2]*10+t[3];
      
      a=b-bb;
      //cout<<a<<endl;
    } 
    cout<<c<<endl;
    
  }
  return 0;
}