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

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

int main(){
	while(1){
		int n[4];
		int L=0;
		int S=0;
		int N=0;
		cin>>N;
		if(N==0){
			break;
		}
		if(N%1111==0){
			cout<<"NA"<<endl;
			continue;
		}
		int cun=0;
		int B=1;
		while(B){
			if(N==6174){
				B=0;
				break;
			}
			cun++;
			n[0]=N/1000;
			n[1]=N/100%10;
			n[2]=N/10%10;
			n[3]=N%10;
			
			sort(n,n+4);
			
			S=n[0]*1000+n[1]*100+n[2]*10+n[3];
			L=n[3]*1000+n[2]*100+n[1]*10+n[0];

			N=L-S;
			
			
		}
		cout<<cun<<endl;
	}
	return 0;
}

C++: //39
#include<iostream>
#include<cstdlib>
#include<sstream>
#include<string>
#include<algorithm>

using namespace std;

int main(){
  for(string s;cin>>s,s!="0000";){
    int c=0;
    while(s!="6174"){
      sort(s.begin(),s.end());
      if(s[0]==s[3]){
	cout<<"NA"<<endl;
	goto next;
      }
      stringstream ss;
      ss.fill('0');
      ss.width(4);
      ss<<atoi(string(s.rbegin(),s.rend()).c_str())-atoi(s.c_str());
      s=ss.str();
      c++;
    }
    cout<<c<<endl;
  next:
    ;
  }
  return 0;
}