Go: package main

import (
	"fmt"
	"sort"
	"strconv"
)

func main() {
	var v int
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		count := 0
		for v != 6174 {
			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))
			v = l - s
			count++
		}
		fmt.Println(count)
	}
}

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

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;
				return 0;
			}
			++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;
	}
	return 0;
}

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

string toString(int n){
  ostringstream oss;
  oss << n;
  return oss.str();
}

int toInt(string s){
  istringstream iss(s);
  int ret;
  iss >> ret;
  return ret;
}


int main(){
  string s;
  while(cin >> s){
    if(s == "0000") break;
    if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) cout << "NA" << endl;
    
    int cnt = 0;
    
    while(s != "6174"){
      cnt++;
      string L, S;
      L = s;
      sort(L.begin(), L.end());
      S = L;
      reverse(L.begin(), L.end());
      int a, b;
      a = toInt(L);
      b = toInt(S);
      s = toString(a-b);
    }
    cout << cnt << endl;
  }
  return 0;
}