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

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 <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <sstream>

#define rep(i, j) FOR(i, 0, j)
#define FOR(i, j, k) for(int i = j; i < k; ++i)

using namespace std;

int main(){
  char n[5];
  while(scanf("%s", n) && strcmp(n, "0000") != 0){
    if(n[0] == n[1] && n[1] == n[2] && n[2] == n[3]){ puts("NA"); continue;}
    int c = 0;
    while(strcmp(n, "6174") != 0 && c < 10){
      rep(i, 4) FOR(j, i + 1, 4){
	if(n[i] > n[j]){ char tmp = n[i]; n[i] = n[j]; n[j] = tmp;}
      }
      int a = atoi(n);
      char tmp = n[0]; n[0] = n[3]; n[3] = tmp;
      tmp = n[1]; n[1] = n[2]; n[2] = tmp;
      int b = atoi(n);
      b -=  a;
      sprintf(n, "%d", b);
      c++;
    }
    printf("%d\n", c);
  }
  return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> pl;

int main()
{
	while (1) {
		string s;
		cin >> s;
		if (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0')
			break;
		if (s == "6174") {
			cout << 0 << endl;
		}
		else if (s[0] == s[1] &&  s[2] == s[3] && s[1] == s[2])
			cout << "NA" << endl;
		else {
			ll ans = 0;
			while (s != "6174") {
				int a[4];
				for (int i = 0; i < 4; i++)
					a[i] = s[i] - '0';

				sort(a, a + 4);

				ll small=0,large=0;
				for (int i = 0; i < 4; i++)
					large += a[i] * (ll)pow(10, i);
				
				reverse(a,a+4);
				for (int i = 0; i < 4; i++) 
					small += a[i] * (ll)pow(10, i);
				
				s = to_string(large - small);
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

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

int main() {
  string s;
  while(cin >> s && s!="0000") {
    int cnt=0;
    bool ck=true;
    while(s!="6174") {
      cnt++;
      int a[4];
      fill(a,a+4,0);
      for(int i=0; i<4; i++) a[i]=(int)s[i]-48;
      sort(a,a+4);
      int b=0,c=0,k1=1,k2=1000;
      for(int i=0; i<4; i++) {
	b+=a[i]*k1;
	c+=a[i]*k2;
	k1*=10;
	k2/=10;
      }
      stringstream ss;
      ss << b-c;
      ss >> s;
      if(s.size()<4) {
	while(s.size()<4) s="0"+s;
      }
      if(s=="0000") {
	cout << "NA" << endl;
	ck=false;
	break;
      }
    }
    if(ck) cout << cnt << endl;
  }
  return 0;
}

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

int main(){
  int t[4];
  int tt[4];
  int a,b,bb,c;
  while(true){
    cin>>a;
    c=0;
    if(a==0)break;
    for(int i=1111;i<10000;i+=1111){
      if(i==a){
	cout<<"NA"<<endl;
	c=-1;
      }
    }
    if(c==-1)continue;
    while(a!=6174){
      b=a;
      c++;
      for(int i=3;i>=0;i--){
	t[i]=b%10;
	b/=10;
      }
      sort(t,t+4);
      reverse(t,t+4);
      b=t[0]*1000+t[1]*100+t[2]*10+t[3];

      bb=a;
      for(int i=3;i>=0;i--){
	t[i]=bb%10;
	bb/=10;
      }
      sort(t,t+4);
      bb=t[0]*1000+t[1]*100+t[2]*10+t[3];
      
      a=b-bb;
      //cout<<a<<endl;
    } 
    cout<<c<<endl;
    
  }
  return 0;
}

C++: /*
 * src.cpp
 *
 *  Created on: 2016/08/08
 *      Author: joi
 */

#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)

char N[5];

char L[5],S[5];
char g[5]="6174";

void itoa(int x){
	REP(i,4){
		N[3-i]=x%10+'0';
		x/=10;
	}
}

bool isend(){
	REP(i,4){
		if (N[i]!='0'){
			return false;
		}
	}
	return true;
}

bool isNA(){
	char c=N[0];
	FOR(i,1,4){
		if (N[i]!=c){
			return false;
		}
	}
	return true;
}

int main(){
	do{
		scanf("%s",N);
		if (!isend()){
			if (isNA()){
				printf("NA\n");
			}else{
				int ans=0;
				while (strcmp(N,g)){
					strcpy(L,N);
					strcpy(S,N);
					sort(L,L+4);
					reverse(L,L+4);
					sort(S,S+4);
					itoa(atoi(L)-atoi(S));
					ans++;
				}
				printf("%d\n",ans);
			}
		}
	}while (!isend());
	return 0;
}

C++: //ソートを使いたくなかっただけなんだ
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	while (true)
	{
		string N;
		cin >> N;
		if (N == "0000")
		{
			break;
		}
		else if (N[0]==N[1] && N[1]==N[2] && N[2]==N[3])
		{
			cout << "NA" << endl;
			continue;
		}

		int cnt1 = 0;
		while (N != "6174")
		{
			int cnts[10];
			for (int i=0; i<10; i++)
			{
				cnts[i] = 0;
				unsigned int j = 0;
				while ((j=N.find('0'+i, j)) != string::npos)
				{
					++j;
					++cnts[i];
				}
			}
			string L="0000", S="0000";
			int cnt2 = 0;
			for (int i=0; i<10; i++)
			{
				for (int j=0; j<cnts[i]; j++)
				{
					L[3-cnt2] = S[cnt2] = '0'+i;
					++cnt2;
				}
			}

			stringstream ss_L(L), ss_S(S);
			int i_L, i_S;
			ss_L >> i_L;
			ss_S >> i_S;
			stringstream ss_N;
			ss_N << setfill('0') << setw(4) << i_L-i_S;
			N = ss_N.str();
			++cnt1;
		}
		cout << cnt1 << endl;
	}

	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;

#define ASC(vec) vec.begin(), vec.end()		// 昇順ソート 例：sort(ASC(vec));
#define DESC(vec) vec.rbegin(), vec.rend()	// 降順ソート 例:sort(DESC(vec));
#define rep(i, n) for(int i = 0; i < n; i++)
#define Rep(i, n) for(int i = 1; i < n; i++)
#define In(vec) rep(i, n) cin>>vec[i];
#define ANS(s) cout << s << endl
#define inc(a, b) cin >> a >> b;

const int mod = 1000000007;
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;

using ii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
using vl = vector<ll>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vii = vector<pair<int, int>>;

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
int round_int(int a, int b) { return (a + (b - 1)) / b; }


// 4,8方向に探索する際に用いる配列
int dx[4] = { 0,  0, 1, -1 };
int dy[4] = { 1, -1, 0,  0 };
//int dx[8] = { 1, 0, -1,  0, 1, -1, -1,  1 };
//int dy[8] = { 0, 1,  0, -1, 1,  1, -1, -1 };

constexpr auto MAX = 100010;

// ────────────────────────────────────────────────────

string solve(string s) {
	sort(DESC(s));
	int ma = atoi(s.c_str());
	sort(ASC(s));
	int mi = atoi(s.c_str());

	ostringstream sout;
	sout << setfill('0') << setw(4) << to_string(ma - mi);
	string ret = sout.str();
	return ret;
}

int main() {
	while (1) {
		string s; cin >> s;
		int tmp = atoi(s.c_str());
		if (!tmp) break;

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

		int cnt = 0;
		while (s != "6174") {
			s = solve(s);
			cnt++;
		}
		cout << cnt << endl;
	}
}