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

int main(){
    int cnt,number;
    int s,l;
    while(1){
        char num[5];
        cnt=0;
        cin >> num;
        if(num[0] != '0' && num[3] == '\0'){
            number = num[0]-'0';
            for(int i=0;i<3;i++){
                num[i] = 0;
            }
            num[3] = number;
        }
        if(strcmp(num,"0000") == 0) break;
        while(1){
            if(strcmp(num,"6174") == 0) break;
            if(num[0] == num[1] && num[1] == num[2] && num[2] == num[3]){
                cout << "NA" << endl;
                break;
            }
            sort(num,num + 4,greater<char>());
            l = (num[0]-'0')*1000 + (num[1]-'0')*100 + (num[2]-'0')*10 + num[3]-'0';
            sort(num,num + 4);
            s = (num[0]-'0')*1000 + (num[1]-'0')*100 + (num[2]-'0')*10 + num[3]-'0';
            l -= s;
            sprintf(num,"%d",l);
            cnt++;
        }
        if(num[0] != num[1]) cout << cnt << endl;
    }
}

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

#define DEBUG(x) cerr << #x << " = " << x << endl

template<class T, class U> T lexical_cast(const U &from) {
    stringstream ss;
    ss << from;
    T to;
    ss >> to;
    return to;
}

bool same(const string &s) {
    return s[0] == s[1] && s[0] == s[2] && s[0] == s[3];
}

int main() {
    for(string S; cin >> S, S != "0000"; ) {
        if(same(S)) {
            cout << "NA" << endl;
            continue;
        }
        int ans = 0;
        while(S != "6174") {
            sort(begin(S), end(S));
            string L = S;
            reverse(begin(L), end(L));
            S = lexical_cast<string>(lexical_cast<int>(L) - lexical_cast<int>(S));
            ans++;
        }
        cout << ans << endl;
    }
}

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

using namespace std;

int main()
{
	int m, n[4], mi[4], ma[4], na;
	int	max_int = 0, min_int = 0, n_int, ans;
	bool same;
				
	while (1){
		ans = 0;
		same = false;

		scanf("%d", &n_int);
		if (n_int == 0) break;
		if (n_int % 1111 == 0){
		 cout << "NA" << endl;
		 same = true;
		}

		for (int i = 3; i >= 0; i--){
			n[i] = n_int / (int)(pow(10, i)) % 10;
		}

		while (n_int != 6174){
			if (same) break;
			max_int = min_int = 0;

			for (int i = 3; i >= 0; i--){
				n[i] = n_int / (int)(pow(10, i)) % 10;
			}
			sort(n, n + 4);

			for (int i = 0; i < 4; i++){
				mi[3 - i] = n[i];
				ma[i] = n[i];
			}

			for (int i = 0; i < 4; i++){
				max_int += ma[i] * (int)(pow(10, i));
				min_int += mi[i] * (int)(pow(10, i));
			}
			n_int = max_int - min_int;
			
			ans++;
		}

		if (!same) cout << ans << endl;
	}
return (0);
}

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


int to_i(string &s){
	int a=0;
	for(int i=0;i<s.size();i++)a=a*10+s[i]-'0';
	return a;
}
string to_s(int n){
	string s;
	while(n){
		s+=n%10+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}



int cache[10000];

int solve(int n){
	if(n==6174)return 0;
	if(cache[n])return cache[n];
	string s=to_s(n);
	int a,b;
	sort(s.begin(),s.end());
	a=to_i(s);
	sort(s.begin(),s.end(),greater<char>());
	b=to_i(s);
	return cache[n] = 1+solve(b-a);
}


int main(){
	string s;
	while(cin>>s&&s!="0000"){
		if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3]){
			cout<<"NA\n";
			continue;
		}
		//cout<<solve(to_i(s))<<endl;
		cout<<1<<endl;
	}
	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);
	    int an = 1000 * a[0] + 100 * a[1] + 10 * a[2] + a[3];
	    int bn = 1000 * b[0] + 100 * b[1] + 10 * b[2] + b[3];
	    s = to_string(an - bn);
	    ans++;
	}
	cout << ans << endl;
    }
    return 0;
}

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

using namespace std;

int main() {
	string n;
	while (cin >> n, n != "0000") {
		if (n[0] == n[1] && n[1] == n[2] && n[2] == n[3]) cout << "NA" << endl;
		else {
			int i;
			for (i = 0; n != "6174"; ++i) {
				sort(n.begin(), n.end());
				int tmp = stoi(n);
				reverse(n.begin(), n.end());
				tmp = stoi(n) - tmp;
				n = to_string(tmp);
				while (n.size() != 4) n = '0' + n;
			}
			cout << i << endl;
		}
	}
	
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<iostream>
#include<vector>

using namespace std ;

int vtonum(vector<int> v )
{
	int num = 0 ,base = 10 ;
	for(int i = 0; i < v.size(); ++i)
	{
		if(i == 0)
		{
			num += v[i] ;
		}
		else
		{
			num += v[i] * (base) ;
			base *= 10 ;
		}
	}
	return num ;
}

int main()
{
	int N,L,S,ans,d, i = 0;
	vector<int> v ;
	while(1)
	{
		cin >> N ;
		ans = 0 ;
		if(N == 0)
		{
			break ;
		}
		while(1)
		{
			if(N == 6174)
			{
				cout << ans << endl ;
				break ;
			}
			v.resize(4) ;
			i = 0 ;
			for(d = 1000; d >= 1; d /= 10)
			{
				v[i] = N /d ;
				N %= d ;
				++i ;
			}
			if(v[0] == v[1] && v[1] == v[2] && v[2] == v[3])
			{
				cout << "NA" << endl ;
				break ;
			}
			std::sort(v.begin(),v.end()) ;
			S = vtonum(v) ;
			std::sort(v.begin(),v.end(),std::greater<int>()) ;
			L = vtonum(v) ;
			N = S - L ;
			++ans ;
		}
	}
	return 0 ;
}

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

int main(){
	string s;
	while(cin>>s){
		if(s=="0000")break;
		int ans=0;
		if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3]){
			ans=-1;
			goto FIN;
		}
		while(s!="6174"){
			string sa,sb;
			char ca[4]={s[0],s[1],s[2],s[3]};
			sort(ca,ca+4);
			for(int i=0;i<4;i++){
			sa.push_back(s[i]);	
			sb.push_back(s[3-i]);
			}
			
			int af=0,bf=0;
			for(int i=0;i<4;i++){
				if(sa[i]==0&&af==0)sa.erase(sa.begin());
				else af++;
				
				if(sb[i]=0&&bf==0)sb.erase(sb.begin());
				else bf++;
				}
			int a=stoi(sa);
			int b=stoi(sb);
			int c=abs(a-b);
			
			string sc=to_string(c);
			for(int i=0;i<4-sc.size();i++)sc.insert(0,"0");
			s=sc;
			ans++;
			}
			FIN:
			if(ans==-1)cout<<"NA"<<endl;
			else cout<<ans<<endl;
		}
		return 0;
}