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<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
using namespace std;
int main(){
  string s;
  while(cin>>s,s!="0000"){
    int co=0;
    if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3]){cout<<"NA"<<endl;continue;}
    while(s!="6174"){
      vector<int>tmp;
      rep(i,4)tmp.pb(s[i]-'0');
      sort(all(tmp));
      int a=0,b=0;
      rep(i,4){
	a+=pow(10,3-i)*tmp[i];
	b+=pow(10,i)*tmp[i];
      }
      a=b-a;
      co++;
      s="";
      while(a){
	s=(char)(a%10+'0')+s;
	a/=10;
      }
      while(s.size()!=4)s='0'+s;
    }
    cout<<co<<endl;
  }
}

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);
	    RREP(i,4){
		if(a[i] < b[i]) {
		    if(a[i-1] > 0){
			a[i-1]--;
			a[i] += 10;
		    }else if(a[i-2] > 0){
			a[i-2]--;
			a[i-1] = 9;
			a[i] += 10;
		    }else{
			a[i-2] = 9;
			a[i-1] = 9;
			a[i] += 10;
		    }
		}
		c[i] = a[i] - b[i];
	    }
	    s = to_string(1000 * c[0] + 100 * c[1] + 10 * c[2] + c[3]);
	    ans++;
	}
	cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int stoi(string s){
	int size=s.size(),res=0;
	for(int i=size-1;i>=0;i--){
		res+=(int)(s[i]-'0')*pow(10,size-1-i);
	}
	return res;
}

string itos(int n){
	int size=log(n)/log(10)+1;
	string s="";
	rep(i,size){
		s+=(char)(n/pow(10,size-1-i)+'0');
		n%=(int)pow(10,size-1-i);
	}
	if(size<4)rep(i,4-size)s='0'+s;
	return s;
}

int main(){
	string s,res[2];
	int ma,mi;
	while(cin>>s){
		if(s[0]=='0' && s[1]=='0' && s[2]=='0' && s[3]=='0')break;
		if(s[0]==s[1] && s[1]==s[2] && s[2]==s[3]){
			cout<<"NA"<<endl;
			continue;
		}
		int cnt=0;
		while(s!="6174"){
			cnt++;
			sort(s.begin(),s.end());
			mi=stoi(s);
			reverse(s.begin(),s.end());
			ma=stoi(s);
			s=itos(ma-mi);
		}
		cout<<cnt<<endl;
	}
	return 0;
}

C++: #include<algorithm>
#include<iostream>
#include<string>

int main()
{
	std::string N;
	while( std::cin >> N, N != "0000" )
	{
		int cnt = 0;

		while( N != "6174" )
		{
			std::string L = N, S = N;

			std::sort( L.rbegin(), L.rend() );
			std::sort( S.begin(), S.end() );

			for( int i = 0; i != N.size(); ++i )
			{
				N[i] = L[i] - S[i] + '0';
				if( i > 0 && N[i] < '0' )
					--N[i-1], N[i] += 10; 
			}

			++cnt;

			if( N == "0000" )
			{
				std::cout << "NA" << std::endl;
				break;
			}
		}

		if( N != "0000" )
			std::cout << cnt << std::endl;
	}

	return 0;
}

C++: #include<bits/stdc++.h>

#define F first
#define S second

using namespace std;

typedef pair<int,int>P;
typedef long long ll;

int main(){
  int n;
  int l,s;
  int buff[4];
  int temp;
  int cnt;
  
  while(1){
    scanf("%d",&n);
    if(n==0) break;

    if(n%1111==0){
      printf("NA\n");
    }else{
      
      for(int i=3;i>=0;i--){
	buff[i]=n%10;
	n/=10;
      }
      
      cnt=0;
      while(!(buff[0]==6&&buff[1]==1&&buff[2]==7&&buff[3]==4)){
	s=l=0;
	sort(buff,buff+4);
	for(int i=0;i<4;i++){
	  s*=10;
	  s+=buff[i];
	}
	
	for(int i=3;i>=0;i--){
	  l*=10;
	  l+=buff[i];
	}
	
	temp=l-s;
	for(int i=3;i>=0;i--){
	  buff[i]=temp%10;
	  temp/=10;
	}
	cnt++;
      }
      
      printf("%d\n",cnt);
    }
  }

  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;


#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 1000


void calc(int n){

  if(n%1111 == 0){
    puts("NA");
    return;
  }

  char l[5], s[5];

  for(int i=0;;i++){
    if(n == 6174){
      printf("%d\n",i);
      break;
    }
    
    sprintf(l,"%04d",n);
    sprintf(s,"%04d",n);
    
    sort(l,l+4);
    sort(s,s+4);
    reverse(l,l+4);
    
    n = atoi(l) - atoi(s);
  }

}
  

int main(){

  int n;

  while(scanf("%d",&n)!=EOF){
    if(n==0) break;
    calc(n);
  }
  
  return 0;
}

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

int solve(string &s, int cnt = 0){
  while(s != "6174"){
    if(s == "0000")return -1;
    cnt++;
    sort(s.begin(), s.end());
    string tmp = s;
    reverse(tmp.begin(), tmp.end());
    int a = stoi(s), b = stoi(tmp);
    s = to_string(b - a);
    while(s.length() != 4)s.push_back('0');
  }
  return cnt;
}

int main(int argc, char *argv[]){
  string s;
  while(cin >> s){
    if(s == "0000")break;
    int ans = solve(s);
    if(ans != -1)
      std::cout << ans << std::endl;
    else
      std::cout << "NA" << std::endl;
  }
  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(0,1);
                else af++;
               
                if(sb[i]==0&&bf==0)sb.erase(0.1);
                else bf++;
                }
            int a=stoi(sa);
            int b=stoi(sb);
            int c=abs(a-b);
           
            string sc=to_string(c);
            int z=sc.size();
            for(int i=0;i<4-z;i++)sc.insert(0,"0");
            s=sc;
            ans++;
            }
            FIN:
            if(ans==-1)cout<<"NA"<<endl;
            else cout<<ans<<endl;
        }
        return 0;
}