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;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int,int> pii;
#define REP(i,n) for(ll i=0;i<n;++i)
#define REPR(i,n) for(ll i=1;i<n;++i)
#define FOR(i,a,b) for(ll i=a;i<b;++i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define MOD (ll)(1e9+7)
#define ADD(a,b) a=((a)+(b))%MOD
#define FIX(a) ((a)%MOD+MOD)%MOD

int toint(string a){
  return atoi(a.c_str());
}
string tostr(int a){
  string ret = "";
  REP(_,4){
    ret = (char)(a%10+'0') + ret;
    a /= 10;
  }
  return ret;
}

int main(){
  while(true){
    string n;
    cin>>n;
    if(n == "0000")break;
    if(n[0]==n[1] && n[1]==n[2] && n[2]==n[3]){
      cout<<"NA"<<endl;
      continue;
    }
    int step = 0;
    while(n != "6174"){
      string l,s;
      sort(ALL(n));
      s = n;
      reverse(ALL(n));
      l = n;
      n = tostr(toint(l)-toint(s));
      ++step;
    }
    cout<<step<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int solve(int num,int& cnt) {
	if (num == 6174)return cnt;
	cnt++;
	int s[5];
	for (int i = 0; i < 4; i++) {
		s[i] = num % 10;
		num -= s[i];
		num /= 10;
	}
	sort(s, s + 4);
	return solve((s[3] - s[0]) * 1000 + (s[2] - s[1]) * 100 + (s[1] - s[2]) * 10 + (s[0] - s[3]), cnt);
}
int main() {
	int n;
	while (cin >> n) {
		if (!n)return 0;
		else if (n % 1111 == 0) {
			cout << "NA\n";
			continue;
		}
		int cnt;
		solve(n, cnt);
		cout << cnt << endl;
	}
}

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

int solve(int);

int main(){
	int N,ans=0;
	while(true){
		cin >> N;
		if(N == 0)
			break;
		if(N/1000 == (N%1000)/100 && (N%1000)/100 == (N%100)/10 && (N%100)/10 == N%10)
			cout << "NA" << endl;
		else{
			while(N != 6174){
				N = solve(N);
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

int solve(int N){
	int a[4],L,S;
	a[0] = N/1000;
	a[1] = (N%1000)/100;
	a[2] = (N%100)/10;
	a[3] = N%10;
	sort(a, a+4);
	S = a[3] + a[2]*10 + a[1]*100 + a[0]*1000;
	L = a[0] + a[1]*10 + a[2]*100 + a[3]*1000;
	return L-S;
}

C++: #include<stdio.h>
int main(void)
{
	int n,h[4],s[4],k[4],i,j,temp,flg,cnt;
	while(1){
		cnt=0;
		scanf("%d",&n);
		if(n==0000)break;
		if(n<1000){
			h[0]=0;
		}
		else{
			h[0]=n/1000;
		}
		if(n<100){
			h[1]=0;
		}
		else{
			h[1]=(n%1000)/100;
		}
		if(n<10){
			h[2]=0;
		}
		else{
			h[2]=((n%1000)%100)/10;
		}
		h[3]=(((n%1000)%100)%10)/1;
		while(1){
			if(h[0]==h[1]&&h[0]==h[2]&&h[0]==h[3]){
			printf("NA\n");
			break;
		}
			if(n==6174)break;
			cnt++;
			for(i=0;i<4;i++){
				s[i]=h[i];
				k[i]=h[i];
			}
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(s[i]>s[j]){
						temp=s[i];
						s[i]=s[j];
						s[j]=temp;
					}
				}
				for(j=i+1;j<4;j++){
					if(k[i]>k[j]){
						temp=k[i];
						k[i]=k[j];
						k[j]=temp;
					}
				}
			}
			n=(s[0]*1000+s[1]*100+s[2]*10+s[3]*1)+(k[0]*1000+k[1]*100+k[2]*10+k[3]*1);
			if(n==6174)break;
			if(n<1000){
			h[0]=0;
			}
			else{
				h[0]=n/1000;
			}
			if(n<100){
				h[1]=0;
			}
			else{
				h[1]=(n%1000)/100;
			}
			if(n<10){
				h[2]=0;
			}
			else{
				h[2]=((n%1000)%100)/10;
			}
			h[3]=(((n%1000)%100)%10)/1;
		}
		if(cnt>0){
			printf("%d\n",cnt);
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
  char c[4];
  while(cin>>c[0]>>c[1]>>c[2]>>c[3]){
    if(c[0]=='0'&&c[1]=='0'&&c[2]=='0'&&c[3]=='0') break;
    int su[4];
    int mems[4];
    int count = 0;
    int d = 1000;
    int h=1;
    for(int i=0;i<4;i++) su[i] = c[i] - '0';
    while(su[0]!=6||su[1]!=1||su[2]!=7||su[3]!=4){
      if(su[0]==0&&su[1]==0&&su[2]==0&&su[3]==0){
	cout << "NA" << endl;
	h = 0;
	break;
      }
      sort(su,su+4);
      for(int i=0;i<4;i++) mems[i] = su[i];
      sort(su,su+4,greater<int>());
      for(int i=0;i<4;i++) su[i] -= mems[i];
	for(int i=4;i>=1;i--){
	  if(su[i]<0){
	    su[i-1] -= 1;
	    su[i] += 10;
	  }
	}
	count++;
      }
    if(h==1) cout << count << endl;
  }
  return 0;
}

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

using namespace std;

int main()
{
	int num[4];
	int n;
	int l, s;
	
	while (cin >> n, n){
		int ct = 0;
		if (n % 1111 == 0){
			cout << "NA" << endl;
			continue;
		}
		while(n != 6174){
			l = s = 0;
			for (int i = 0; i < 4; i++){
				num[i] = n % 10;
				n /= 10;
			}
			sort(num, num + 4);
			
			for (int i = 0; i < 4; i++){
				s = s * 10 + num[i];
				l = l * 10 + num[3 - i];
			}
			n = l - s;
			ct++;
		}
		cout << ct << endl;
	}
	return (0);
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>

using namespace std;

int main(){
	int work[4],L,S,Result,count;
	char buf[5];

	while(true){
		scanf("%s",buf);

		if(buf[0] == '0' && buf[1] == '0' && buf[2] == '0' && buf[3] == '0')break;

		if(buf[0] == buf[1] && buf[0] == buf[2] && buf[0] == buf[3]){
			printf("NA\n");
			continue;
		}

		work[0] = buf[0] - '0';
		work[1] = buf[1] - '0';
		work[2] = buf[2] - '0';
		work[3] = buf[3] - '0';

		count = 0;

		while(true){
			if(work[0] == 6 && work[1] == 1 && work[2] == 7 && work[3] == 4)break;

			count++;
			for(int i = 1; i <= 3; i++){
				for(int k = 3; k >= i; k--){
					if(work[k] > work[k-1])swap(work[k],work[k-1]);
				}
			}

			L = 1000*work[0]+100*work[1]+10*work[2]+work[3];

			for(int i = 1; i <= 3; i++){
				for(int k = 3; k >= i; k--){
					if(work[k] < work[k-1])swap(work[k],work[k-1]);
				}
			}
			S = 1000*work[0]+100*work[1]+10*work[2]+work[3];
			Result = L - S;

			for(int i = 0,k=1000; i < 4; i++, k /= 10){
				work[i] = Result/k;
				Result %= k;
			}
		}
		printf("%d\n",count);
	}

	return 0;
}

C++: #include<bits/stdc++.h>
#define endl '\n'
#define INF (1 << 21)
const int MOD = 1000000007;
using namespace std;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  string str;
  int n;
  int l, s;
  int ans;

  cin >> str;
  while(str != "0000"){
    if(str == "6174"){
      cout << 0 << endl;
    }else if(str[0] == str[1] && str[1] == str[2] && str[2] == str[3]){
      cout << "NA" << endl;
    }else{
      ans = 0;

      while(1){
	l = s = 0;
	ans ++;

	int r = 1;
	sort(str.begin(), str.end());
	for(int i = 3;i >= 0;i--){
	  s += (str[i] - '0') * r;
	  r *= 10;
	}
	
	r = 1;
	reverse(str.begin(), str.end());
	for(int i = 3;i >= 0;i--){
	  l += (str[i] - '0') * r;
	  r *= 10;
	}
	
	if((n = l - s) == 6174){
	  break;
	}else{
	  for(int i = 3;i >= 0;i--){
	    str[i] = (n % 10) + '0';
	    n /= 10;
	  }
	}
      }
      cout << ans << endl;
    }      
    cin >> str;
  }
  return 0;
}