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

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