Go: package main

import (
	"fmt"
	"sort"
)

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]
	return solve(l-s) + 1
}

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

C++: #include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<algorithm>
typedef unsigned long long ull;
#define rep(i,a) for(int i=0;i<a;i++)
#define loop(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1);
const double inf = (int)1e8;

int main(void){
  string s;
  while(cin>>s){
    bool brek = true;
    rep(i,4)if(s[i] != s[0])brek = false;
    if(brek&&s[0]=='0')break;
    if(brek&&s[0]!='0'){cout<<"NA"<<endl;continue;}

    int cnt = 0;
    while(s!="6174"){
      cnt ++;
      vector<char> used;
      rep(i,4){
	used.push_back(s[i]);
      }
    
      sort(used.begin(),used.end());
      string diff;
      rep(i,used.size()){
	diff += used[used.size()-i-1] - used[i] + '0';
	if(diff[i]<'0'){diff[i-1]--; diff[i]+= 10;}
      }
      //cout<<diff<<endl;
      s=diff;
    }
    cout<<cnt<<endl;
  }
  return 0;
}

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

std::string intToStr( int n )
{
	std::ostringstream oss;
	oss << n;

	return oss.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 )
		{
			puts("NA");
			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() ) );
		}

		printf( "%d\n", ans );
	}

	return 0;
}

C++: #include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std;
char A[9],B[9];
#define I(s)strtol(s.c_str(),NULL,10)
string S(int i,int L){sprintf(A,"%%0%dd",L),sprintf(B,A,i);return B;}
int main(){
	int a,i;
	for(;scanf("%d",&a),a;printf(a?"%d\n":"NA\n",i)){
		for(i=0;a&&a!=6174;i++){
			string t=S(a,4),s;
			sort(t.begin(),t.end());
			s=t;
			reverse(s.begin(),s.end());
			a=I(s)-I(t);			
		}
	}
}

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

int main(){
  string str;
  while(cin >> str && str != "0000"){
    if(str == "1111" || 
       str == "2222" || 
       str == "3333" || 
       str == "4444" || 
       str == "5555" || 
       str == "6666" || 
       str == "7777" || 
       str == "8888" || 
       str == "9999") cout << "NA" << endl;
    else if(str == "6174") cout << 0 << endl;
    else{
      for(int n=1;;n++){
        int L=0,S=0,LS,a,b;
        bool f[4];
        fill(f,f+4,false);

        for(int i=1;i<=1000;i*=10){
          a = b = -1;
          for(int j=0;j<4;j++){
            if(f[j]) continue;
            else if(a < str[j] - '0'){
              a = str[j] - '0';
              b = j;
            }
          }
          f[b] = true;
          L += 1000 / i * a;
          S += i * a;
        }
        LS = L - S;

        if(LS == 6174){
          cout << n << endl;
          break;
        }

        for(int i=3;i>=0;i--){
          str[i] = LS % 10 + '0';
          LS /= 10;
        }
      }
    }
  }
}

C++: #include<iostream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<cstdlib>

int main() {
	char str[4];
	while (std::cin >> str) {
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == '0')
				break;
			else
				std::cout << "NA" << std::endl;
		}
		char l[4], s[4];
		int cnt = 0;
		while(str!="6174") {
			++cnt;
			std::memcpy(l, str, 4);
			std::memcpy(s, str, 4);
			std::sort(l, l + 4, std::greater<char>());
			std::sort(s, s + 4);
			int a = atoi(l) - atoi(s);
			itoa(a, str, 10);
		}
		std::cout << cnt << std::endl;
	}
	return 0;
}

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

using namespace std;

int main(){
	int a[4];
	int b,flg,c,d,e,cunt,m,f;
	
	scanf("%d",&d);
	while(d!=0000){
		
		m=1000;
		cunt=0;
		flg=0;
		
		f=d;
		
		for(int i=0;i<3;i++){
			a[3-i]=d/m;
			d=d%m;
			m/=10;
		}
		a[0]=d;
		
		for(int i=0;i<3;i++){
			if(a[i]!=a[i+1]) flg=1;
		}
		
		
		
		if(flg==1){
			while(f!=6174){
				c=0;
				e=0;
				sort(a,a+4);
				m=1;
				for(int i=0;i<4;i++){
					c+=a[i]*m;
					m*=10;
				}
				sort(a,a+4,greater<int>());
				m=1;
				for(int i=0;i<4;i++){
					e+=a[i]*m;
					m*=10;
				}
				
				d=c-e;
				
				
				f=d;
				
				m=1000;
				
				for(int i=0;i<3;i++){
					a[3-i]=d/m;
					d=d%m;
					m/=10;
				}
				a[0]=d;
				cunt++;
			}
		}
		
		if(flg==0){
			printf("NA\n");
		}else{
			printf("%d\n",cunt);
		}
		scanf("%d",&d);
	}

}

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

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

int s(int n, bool f) {
  vector<int> a;
  while (n) {
    a.push_back(n % 10);
    n /= 10;
  }

  sort(a.begin(), a.end());
  if (f) reverse(a.begin(), a.end());

  int res = 0;
  for (int i = 0; i < a.size(); ++i) {
    res *= 10;
    res += a[i];
  }

  return res;
}

int main() {
  int n;

  while (cin >> n, n) {
    if (n % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }

    // cout << s(n, true) - s(n, false) << endl;

    int cnt = 0;
    while (n != 6174) {
      cnt++;
      n = s(n, true) - s(n, false);
    }

    cout << cnt << endl;
  }
}

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

int stoi(string s){
  stringstream ss(s);
  int res; ss >> res;
  return res;
}

int main(){
  string s;
  while(cin >> s,s!="0000"){
    bool f = true;
    for(int i=0;i<3;i++)f &= (s[i]==s[i+1]);
    if(f){ cout << "NA\n"; continue; }

    int cnt = 0;
    while(s!="6174"){
      sort(s.begin(),s.end());
      int S = stoi(s);
      reverse(s.begin(),s.end());
      int L = stoi(s);
      char N[5]; sprintf(N,"%04d",L-S);
      for(int i=0;i<4;i++)s[i] = N[i];
      cnt++;
    }
    cout << cnt << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int m,n,l;
  cin>>n;
  while(n!=0){
    int nn[4]={n%10,(n/10)%10,(n/100)%10,(n/1000)%10};
    if(nn[0]==nn[1]&&nn[1]==nn[2]&&nn[2]==nn[3]){
      cout<<"NA"<<endl;
    }else{
      int cou=0;
      while(nn[0]!=4&&nn[1]!=7&&nn[2]!=1&&nn[3]!=6){
	int rn[4];
	for(int i=0;i<3;i++){
	  for(int j=i+1;j<4;j++){
	    if(nn[i]>nn[j]){
	      int exi=nn[i];
	      nn[i]=nn[j];
	      nn[j]=exi;
	    }
	  }
	}
	for(int i=0;i<4;i++){
	  rn[i]=nn[i];
	}
	for(int i=0;i<4;i++){
	  if(rn[i]-nn[3-i]<0){
	    rn[i]+=10;
	    rn[i]-=nn[3-i];
	    rn[i+1]--;
	  }else{
	    rn[i]-=nn[3-i];
	  }
	}
	for(int i=0;i<4;i++){
	  nn[i]=rn[i];
	}
	cou++;
      }
      cout<<cou<<endl;
    }
    cin>>n;
  }
  return (0);
}