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<stdio.h>
int main(void)
{
	int N,L,S,N1,N2,N3,N4,N5,N6,i,temp,j,cnt,flg,flg1;
	int a[4];
	cnt=1;
	flg=0;
	flg1=0;
	scanf("%d",&N);
	if(N==6174){
		flg1=1;
	}
	while(!(N==0000)){
		if(flg1==1){
			cnt=0;
			flg=1;
			break;
		}
		else{
			N1=N/1000;/*6!*/
			N2=N%1000;/*174*/
			N3=N2/100;/*1!*/
			N4=N2%100;/*74*/
			N5=N4/10;/*7*/
			N6=N4%10;/*4!*/
			if(N1==N3 && N1==N5 && N1==N6){
				printf("NA\n");
				break;
			}
			else{
				a[0]=N1;
				a[1]=N3;
				a[2]=N5;
				a[3]=N6;
				for(i=0;i<4;i++){
					for(j=i+1;j<4;j++){
						if(a[i]<a[j]){
							temp=a[j];
							a[j]=a[i];
							a[i]=temp;	
						}
					}
				}
				N1=a[0]*1000;
				N3=a[1]*100;
				N5=a[2]*10;
				N6=a[3];
				L=N1+N3+N5+N6;
				for(i=0;i<4;i++){
					for(j=i+1;j<4;j++){
						if(a[i]>a[j]){
							temp=a[i];
							a[i]=a[j];
							a[j]=temp;	
						}
						
					}
				}
				N1=a[0]*1000;
				N3=a[1]*100;
				N5=a[2]*10;
				N6=a[3];
				S=N1+N3+N5+N6;
				N=L-S;
				if(N==6174){
				
					break;
				}
				else{
				flg=1;
					cnt++;
				}
			}
		}
	}
	if(flg==1){
	printf("%d\n",cnt);
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
string S; int a[4], b, cnt;
int main() {
	while (true) {
		cin >> S;
		if (S == "0000") { break; }
		if (S[0] == S[1] && S[2] == S[3] && S[1] == S[2]) { cout << "NA" << endl; }
		else {
			b = stoi(S);
			cnt = 0;
			while (b != 6174) {
				cnt++;
				for (int i = 0; i < 4; i++) {
					a[i] = (b / (int)pow(10, i)) % 10;
				}
				sort(a, a + 4);
				b = (a[3] - a[0]) * 1000 + (a[2] - a[1]) * 100 + (a[1] - a[2]) * 10 + (a[0] - a[3]);
			}
			cout << cnt << 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 <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( v, c ) for ( auto &v : c )

#define EACH( it, c ) for ( auto it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

bool isNA( string str )
{
	sort( ALL( str ) );
	return str[0] == str.back();
}

int solve( string str )
{
	int res = 0;
	while ( str != "6174" )
	{
		res++;

		string l( str ), s( str );
		sort( ALL( l ), greater<char>() );
		sort( ALL( s ) );

		ISS issL( l ), issS( s );
		int iL, iS;
		issL >> iL;
		issS >> iS;
		int n = iL - iS;

		OSS oss;
		oss << setw( 4 ) << setfill( '0' );
		oss << n;
		str = oss.str();
	}
	return res;
}


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

	while ( true )
	{
		string str;
		cin >> str;

		if ( str == "0000" )
		{
			break;
		}

		if ( isNA( str ) )
		{
			cout << "NA" << endl;
		}
		else
		{
			cout << solve( str ) << endl;
		}
	}

	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<math.h>

using namespace std;

/*
//1
int main() {
	int ans = 0;
	for (int i = 0; i < 10; i++) {
		int a;
		cin >> a;
		ans += a;
	}
	cout << ans << endl;

	return 0;
}
*/

/*
//2
int main() {
	int a, b, c;
	cin >> a >> b >> c;
	if ((a == 1 && b == 1) || c == 1) {
		cout << "Open" << endl;
	}
	else {
		cout << "Close" << endl;
	}

return 0;
}
*/

/*
//3
int main() {
	while (true) {
		int n;
		cin >> n;
		if (n == 0) break;

		int H[110];
		for (int i = 0; i < n + 1; i++) {
			cin >> H[i];
		}

	}

return 0;
}
*/


//4
int main() {
	while (true) {
		int N;
		cin >> N;
		if (N == 0) break;
		int num[4] = {};
		num[0] = N / 1000;
		num[1] = N % 1000 / 100;
		num[2] = N % 100 / 10;
		num[3] = N % 10;

		if (num[0] == num[1] && num[0] == num[2] && num[0] == num[3]) {
			cout << "NA" << endl;
			continue;
		}
		if (N == 6174) {
			cout << 0 << endl;
		continue;
		}

		
		int count = 0;
		while (true) {
			sort(num, num + 4);
			int S = 1000 * num[0] + 100 * num[1] + 10 * num[2] + num[3];
			int L = 1000 * num[3] + 100 * num[2] + 10 * num[1] + num[0];
			N = L - S;
			count++;
			if (N == 6174) {
				break;
			}
			num[0] = N / 1000;
			num[1] = N % 1000 / 100;
			num[2] = N % 100 / 10;
			num[3] = N % 10;
		}

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


/*
//5
int main() {


return 0;
}
*/

/*
//6
int main() {


return 0;
}
*/

/*
//7
int main() {


return 0;
}
*/

/*
//8
int main() {


return 0;
}
*/

/*
//9
int main() {


return 0;
}
*/

/*
//10
int main() {


return 0;
}
*/