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<iomanip>
#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 << std::setprecision( 4 ) << std::setfill( '0' ) << 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 )
		{
			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<stdio.h>

int main(){

char ty;
char c1,c2,c3,c4,ch5;
int n1=0,n2=0,n3=0,n4=0,N=0,S=0,P=0,K=0;
int n[5]={};
int F=0;
while(1){
//n1=c1-'0';n2=c2-'0';n3=c3-'0';n4=c4-'0';
	
	bool f=false;
	bool FF=false;
	do{
	scanf("%c%c%c%c%c",&c1,&c2,&c3,&c4,&ch5);
	ty=c1;
	
     if(ty==c1&&ty==c3&&ty==c4&&ty==c2)f=false;
	else f=true;

	if(ty=='0')f=true;
	if(f==false)printf("NA\n");

	}while(f!=true);
	if(c1=='0'&&c2=='0'&&c3=='0'&&c4=='0')break;
	F++;
	if(F==10001)break;
	
	if(c1=='6'&&c2=='1'&&c3=='7'&&c4=='4')FF=true;



	for(int i=0;i<4;i++)
	n[i]=0;
int cnt=0;


n[0]=c1-'0';n[1]=c2-'0';n[2]=c3-'0';n[3]=c4-'0';


for(int i=0;i<3;i++)
for(int j=3;j>i;j--)
if(n[j]>n[j-1]){int t=n[j];n[j]=n[j-1];n[j-1]=t;}

N=1000*n[0]+100*n[1]+10*n[2]+n[3];
S=1000*n[3]+100*n[2]+10*n[1]+n[0];

P=N-S;
if(FF!=true)cnt++;

while(P!=6174){
N=P;
K=P;n[0]=N/1000;
K=N%1000;n[1]=K/100;
K=N%100;n[2]=K/10;
K=N%10;n[3]=K;

//printf(" %d\n",N);

for(int i=0;i<3;i++)
for(int j=3;j>i;j--)
if(n[j]>n[j-1]){int t=n[j];n[j]=n[j-1];n[j-1]=t;}


N=1000*n[0]+100*n[1]+10*n[2]+n[3];
S=1000*n[3]+100*n[2]+10*n[1]+n[0];

cnt++;
P=N-S;

}
printf("%d\n",cnt);
}
return 0;
}

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

typedef long int lint;
typedef long long int llint;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
typedef pair<long, long> pll;

#define EACH(i, c) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, e) for (int i = (s); i < (int)(e); ++i)
#define REP(i, n) FOR(i, 0, n)
#define endl '\n'
#define mod 1000000007
#define INF 100000000
#define fk first
#define sv second

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, 1, 0, -1, 1, -1, 1, -1};

template<typename T>
void print(const std::vector<T>& v, char sep=' ') {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i != 0) cout << sep;
    cout << v[i];
  }
  cout << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;

  while (cin >> n, n){
    int ct = 0;
    if (n % 1111 == 0){
      cout << "NA\n";
      continue;
    }
    
    while (n != 6174){
      int l, s, num[4];
      REP(i, 4){
        num[i] = n % 10;
        n /= 10;
      }
      sort(num, num + 4);
      l = s = 0;
      REP(i, 4){
        l = l * 10 + num[3 - i];
        s = s * 10 + num[i];
      }
      n = l - s;
      ct++;
    }
    cout << ct << endl;
  }
}

C++: #include <iostream>
#include <stdio.h>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <set>
#include <math.h>
#include <utility>
#include <stack>
#include <string.h>
#include <complex>
using namespace std;
const int INF = 1<<29;
const double EPS = 1e-8;
typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int,int> P;
struct edge{int to,cost;};

const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};



int main(){
    string N;
    while(cin >> N, N != "0000"){
        if(N=="0000"||N=="1111"||N=="2222"||N=="3333"||N=="4444"||N=="5555"||N=="6666"||N=="7777"||N=="8888"||N=="9999"){
            cout << "NA" << endl;
            continue;
        }else if(N=="6174"){
            cout << 0 << endl;
            continue;
        }
        vec Nv(4);
        for(int i=0;i<4;i++){
            Nv[i] = N[i]-'0';
        }
        int t = 0;
        while(1){
            t++;
            sort(Nv.begin(), Nv.end());
            int l=0, s=0, p=1;
            for(int i=0;i<4;i++){
                l += Nv[i]*p;
                s += Nv[i]*1000/p;
                p *= 10;
            }
            int sub = l - s;
            if(sub == 6174) break;
            p = 1000;
            for(int i=0;i<4;i++){
                Nv[i] = (sub/p)%10;
                p /= 10;
            }
        }
        cout << t << endl;
    }
    return 0;
}

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

string toString(int n){
  ostringstream oss;
  oss << n;
  return oss.str();
}

int toInt(string s){
  istringstream iss(s);
  int ret;
  iss >> ret;
  return ret;
}


int main(){
  string s;
  while(cin >> s){
    if(s == "0000") break;
    if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]){
      cout << "NA" << endl;
      continue;
    }
    int cnt = 0;
    bool f = false;
    while(s != "6174"){
    if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]){
      cout <<"NA"<<endl;
      f = true;
      break;
    }
      cnt++;
      string L, S;
      L = s;
      sort(L.begin(), L.end());
      S = L;
      reverse(L.begin(), L.end());
      int a, b;
      a = toInt(L);
      b = toInt(S);
      s = toString(a-b);
      while(s.size() != 4){
	s += "0";
      }
    }
    if(!f)cout << cnt << endl;
  }
  return 0;
}

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

int N,a[4],S.L,count;

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

C++: #include<bits/stdc++.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;

int main(){
	string c;
	while(cin>>c,c!="0000"){
		if(c[0]==c[1]&&c[0]==c[2]&&c[0]==c[3]){ printf("NA\n"); continue;}
		int res=0;
		while(c!="6174"){cout<<c<<endl;
			string l,m;
			l=m=c;
			sort(l.begin(),l.end(),greater<char>()); sort(m.begin(),m.end());
			int a=(l[0]-48)*1000+(l[1]-48)*100+(l[0]-48)*10+(l[1]-48);
			int b=(m[0]-48)*1000+(m[1]-48)*100+(m[0]-48)*10+(m[1]-48);
			int x=a-b;
			for(int i=0,p=1000;i<4;i++){ c[i]=(char)(48+x/p); p%=10;}
			res++;
		}
		printf("%d\n",res);
	}
}

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