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.Print("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<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>>setw(4)>>setfill('0')>>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>>to_;
            Q.push(data(to_,n+1));
        }
        if(memo[6174]==-1)cout<<"NA"<<endl;
        else cout<<memo[6174]<<endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstdlib>
#define rep(i,a) for( int i = 0; i != (a); ++i )

std::string intToStr( int n )
{
	char str[5];
	sprintf( str, "%04d", n );

	return str;
}

int main()
{
	std::string L, S;
	while( std::cin >> L, L != "0000" )
	{
		bool fl = true;

		rep( i, 3 )
			fl &= L[i+1] == L[i];
		if( fl )
		{
			std::cout << "NA" << std::endl;
			continue;
		}

		int ans = 0;
		while( L != "6174" )
		{
			S = L;
			std::sort( L.rbegin(), L.rend() );
			std::sort( S.begin(), S.end() );

			++ans;

			L = intToStr( atoi( L.c_str() ) - atoi( S.c_str() ) );
		}

		std::cout << ans << std::endl;
	}

	return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
using namespace std;

int main(){
	int n,d[4],turn;
	while(1){
		A:
		cin >> n;
		if(n==0){
			return 0;
		}
		d[3] = n/1000;
		d[2] = n/100 - d[3]*10;
		d[1] = n/10 - d[3]*100 - d[2]*10;
		d[0] = n - d[3]*1000 -d[2]*100 -d[1]*10;
		if((d[0]==d[1])&&(d[1]==d[2])&&(d[2]==d[3])){
			cout << "NA\n";
			goto A;
		}
		turn=0;
		while(n!=6174){
			d[3] = n/1000;
			d[2] = n/100 - d[3]*10;
			d[1] = n/10 - d[3]*100 - d[2]*10;
			d[0] = n - d[3]*1000 -d[2]*100 -d[1]*10;
		
			int y;
			for(int i=1;i<4;i++){
				for(int j=1;j<5-i;j++){
					if(d[j-1]>d[j]){
						y = d[j-1];
						d[j-1] = d[j];
						d[j] = y;
					}
				}
			}
		
			n = (d[3]-d[0])*999 + (d[2]-d[1])*90;

			turn++;
		}
		cout << turn <<  "\n";
	}
return 0;
}

C++: #include<stdio.h>
int main (){
	int N[4],L,S,c,i,a;
	while(1){
		for(i=0;i!=4;i++){
			scanf("%1d",&N[i]);
		}
		if(N[0]==N[1]&&N[2]==N[3]&&N[1]==N[2]){
			if(N[1]==0)
				break;
			else
				puts("NA");
		}
		else{
			for(i=0;c!=6174;i++){
				for(S=0;S!=4;S++){
					for(a=S;a!=4;a++){
						if(N[S]<N[a]){
							c=N[a];
							N[a]=N[S];
							N[S]=c;
						}
					}
				}
				L=1000*N[0]+100*N[1]+10*N[2]+N[3];
				S=1000*N[3]+100*N[2]+10*N[1]+N[0];
				L-=S;
				c=L;
				N[0]=L/1000;
				L%=1000;
				N[1]=L/100;
				L%=100;
				N[2]=L/10;
				L%=10;
				N[3]=L;
			}
			printf("%d\n",i);
		}
	}
}

#include<stdio.h>
int main (){
	int N[4],L,S,c,i,a;
	while(1){
		for(i=0;i!=4;i++){
			scanf("%1d",&N[i]);
		}
		if(N[0]==N[1]&&N[2]==N[3]&&N[1]==N[2]){
			if(N[1]==0)
				break;
			else
				puts("NA");
		}
		else{
			for(i=0;c!=6174;i++){
				for(S=0;S!=4;S++){
					for(a=S;a!=4;a++){
						if(N[S]<N[a]){
							c=N[a];
							N[a]=N[S];
							N[S]=c;
						}
					}
				}
				L=1000*N[0]+100*N[1]+10*N[2]+N[3];
				S=1000*N[3]+100*N[2]+10*N[1]+N[0];
				L-=S;
				c=L;
				N[0]=L/1000;
				L%=1000;
				N[1]=L/100;
				L%=100;
				N[2]=L/10;
				L%=10;
				N[3]=L;
			}
			printf("%d\n",i);
		}
	}
}

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

using namespace std;

int main(){
	int n, l, s, a;
	char c[4];
	while (1){
		int ans = 0;
		cin >> n;
		if (n == 0000){
			break;
		}
		a = n;
		while (a != 6174){
			for (int i = 0; i < 4; i++){
				c[i] = a % 10;
				a /= 10;
			}
			if (c[0] == c[1] && c[0] == c[2] && c[0] == c[3]){
				cout << "NA" << endl;
				break;
			}
			sort(c, c + 4);
			l = c[0] * 1000 + c[1] * 100 + c[2] * 10 + c[3];
			reverse(c, c + 4);
			s = c[0] * 1000 + c[1] * 100 + c[2] * 10 + c[3];
			a = s - l;
			ans++;
		}
		if (a == 6174){
			cout << ans << endl;
		}
	}
}

C++: #include <cstdio>
#include <cstring>
#include <string>

using namespace std;

int main(){
	while(1){
		int n,l,s;
		int cnt = 0;
		string tmp;

		scanf("%d",&n);
		if(n == 0)break;
		if(n % 1111 == 0){
			printf("NA\n");
			continue;
		}

		while(n != 6174){
			tmp = to_string(n);

			sort(tmp.begin(),tmp.end());
			s = stoi(tmp);

			reverse(tmp.begin(),tmp.end());
			l = stoi(tmp);
			n = l - s;
			cnt++;
		}
		printf("%d\n", cnt);
	}
	return 0;
}

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

int main() {
	string s;
	while (cin >> s) {
		if (s == "0000")break;
		int n[4] = { s[0] - '0',s[1] - '0',s[2] - '0',s[3] - '0' };
		int ans = 0;
		if (n[0] == n[1] && n[2] == n[3] && n[1] == n[2]) {
			ans = -1;
		}
		else {
			while (1) {
				if (n[0] == 6 && n[1] == 1 && n[2] == 7 && n[3] == 4)break;
				sort(n, n + 4);
				int na[4];
				na[0] = n[3] - n[0];
				na[1] = n[2] - n[1];
				na[2] = n[1] - n[2];
				na[3] = n[0] - n[3];
				for (int i = 0; i < 4; i++)n[i] = na[i];
				if (n[3] < 0) {
					n[3] += 10;
					n[2] --;
				}
				if (n[2] < 0) {
					n[2] += 10;
					n[1]--;
				}
				if (n[1] < 0){
					n[1] += 10;
					n[0]--;
				}
				ans++;
			}
		}
		if (ans == -1)cout << "NA" << endl;
		else cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

int main()
{
	int n;
	int l, s;
	int la[4], su[4];
	
	while (scanf("%d", &n), n != 0000){
		for (int i = 0; i < 4; i++){
			la[i] = n % 10;
			su[i] = n % 10;
			n /= 10;
		}
		
		if (la[0] == la[1] && la[0] == la[2] && la[0] == la[3]){
			printf("NA\n");
		}
		
		else {
			sort(la, la + 4);
			reverse(la, la + 4);
			
			sort(su, su + 4);
			
			l = (la[0] - '0') * 1000 + (la[1] - '0')* 100 + (la[2] - '0') * 10 + la[3] - '0';
			s = (su[0] - '0') * 1000 + (su[1] - '0')* 100 + (su[2] - '0') * 10 + su[3] - '0';
			int c = 1;
			while (l - s != 6174){
				n = l - s;
				for (int i = 0; i < 4; i++){
					la[i] = n % 10;
					su[i] = n % 10;
					n /= 10;
				}
				sort(la, la + 4);
				reverse(la, la + 4);
				
				sort(su, su + 4);
				
				l = (la[0] - '0') * 1000 + (la[1] - '0')* 100 + (la[2] - '0') * 10 + la[3] - '0';
				s = (su[0] - '0') * 1000 + (su[1] - '0')* 100 + (su[2] - '0') * 10 + su[3] - '0';
				
				c++;
			}
			printf("%d\n", c);
		}
	}
	
	return (0);
}

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

int parse(string a) {
	int l, r;
	l = r = 0;
	for (int b = 0; b < 4; b++) {
		l *= 10;
		l += a[b] - 48;
	}
	int b = 3;
	for (; b>-1; b--) {
		if (a[b] != '0')break;
	}
	for (int c = b; c!=-1; c--) {
		r *= 10;
		r += a[c] - 48;
	}
	return r - l;
}
int main() {
	while (1) {
		string a;
		cin >> a;
		if (a == "0000")break;
		if (a[0] == a[1] && a[1] == a[2] && a[2] == a[3]) { cout << "NA" << endl; continue; }
		for (int b = 0;; b++) {
			if (a == "6174") { cout << b << endl; break; }
			sort(a.begin(), a.end());
			a = to_string(parse(a));
		}
	}
}