Go: package main

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

var memo map[int]int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if _, ok := memo[v]; !ok {
		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))
		memo[v] = solve(l-s) + 1
	}
	return memo[v]
}

func main() {
	var v int
	memo = make(map[int]int)
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

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

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

C++: #include<stdio.h>
int main(void)
{
	int a,flg,b,r,A,i,j,X,W,w,x[100];
	while(1){
		scanf("%d",&a);
		flg=0;
		r=0;
		if(a==0000) break;
		while(1){
			if(a%1111==0){
				flg=1;
				break;
			}
			else if(a==6174) break;
			r++;
			A=a;
			b=1000;
			for(i=0;i<4;i++){
				x[i]=a/b;
				a=a-x[i]*b;
				b/=10;
			}
			for(i=0;i<4;i++){
				for(j=i+1;j<4;j++){
					if(x[i]<x[j]){
						X=x[i];
						x[i]=x[j];
						x[j]=X;
					}
				}
			}
			w=x[0]*1000+x[1]*100+x[2]*10+x[3];
			b=1000;
			for(i=0;i<4;i++){
				x[i]=A/b;
				A=A-x[i]*b;
				b/=10;
			}
			for(i=0;i<4;i++){
				for(j=i+1;j<4;j++){
					if(x[i]>x[j]){
						X=x[i];
						x[i]=x[j];
						x[j]=X;
					}
				}
			}
			W=x[0]*1000+x[1]*100+x[2]*10+x[3];
			a=w-W;
		}
		if(flg==1){
				printf("NA\n");
		}
		else if(a==6174){
			printf("%d\n",r);
		}
	}
	return 0;
}

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

C++: #include <iostream>
#include <string>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int solve(string s){
    if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3])return -1;
    
    for(int cou=0;;cou++){
        if(s=="6174")return cou;
        
        string L,S;
        L=S=s;
        sort(L.begin(),L.end(),greater<char>());
        sort(S.begin(),S.end());
        int x=atoi(L.c_str())-atoi(S.c_str());
        for(int i=3;i>=0;i--,x/=10){
            s[i]='0'+x%10;
        }
      
    }
}
int main(void){
  while(1){
      string n;
      cin>>n;
      if(n=="0000")break;
      
      int ans=solve(n);
      if(ans==-1)cout<<"NA"<<endl;
      else cout<<ans<<endl;
  }
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"

string to_string_keta(const long long int l,const int keta) {
	ostringstream oss;

	oss << std::setw(keta)<<std::setfill('0')  << l;

	return oss.str();
}

int main() {
	while (1) {
		int N; cin >> N;
		if (!N)break;
		if (N % 1111 == 0) {
			cout << "NA" << endl;
			continue;
		}
		int num = 0;
		while (1) {
			if (N == 6174)break;
			string st(to_string_keta(N, 4));
			sort(st.begin(), st.end());
			int amin(stoi(st));
			reverse(st.begin(), st.end());
			int amax(stoi(st));
			N = amax - amin;
			num++;

		}
		cout << num << endl;
	}
	
	return 0;
}

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

			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(keta[i]>keta[j]){
						temp=keta[i];
						keta[i]=keta[j];
						keta[j]=temp;
					}
				}
			}
			s=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];
			n=l-s;
			cnt++;
		}
	printf("%d\n",cnt);
	}
	return 0;
}