Go: package main

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

func main() {
	var v int
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		count := 0
		for v != 6174 {
			str := ([]byte)(strconv.Itoa(v))
			sort.Slice(str, func(i, j int) bool {
				return str[i] < str[j]
			})
			s, _ := strconv.Atoi(string(str))
			sort.Slice(str, func(i, j int) bool {
				return str[i] > str[j]
			})
			l, _ := strconv.Atoi(string(str))
			v = l - s
			count++
		}
		fmt.Println(count)
	}
}

C++: #include<stdio.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<string.h>
#include<map>
#include<queue>

using namespace std;

typedef pair<int,int> pii;

#define rep(i,j) for(int i=0;i<(j);i++)
#define reps(i,j,k) for(int i=j;i<=k;i++)
#define in(i,j,k) ((i)>=(j)&&(i)<=(k))

int main(){
	char n[5],l[5],h[5],*k;
	int cnt,t;
	k = "6174";
	while(~scanf("%s",n)){
		cnt=0;
		strcpy(l,n);
		strcpy(h,n);
		sort(l,l+4);
		sort(h,h+4,greater<char>());
		if(*h=='0')break;
		if(!strcmp(h,l)){
			puts("NA");
			continue;
		}
		while(strcmp(n,k)){
			strcpy(l,n);
			strcpy(h,n);
			sort(l,l+4);
			sort(h,h+4,greater<char>());
			for(int i=3;i>=0;i--){
				n[i]=h[i]-l[i]+'0';
				if(n[i]<'0'){
					n[i]+=10;
					h[i-1]--;
				}
			}
			cnt++;
			if(cnt>10)return 0;
		}
		printf("%d\n",cnt);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int main(){
    while(true){
	string s;
	int ans = 0;
	cin >> s;
	if(s == "0000") break;
	else if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]){
	    cout << "NA" << endl;
	    continue;
	}
	while(s != "6174"){
	    vector<int> a(4);
	    vector<int> b(4);
	    a[0] = (int)(s[0] - '0');
	    a[1] = (int)(s[1] - '0');
	    a[2] = (int)(s[2] - '0');
	    a[3] = (int)(s[3] - '0');
	    b[0] = (int)(s[0] - '0');
	    b[1] = (int)(s[1] - '0');
	    b[2] = (int)(s[2] - '0');
	    b[3] = (int)(s[3] - '0');
	    sort(ALL(b));
	    sort(RALL(a));
	    int u = 0;
	    vector<int> c(4);
	    RREP(i,4){
		if(a[i] < b[i]) {
		    if(a[i-1] > 0){
			a[i-1]--;
			a[i] += 10;
		    }else if(a[i-2] > 0){
			a[i-2]--;
			a[i-1] = 9;
			a[i] += 10;
		    }else{
			a[0]--;
			a[i-2] = 9;
			a[i-1] = 9;
			a[i] += 10;
		    }
		}
		c[i] = a[i] - b[i];
	    }
	    s = to_string(1000 * c[0] + 100 * c[1] + 10 * c[2] + c[3]);
	    ans++;
	}
	cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
char s[8];
int hoge(void){
  return (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0');
}
int sort(void){
  char temp;
  for(int i = 0;i < 3; i++){
    for(int j = 3;j > i; j--){
      if (s[j] > s[j-1]){
	temp = s[j];
	s[j] = s[j-1];
	s[j-1] = temp;
      }
    }
  }
  return hoge();
}
int reverse(void){
  char temp;
  temp = s[0]; s[0] = s[3]; s[3] = temp;
  temp = s[1]; s[1] = s[2]; s[2] = temp;
  return hoge();
}
int main(void){
  int n,ans;
  for( ; ; ){
    cin >> s;
    if(s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0') break;
    if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]){
      cout << "NA" << endl;
      continue;
    }
    ans = 0;
    n = atoi(s);
    for( ; ; ){
      if(strlen(s) < 4){
	for(int i = strlen(s);i < 4; i++) s[i] = '0';
	s[4] = '\0';
      }
      if(n == 6174){
	cout << ans << endl;
	break;
      }
      n = sort() - reverse();
      sprintf(s,"%d",n);
      ans++;
    }
  }
}

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<algorithm>
using namespace std;

int main(){
    int n, d[4];
    while(cin >> n){
        if(n % 1111 == 0){
            cout << "NA" << endl;
        }else{
            int tmpn = n;
            for(int i = 3; i >= 0; i--){
                d[i] = tmpn%10;
                tmpn /= 10;
            }
            
            int ans = 0;
            while(n != 6174){
                ans++;
                sort(d, d+4);
                n = (1000*d[3] + 100*d[2] + 10*d[1] + d[0]) -
                        (1000*d[0] + 100*d[1] + 10*d[2] + d[3]);
                tmpn = n;
                for(int i = 3; i >= 0; i--){
                    d[i] = tmpn%10;
                    tmpn /= 10;
                }
            }
            
            cout << ans << endl;
        }
    }
    return 0;
}

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

using namespace std;
using i64 = int64_t;
i64 MOD = 1e9 + 7;

i64 solve(){
    
    string n;
    cin >> n;
    int cnt = 0;
    if(n == "0000")
        return MOD;
    if(n[0] == n[1] && n[0] == n[2] && n[0] == n[3]){
        cout << "NA" << endl;
        return 1;
    }
    for(; n != "6174"; ++cnt){
        auto f = [&]{
            i64 v = 0;
            int b = 1000;
            for(int i = 0; i < 4; ++i){
                v += (n[i] - '0') * b;
                b /= 10;
            }
            return v;
        };
        auto g = [&](i64 t){
            string s = "0000";
            int b = 1000;
            for(int i = 0; i < 4; ++i){
                s[i] += (t / b) % 10;
                b /= 10;
            }
            return s;
        };
        sort(n.begin(), n.end());
        i64 t1 = f();
        reverse(n.begin(), n.end());
        i64 t2 = f();
        n = g(t2 - t1);
    }
    cout << cnt << endl;
    
    return true;
}

signed main(){
    while(solve() != MOD);
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<sstream>
#include<queue>
#include<iomanip>
using namespace std;
int latte(string str){
    stringstream ss(str);
    int n;
    ss>>n;
    return n;
}
struct data{
    string str;
    int n;
    data(string a,int b):str(a),n(b){}
    data(){}
};
int main(){
    string str="";
    while(cin>>str,str!="0000"){
        int memo[10000];
        fill(memo,memo+10000,-1);
        queue<data>Q;
        Q.push(data(str,0));
        while(!Q.empty()){
            data ins=Q.front();Q.pop();
            str=ins.str;int n=ins.n;
            if(memo[latte(str)]!=-1)continue;
            memo[latte(str)]=n;
            string a,b;
            a=b=str;
            sort(a.rbegin(),a.rend());sort(b.begin(),b.end());
            int to=latte(a)-latte(b);
            stringstream ss;
            ss<<to;
            string to_;
            ss>>setw(4)>>setfill('0')>>to_;
            if(to_.length()!=4)for(int i=0;i<4-to_.length();i++)to_="0"+to_;
            //cout<<a<<"-"<<b<<"="<<to_<<endl;
            Q.push(data(to_,n+1));
        }
        if(memo[6174]==-1)cout<<"NA"<<endl;
        else cout<<memo[6174]<<endl;
    }
}