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<algorithm>
#include<functional>
#include<cstring>
#include<cstdlib>

int main() {
	int n;
	int str[4];
	while (std::cin >> n) {
		int str[4] = { (n / 1000),(n / 100) % 10,(n / 10) % 10,n % 10 };
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == 0)
				break;
			else
				std::cout << "NA" << std::endl;
		}
		int cnt = 0;
		while(n!=6174) {
			if (str[0] == str[1] == str[2] == str[3]) {
				std::cout << "NA" << std::endl;
				goto a;
			}
			++cnt;
			std::sort(str, str + 4, std::greater<int>());
			int l = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
			std::sort(str, str + 4);
			int	s = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
			n = l - s;
			str[0] = n / 1000;
			str[1] = (n / 100) % 10;
			str[2] = (n / 10) % 10;
			str[3] = n % 10;
		}
		std::cout << cnt << std::endl;
	a:;
	}
	return 0;
}

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

int main() {
	std::string str;
	while (std::cin >> str) {
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == '0')
				break;
			else
				std::cout << "NA" << std::endl;
		}
		std::string l, s;
		int cnt = 0;
		while(str!="6174") {
			++cnt;
			l = str;
			std::sort(l.begin(), l.end(), std::greater<char>());
			s = str;
			std::sort(s.begin(), s.end());
			str = std::to_string(std::stoi(l) - std::stoi(s));
		}
		std::cout << cnt << std::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" )
	{
		int ans = 0;
		while( L != "6174" )
		{
			bool fl = true;

			rep( i, 3 )
				fl &= L[i+1] == L[i];
			if( fl )
			{
				std::cout << "NA" << std::endl;
				goto loop_end;
			}

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

	return 0;
}

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

int main()
{
	int n;

	cin >> n;

	while (n > 0) {
		int a[4];
		a[0] = n / 1000;
		a[1] = (n % 1000) / 100;
		a[2] = (n % 100) / 10;
		a[3] = n % 10;

		if (a[0] == a[1] && a[0] == a[2] && a[0] == a[3])
			cout << "NA" << endl;
		else {
			int t = 0;

			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);
				n = (a[3] - a[0]) * 999 + (a[2] - a[1]) * 90;
				t++;
			}

			cout << t << endl;
		}

		cin >> n;
	}

	return 0;
}

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

	using namespace std;

	int main()
	{
		int m, n[4], mi[4], ma[4], na;
		int	max_int, min_int, n_int, ans;
		bool same = false;
			while (scanf("%1d", &n[i])){
				na = n[0];
				for (int i = 1; i < 4; i++){
					if (na != n[i]) same = true;
				}
				if (!same) cout << "NA" << endl;

			while(n[0] != 6 || n[1] != 1 || n[2] != 7 || n[3] != 4){
				if (!same) break;
				sort(n, n + 4);
				
				for (int i = 0; i < 4; i++){
					mi[i] = n[i];
					ma[3 - i] = n[i];
				}

				for (int i = 0; i < 4; i++){
					max_int = ma[i] * 10 ^ i;
					min_int = mi[i] * 10 ^ i;
				}
				n_int = max_int - min_int;
				for (int i = 0; i < 4; i++){
					n[i] = n_int / (10 ^ (3 - i)) % 10;
				}
				ans++;
			}

			if (!same) cout << ans << endl;
		}
		return (0);
	}

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


bool allsame(string s){
  char c=s[0];
  for(int i=0;i<s.size();i++){
    if(s[i]!=c)return 0;
  }
  return 1;
}

string toSortString(int n){
  string res="0000";
  int tmp[4]={0,0,0,0};
  for(int i=0;n!=0;i++){
    tmp[i]=n%10;
    n/=10;
  }
  for(int i=0;i<4;i++){
    res[i]=tmp[i]+'0';
  }
  sort(res.begin(),res.end());
  return res;
}

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

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

int main(){
  int n;
  int ans=0;
  while(cin>>n,n!=0){
    ans=0;
    while(n!=6174){
      string tmp_n=toSortString(n);
      if(allsame(tmp_n)){
        cout<<"NA"<<endl;
        break;
      }
      ans++;
      int l,s;
      cout<<tmp_n<<endl;
      l=make_l(tmp_n);
      s=make_s(tmp_n);
      cout<<l<<endl;
      cout<<s<<endl<<endl;
      n=l-s;
      //cout<<n<<endl;
      assert(ans<100);
    }
    if(n==6174){
      cout<<ans<<endl;
    }
  }
}

C++: import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        for (;;) {
            int n=sc.nextInt();
            if(n==0) {
                break;
            }
            Integer[] l=d(n);
            if(l[0]==l[1]&&l[1]==l[2]&&l[2]==l[3]) {
                System.out.println("NA");
                continue;
            }
            int c=0;
            while(n!=6174) {
                c++;
                l=d(n);
                Arrays.sort(l,Comparator.reverseOrder());
                int a=c(l);
                Arrays.sort(l);
                int b=c(l);
                n=a-b;
            }
            System.out.println(c);
        }
    }
    static int c(Integer[] l) {
        int n=0;
        for(int i=0,j=1000;i<4;i++,j/=10) {
            n+=l[i]*j;
        }
        return n;
    }
    static Integer[] d(int n) {
        Integer[] l=new Integer[4];
        for(int i=0,j=1000;i<4;i++,j/=10) {
            l[i]=n/j;
            n%=j;
        }
        return l;
    }
}

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

#define reps(i,f,n) for(int i = f; i < int(n); i++)
#define rep(i,n) reps(i,0,n)
using namespace std;

int to[10000];
int memo[10000];

void init(){
	string str, rstr;
	int L,S;
	stringstream ss;
	rep(i,10000)memo[i] = -1;
	rep(i,10000){
		ss.clear();
		ss<<i;
		ss>>rstr;
		sort(rstr.begin(),rstr.end());
		str = rstr;
		reverse(str.begin(), str.end());
		ss.clear();
		ss << (str + " " + rstr);
		ss >> L >> S;
		to[i] = L-S;
	}
}

int main(void){
	int n;
	init();
	
	while(scanf("%d", &n),n){
		int ans = 0;
		int n_ = n;
		if(to[n] == 0){
			puts("NA");
			continue;
		}
		if(memo[n] != -1){
			ans = memo[n];
		}else{
			while(n != 6174){
				n = to[n];
				ans++;
			}
			memo[n_] = ans;
		}
		printf("%d\n", ans);
	}
	
	return 0;
}