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 <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;
	}

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

using namespace std;


bool invalid(string s){
  char c = s[0];

  for(int i = 0; i < s.length(); i++)
    if(s[i] != c) return false;

  return true;
}

string tos(int n){
  string out = "";

  for(int i = 0; i < 4; i++){
    out += n%10+'0';
    n/=10;
  }
  reverse(out.begin(),out.end());
  return out;
}

string solve(string s){
  string A,B;

  sort(s.begin(),s.end());
  A = s;
  reverse(s.begin(),s.end());
  B = s;

  int a,b;
  stringstream s1(A),s2(B);

  s1 >> a;
  s2 >> b;
  return tos(b-a);
}

int main(){
  string s;
  while(cin >> s && s != "0000"){
    int ans = 0;

    if(invalid(s)){
      cout << "NA" << endl;
      continue;
    }

    while(s!="6174"){
      s = solve(s);
      ans++;
    }

    cout << ans << endl;
  }
  return 0;
}

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

int main(){
  int a[4];
  int n;
  
  while(1){
    cin>>n;
    if(n==0) break;
    int nmax,nmin;
    int co=0;
    while(1){
      if(n==0 || n==6174) break;
      co++;
      for(int i=0;i<4;i++){
	a[i] = n%10;
	n/=10;
      }
      nmax=0,nmin=0;
      sort(a,a+4);
      for(int i=0;i<4;i++){
	nmin = nmin*10+a[i];
	nmax = nmax*10+a[3-i];
      }
      n=nmax-nmin;
    }
    if(n==0) cout<<"NA"<<endl;
    else cout <<co<<endl;
  }


  return 0;
}

C++: #include<iostream>
#include<string>
#include<sstream>
#include<cmath>
#include<algorithm>
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());
  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){
    if(allsame(n)){
      cout<<"NA"<<endl;
      continue;
    }
    while(n!=6174){
      ans++;
      int l,s;
      string tmp_n=toSortString(n);
      l=make_l(tmp_n);
      s=make_s(tmp_n);
      n=l-s;
    }
    if(n==6174){
      cout<<ans<<endl;
    }
  }
}

C++: #include<stdio.h>
#include<algorithm>
#include<functional>
using namespace std;
int main(void){
	int n,i,a,j;
	int de,syo,tem[4];
	while(1){
		scanf("%d",&n);
		if(n==0000)
			break;
		int cnt=0,flg=0;
		while(n!=6174){
			de=syo=0;
			for(i=0;i<4;i++){
				a=1;
				for(j=0;j<3-i;j++){
					a*=10;
				}
				tem[i]=n/a;
				n-=tem[i]*a;
			}
			if(tem[0]==tem[1]==tem[2]==tem[3]){
				flg=1;
				break;
			}
			sort(tem,tem+4);
			a=1000;
			for(i=0;i<4;i++){
				syo+=tem[i]*a;
				a/=10;
			}
			sort(tem,tem+4,greater<int>());
			a=1000;
			for(i=0;i<4;i++){
				de+=tem[i]*a;
				a/=10;
			}
			cnt++;
			n=de-syo;
		}
		if(flg==0)
			printf("%d\n",cnt);
		else 
			printf("NA\n");
	}
	return 0;
}

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

int s(int n, bool f) {
  vector<int> a;
  while (n) {
    a.push_back(n % 10);
    n /= 10;
  }

  sort(a.begin(), a.end());
  if (f) reverse(a.begin(), a.end());

  int res = 0;
  for (int i = 0; i < a.size(); ++i) {
    res *= 10;
    res += a[i];
  }

  return res;
}

int main() {
  int n;

  while (cin >> n, n) {
    if (n % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }

    int cnt = 0;
    while (n != 6174) {
      cnt++;
      n = s(n, true) - s(n, false);
    }

    cout << cnt << endl;
  }
}