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 <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;
}
*/

C++: #include <stdio.h>
#include <stdlib.h>

int main(void) {
  char s[10], a[10], b[10];
  int i, j;
  a[4] = 0, b[4] = 0;
  while( 1 ) {
    int ans = 0;
    scanf("%s", s);
    if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
      if(s[0] == '0') break;
      else {
        printf("NA\n");
        continue;
      }
    }
    while(s[0] != '6' || s[1] != '1' || s[2] != '7' || s[3] != '4') {
      for(i = 0; i < 4; ++i) a[i] = s[i], b[i] = s[i];
      for(i = 0; i < 4; ++i) for(j = i + 1; j < 4; ++j) {
        char t;
        if(a[i] > a[j]) t = a[i], a[i] = a[j], a[j] = t;
        if(b[i] < b[j]) t = b[i], b[i] = b[j], b[j] = t;
      }
      int na = atoi(a), nb = atoi(b);
      int num = nb - na;
      for(i = 3; i >= 0; --i) {
        s[i] = num % 10 + '0';
        num /= 10;
      }
      ans++;
    }
    printf("%d\n", ans);
  }
  return 0;
}

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

using namespace std;

int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

int main(void)
{
	string n;
	while (cin >> n && n != "0000"){
		if (count(n.begin(), n.end(), n[0]) == 4){
			cout << "NA" << endl;
		}
		assert(n != "0000");

		int tmp = toInt(n);
		char str[128];
		sprintf(str, "%04d", tmp);
		n = string(str);
		int cnt = 0;
		while (n != "6174"){
			string L = n; sort(L.rbegin(), L.rend());
			string S = n; sort(S.begin(), S.end());
			// cout << n << " " << L << " " << S << endl;
			int tmp = toInt(L) - toInt(S);
			char str[128];
			sprintf(str, "%04d", tmp);
			n = string(str);
			cnt++;
		}
		cout << cnt << endl;
	}

	return 0;
}