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 <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;
	ll ans = 0;
	cin >> s;
	if(s == "0000") break;
	else if(s[0] == s[1] && s[0] == s[2] && s[0] == s[3]){
	    cout << "NA" << endl;
	    continue;
	}
	while(s.compare("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(RALL(a));
	    sort(ALL(b));
	    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>
#include <vector>
#include <functional>
#include <queue>
#include <set>
#include <map>
#include <numeric>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <sstream>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for(int i = a; i < b; ++i)
#define repll(i, a) REPLL(i, 0, a)
#define REPLL(i, a, b) for(ll i = a; i < b; ++i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;
const double PI = 3.14159265358979323846;
const double esp = 1e-9;
const int infi = (int)1e+9 + 10;
const ll infll = (ll)1e+17 + 10;

std::string n, l, s;
int cnt = 0;
bool used[10000];
bool flg = true;

int main(){
	while (std::cin >> n, n != "0000"){
		rep(i, 10000)used[i] = false;
		cnt = 0;
		flg = true;
		while (n != "6174"){
			l = n;
			s = n;
			std::sort(l.begin(), l.end(), std::greater<char>());
			std::sort(s.begin(), s.end());
			n = std::to_string(std::stoi(l) - std::stoi(s));
			if (used[std::stoi(n)]){
				flg = false;
				break;
			}
			used[std::stoi(n)] = true;
			++cnt;
		}
		if (flg)std::cout << cnt << std::endl;
		else std::cout << "NA" << 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" )
	{
		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<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int N;
int ans = 0;
int main()
{
	while (true){
		cin >> N;
		if (N == 0)break;
		if (N % 1111 == 0)printf("NA\n");
		else{
			ans = 0;
			while (N != 6174){
				int A[4];
				A[0] = N / 1000;
				A[1] = (N / 100) % 10;
				A[2] = (N / 10) % 10;
				A[3] = N % 10;
				sort(A, A + 4);
				reverse(A, A + 4);
				N = 999 * (A[0] - A[3]) + 90 * (A[1] - A[2]);
				ans++;
				
			}
			
			printf("%d\n", ans);
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int f(int p) {
	int a[10];
	a[0] = (p - p % 1000) / 1000;
	p -= a[0] * 1000;
	a[1] = (p - p % 100) / 100;
	p -= a[1] * 100;
	a[2] = (p - p % 10) / 10;
	p -= a[2] * 10;
	a[3] = p;
	sort(a, a + 4);
	return a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0] - a[0] * 1000 - a[1] * 100 - a[2] * 10 - a[3];
}
int main() {
	int n; while (cin >> n) {
		if (n == 0)return 0;
		else if (n % 1111 == 0) { cout << "NA\n"; continue; }
		else {
			for (int i = 0; i <= 6174; i++) {
				if (n == 6174) {
					cout << i << endl;
					i = 10000;
				}
				else n = f(n);
			}
		}
	}

}

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

int main() {
	int n;
	int a[4] = {};

	while (std::cin >> n) {
		if (n == 0000)
			break;

		if (n % 1111 == 0) {
			std::cout << "NA" << std::endl;
			continue;
		}

		int count = 0;
		while (n != 6174) {
			a[0] = n / 1000, n %= 1000;
			a[1] = n / 100, n %= 100;
			a[2] = n / 10, n %= 10;
			a[3] = n;

			std::sort(a, a + 4);

			n = 0;
			n += (a[3] - a[0]) * 1000;
			n += (a[2] - a[1]) * 100;
			n += (a[1] - a[2]) * 10;
			n += a[0] - a[3];

			++count;
		}

		std::cout << count << std::endl;
	}
	return 0;
}

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

int main(void) {
	int S, L;
	string N;
	int ans;
	bool flag = false;

	while(1) {
		cin >> N;
		if(N[0] == N[1] && N[1] == N[2] && N[2] == N[3]) {
			flag = true;
			if(N[0] == '0')
				break;
		}

		if(!flag) {
			for(ans = 0; atoi(N.c_str()) != 6174; ans++) {
				sort(N.begin(), N.end());
				S = atoi(N.c_str());
				sort(N.begin(), N.end(), greater<int>());
				L = atoi(N.c_str());
				int tmp = L - S;

				for(int i = 1000, j = 0; i; i /= 10, j++) {
					N[j] = tmp / i + '0';
					tmp %= i;
				}
			}
		}

		if(flag)
			cout << "NA" << endl;
		else
			cout << ans << endl;
	}

	return 0;
}

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

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

int main(){
	for(char s[5];scanf("%s",s),strcmp(s,"0000")!=0;){
		if(count(s,s+4,s[0])==4){
			puts("NA");
			continue;
		}

		int ans=0;
		while(strcmp(s,"6174")!=0){
			char s1[5]; strcpy(s1,s); sort(s1,s1+4);
			char s2[5]; strcpy(s2,s); sort(s2,s2+4,greater<char>());
			int a1=atoi(s1);
			int a2=atoi(s2);
			sprintf(s,"%04d",a2-a1);
			ans++;
		}
		printf("%d\n",ans);
	}

	return 0;
}

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


int to_i(string &s){
	int a=0;
	for(int i=0;i<s.size();i++)a=a*10+s[i]-'0';
	return a;
}
string to_s(int n){
	string s;
	while(n){
		s+=n%10+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}



int cache[10000];

int solve(int n){
	if(n==6174)return 0;
	if(cache[n])return n;
	string s=to_s(n);
	int a,b;
	sort(s.begin(),s.end());
	a=to_i(s);
	sort(s.begin(),s.end(),greater<char>());
	b=to_i(s);
	return cache[n] = 1+solve(b-a);
}


int main(){
	string s;
	while(cin>>s&&s!="0000"){
		if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3]){
			cout<<"NA\n";
			continue;
		}
		cout<<solve(to_i(s))<<endl;
		
	}
	return 0;
}